smaller refactorings continued

This commit is contained in:
azivner 2018-04-01 11:42:12 -04:00
parent fad0ec757b
commit acc82f39c4
13 changed files with 96 additions and 100 deletions

View File

@ -4,7 +4,7 @@ const utils = require('../services/utils');
const repository = require('../services/repository');
class Entity {
constructor(row) {
constructor(row = {}) {
utils.assertArguments(row);
for (const key in row) {

View File

@ -1,5 +1,6 @@
const Note = require('../entities/note');
const NoteRevision = require('../entities/note_revision');
const Image = require('../entities/image');
const NoteImage = require('../entities/note_image');
const Branch = require('../entities/branch');
const Label = require('../entities/label');
@ -17,6 +18,9 @@ function createEntityFromRow(row) {
else if (row.noteImageId) {
entity = new NoteImage(row);
}
else if (row.imageId) {
entity = new Image(row);
}
else if (row.branchId) {
entity = new Branch(row);
}

View File

@ -15,6 +15,18 @@ function LabelsModel() {
this.labels = ko.observableArray();
this.updateLabelPositions = function() {
let position = 0;
// we need to update positions by searching in the DOM, because order of the
// labels in the viewmodel (self.labels()) stays the same
$labelsBody.find('input[name="position"]').each(function() {
const label = self.getTargetLabel(this);
label().position = position++;
});
};
this.loadLabels = async function() {
const noteId = noteDetailService.getCurrentNoteId();
@ -32,17 +44,7 @@ function LabelsModel() {
$labelsBody.sortable({
handle: '.handle',
containment: $labelsBody,
update: function() {
let position = 0;
// we need to update positions by searching in the DOM, because order of the
// labels in the viewmodel (self.labels()) stays the same
$labelsBody.find('input[name="position"]').each(function() {
const label = self.getTargetLabel(this);
label().position = position++;
});
}
update: this.updateLabelPositions
});
};
@ -80,6 +82,8 @@ function LabelsModel() {
return;
}
self.updateLabelPositions();
const noteId = noteDetailService.getCurrentNoteId();
const labelsToSave = self.labels()

View File

@ -438,35 +438,28 @@ async function createNote(node, parentNoteId, target, isProtected) {
const newNoteName = "new note";
const result = await server.post('notes/' + parentNoteId + '/children', {
const {note, branch} = await server.post('notes/' + parentNoteId + '/children', {
title: newNoteName,
target: target,
target_branchId: node.data.branchId,
isProtected: isProtected
});
const note = new NoteShort(treeCache, {
noteId: result.noteId,
title: result.title,
isProtected: result.isProtected,
type: result.type,
mime: result.mime
});
const noteEntity = new NoteShort(treeCache, note);
const branchEntity = new Branch(treeCache, branch);
const branch = new Branch(treeCache, result);
treeCache.add(note, branch);
treeCache.add(noteEntity, branchEntity);
noteDetailService.newNoteCreated();
const newNode = {
title: newNoteName,
noteId: result.noteId,
noteId: branchEntity.noteId,
parentNoteId: parentNoteId,
refKey: result.noteId,
branchId: result.branchId,
refKey: branchEntity.noteId,
branchId: branchEntity.branchId,
isProtected: isProtected,
extraClasses: await treeBuilder.getExtraClasses(note)
extraClasses: await treeBuilder.getExtraClasses(noteEntity)
};
if (target === 'after') {

View File

@ -1,9 +1,9 @@
"use strict";
const sql = require('../../services/sql');
const notes = require('../../services/notes');
const labels = require('../../services/labels');
const protected_session = require('../../services/protected_session');
const repository = require('../../services/repository');
async function uploadFile(req) {
const parentNoteId = req.params.parentNoteId;
@ -11,50 +11,46 @@ async function uploadFile(req) {
const originalName = file.originalname;
const size = file.size;
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]);
const parentNote = await repository.getNote(parentNoteId);
if (!note) {
if (!parentNote) {
return [404, `Note ${parentNoteId} doesn't exist.`];
}
const {noteId} = await notes.createNewNote(parentNoteId, {
const {note} = await notes.createNewNote(parentNoteId, {
title: originalName,
content: file.buffer,
target: 'into',
isProtected: false,
type: 'file',
mime: file.mimetype
}, req);
});
await labels.createLabel(noteId, "original_file_name", originalName);
await labels.createLabel(noteId, "file_size", size);
await labels.createLabel(note.noteId, "original_file_name", originalName);
await labels.createLabel(note.noteId, "file_size", size);
return {
noteId: noteId
noteId: note.noteId
};
}
async function downloadFile(req, res) {
const noteId = req.params.noteId;
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
const note = await repository.getNote(noteId);
if (!note) {
return res.status(404).send(`Note ${noteId} doesn't exist.`);
}
if (note.isProtected) {
if (!protected_session.isProtectedSessionAvailable()) {
res.status(401).send("Protected session not available");
return;
}
protected_session.decryptNote(note);
if (note.isProtected && !protected_session.isProtectedSessionAvailable()) {
res.status(401).send("Protected session not available");
return;
}
const labelMap = await labels.getNoteLabelMap(noteId);
const labelMap = await note.getLabelMap();
const fileName = labelMap.original_file_name ? labelMap.original_file_name : note.title;
res.setHeader('Content-Disposition', 'file; filename=' + fileName);
res.setHeader('Content-Disposition', 'file; filename="' + fileName + '"');
res.setHeader('Content-Type', note.mime);
res.send(note.content);

View File

@ -1,12 +1,12 @@
"use strict";
const sql = require('../../services/sql');
const image = require('../../services/image');
const repository = require('../../services/repository');
const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR;
const fs = require('fs');
async function returnImage(req, res) {
const image = await sql.getRow("SELECT * FROM images WHERE imageId = ?", [req.params.imageId]);
const image = await repository.getImage(req.params.imageId);
if (!image) {
return res.sendStatus(404);
@ -25,7 +25,7 @@ async function uploadImage(req) {
const noteId = req.query.noteId;
const file = req.file;
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
const note = await repository.getNote(noteId);
if (!note) {
return [404, `Note ${noteId} doesn't exist.`];

View File

@ -1,6 +1,6 @@
"use strict";
const sql = require('../../services/sql');
const repository = require('../../services/repository');
const labels = require('../../services/labels');
const notes = require('../../services/notes');
const tar = require('tar-stream');
@ -89,9 +89,9 @@ async function importTar(req) {
const parentNoteId = req.params.parentNoteId;
const file = req.file;
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]);
const parentNote = await repository.getNote(parentNoteId);
if (!note) {
if (!parentNote) {
return [404, `Note ${parentNoteId} doesn't exist.`];
}

View File

@ -1,25 +1,26 @@
"use strict";
const sql = require('../../services/sql');
const sync_table = require('../../services/sync_table');
const utils = require('../../services/utils');
const labels = require('../../services/labels');
const repository = require('../../services/repository');
const Label = require('../../entities/label');
async function getNoteLabels(req) {
const noteId = req.params.noteId;
return await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
return await repository.getEntities("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
}
async function updateNoteLabels(req, res, next) {
async function updateNoteLabels(req) {
const noteId = req.params.noteId;
const labels = req.body;
const now = utils.nowDate();
for (const label of labels) {
let labelEntity;
if (label.labelId) {
await sql.execute("UPDATE labels SET name = ?, value = ?, dateModified = ?, isDeleted = ?, position = ? WHERE labelId = ?",
[label.name, label.value, now, label.isDeleted, label.position, label.labelId]);
labelEntity = await repository.getLabel(label.labelId);
}
else {
// if it was "created" and then immediatelly deleted, we just don't create it at all
@ -27,27 +28,23 @@ async function updateNoteLabels(req, res, next) {
continue;
}
label.labelId = utils.newLabelId();
await sql.insert("labels", {
labelId: label.labelId,
noteId: noteId,
name: label.name,
value: label.value,
position: label.position,
dateCreated: now,
dateModified: now,
isDeleted: false
});
labelEntity = new Label();
labelEntity.labelId = utils.newLabelId();
labelEntity.noteId = noteId;
}
await sync_table.addLabelSync(label.labelId);
labelEntity.name = label.name;
labelEntity.value = label.value;
labelEntity.position = label.position;
labelEntity.isDeleted = label.isDeleted;
await labelEntity.save();
}
return await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
return await repository.getEntities("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
}
async function getAllLabelNames(req) {
async function getAllLabelNames() {
const names = await sql.getColumn("SELECT DISTINCT name FROM labels WHERE isDeleted = 0");
for (const label of labels.BUILTIN_LABELS) {

View File

@ -3,22 +3,18 @@
const sql = require('../../services/sql');
const notes = require('../../services/notes');
const utils = require('../../services/utils');
const protected_session = require('../../services/protected_session');
const tree = require('../../services/tree');
const sync_table = require('../../services/sync_table');
const repository = require('../../services/repository');
async function getNote(req) {
const noteId = req.params.noteId;
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
const note = await repository.getNote(noteId);
if (!note) {
return [404, "Note " + noteId + " has not been found."];
}
protected_session.decryptNote(note);
if (note.type === 'file') {
// no need to transfer (potentially large) file payload for this request
note.content = null;
@ -31,12 +27,11 @@ async function createNote(req) {
const parentNoteId = req.params.parentNoteId;
const newNote = req.body;
const { noteId, branchId, note } = await notes.createNewNote(parentNoteId, newNote, req);
const { note, branch } = await notes.createNewNote(parentNoteId, newNote, req);
return {
'noteId': noteId,
'branchId': branchId,
'note': note
note,
branch
};
}

View File

@ -47,7 +47,7 @@ async function uploadImage(req) {
const parentNoteId = await date_notes.getDateNoteId(req.headers['x-local-date']);
const {noteId} = await notes.createNewNote(parentNoteId, {
const {note} = await notes.createNewNote(parentNoteId, {
title: "Sender image",
content: "",
target: 'into',
@ -56,13 +56,13 @@ async function uploadImage(req) {
mime: 'text/html'
});
const {fileName, imageId} = await image.saveImage(file, null, noteId);
const {fileName, imageId} = await image.saveImage(file, null, note.noteId);
const url = `/api/images/${imageId}/${fileName}`;
const content = `<img src="${url}"/>`;
await sql.execute("UPDATE notes SET content = ? WHERE noteId = ?", [content, noteId]);
await sql.execute("UPDATE notes SET content = ? WHERE noteId = ?", [content, note.noteId]);
}
async function saveNote(req) {

View File

@ -14,12 +14,14 @@ const DAYS = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Satur
const MONTHS = ['January','February','March','April','May','June','July','August','September','October','November','December'];
async function createNote(parentNoteId, noteTitle, noteText) {
return (await notes.createNewNote(parentNoteId, {
const {note} = await notes.createNewNote(parentNoteId, {
title: noteTitle,
content: noteText,
target: 'into',
isProtected: false
})).noteId;
});
return note.noteId;
}
async function getNoteStartingWith(parentNoteId, startsWith) {
@ -34,11 +36,13 @@ async function getRootCalendarNoteId() {
WHERE labels.name = '${CALENDAR_ROOT_LABEL}' AND notes.isDeleted = 0`);
if (!rootNoteId) {
rootNoteId = (await notes.createNewNote('root', {
const {rootNote} = await notes.createNewNote('root', {
title: 'Calendar',
target: 'into',
isProtected: false
})).noteId;
});
const rootNoteId = rootNote.noteId;
await labels.createLabel(rootNoteId, CALENDAR_ROOT_LABEL);
}

View File

@ -72,9 +72,7 @@ async function createNewNote(parentNoteId, noteOpts) {
await branch.save();
return {
noteId: note.noteId,
note,
branchId: branch.branchId,
branch
};
}
@ -83,7 +81,7 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})
if (!parentNoteId) throw new Error("Empty parentNoteId");
if (!title) throw new Error("Empty title");
const note = {
const noteData = {
title: title,
content: extraOptions.json ? JSON.stringify(content, null, '\t') : content,
target: 'into',
@ -92,25 +90,25 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})
mime: extraOptions.mime
};
if (extraOptions.json && !note.type) {
note.type = "code";
note.mime = "application/json";
if (extraOptions.json && !noteData.type) {
noteData.type = "code";
noteData.mime = "application/json";
}
if (!note.type) {
note.type = "text";
note.mime = "text/html";
if (!noteData.type) {
noteData.type = "text";
noteData.mime = "text/html";
}
const {noteId} = await createNewNote(parentNoteId, note);
const {note} = await createNewNote(parentNoteId, noteData);
if (extraOptions.labels) {
for (const labelName in extraOptions.labels) {
await labels.createLabel(noteId, labelName, extraOptions.labels[labelName]);
await labels.createLabel(note.noteId, labelName, extraOptions.labels[labelName]);
}
}
return noteId;
return note.noteId;
}
async function protectNoteRecursively(note, protect) {

View File

@ -37,6 +37,10 @@ async function getImage(imageId) {
return await getEntity("SELECT * FROM images WHERE imageId = ?", [imageId]);
}
async function getLabel(labelId) {
return await getEntity("SELECT * FROM labels WHERE labelId = ?", [labelId]);
}
async function updateEntity(entity) {
if (entity.beforeSaving) {
entity.beforeSaving();
@ -59,6 +63,7 @@ module.exports = {
getNote,
getBranch,
getImage,
getLabel,
updateEntity,
setEntityConstructor
};