smaller refactorings continued

This commit is contained in:
azivner 2018-04-01 12:03:21 -04:00
parent acc82f39c4
commit 8ba830c04b
8 changed files with 41 additions and 34 deletions

View File

@ -4,6 +4,7 @@ const Image = require('../entities/image');
const NoteImage = require('../entities/note_image'); const NoteImage = require('../entities/note_image');
const Branch = require('../entities/branch'); const Branch = require('../entities/branch');
const Label = require('../entities/label'); const Label = require('../entities/label');
const RecentNote = require('../entities/recent_note');
const repository = require('../services/repository'); const repository = require('../services/repository');
function createEntityFromRow(row) { function createEntityFromRow(row) {
@ -21,6 +22,9 @@ function createEntityFromRow(row) {
else if (row.imageId) { else if (row.imageId) {
entity = new Image(row); entity = new Image(row);
} }
else if (row.branchId && row.notePath) {
entity = new RecentNote(row);
}
else if (row.branchId) { else if (row.branchId) {
entity = new Branch(row); entity = new Branch(row);
} }

View File

@ -0,0 +1,10 @@
"use strict";
const Entity = require('./entity');
class RecentNote extends Entity {
static get tableName() { return "recent_notes"; }
static get primaryKeyName() { return "branchId"; }
}
module.exports = RecentNote;

View File

@ -41,11 +41,11 @@ async function moveToNode(nodesToMove, toNode) {
return; return;
} }
await changeNode(nodeToMove, node => { await changeNode(nodeToMove, async node => {
// first expand which will force lazy load and only then move the node // first expand which will force lazy load and only then move the node
// if this is not expanded before moving, then lazy load won't happen because it already contains node // if this is not expanded before moving, then lazy load won't happen because it already contains node
// this doesn't work if this isn't a folder yet, that's why we expand second time below // this doesn't work if this isn't a folder yet, that's why we expand second time below
toNode.setExpanded(true); await toNode.setExpanded(true);
node.moveTo(toNode); node.moveTo(toNode);
@ -53,7 +53,7 @@ async function moveToNode(nodesToMove, toNode) {
toNode.renderTitle(); toNode.renderTitle();
// this expands the note in case it become the folder only after the move // this expands the note in case it become the folder only after the move
toNode.setExpanded(true); await toNode.setExpanded(true);
}); });
} }
} }
@ -117,7 +117,7 @@ async function changeNode(node, func) {
const childNoteId = node.data.noteId; const childNoteId = node.data.noteId;
const oldParentNoteId = node.data.parentNoteId; const oldParentNoteId = node.data.parentNoteId;
func(node); await func(node);
const newParentNoteId = node.data.parentNoteId = utils.isTopLevelNode(node) ? 'root' : node.getParent().data.noteId; const newParentNoteId = node.data.parentNoteId = utils.isTopLevelNode(node) ? 'root' : node.getParent().data.noteId;

View File

@ -3,7 +3,7 @@
const changePasswordService = require('../../services/change_password'); const changePasswordService = require('../../services/change_password');
async function changePassword(req) { async function changePassword(req) {
return await changePasswordService.changePassword(req.body['current_password'], req.body['new_password'], req); return await changePasswordService.changePassword(req.body.current_password, req.body.new_password);
} }
module.exports = { module.exports = {

View File

@ -1,12 +1,12 @@
"use strict"; "use strict";
const sql = require('../../services/sql'); const repository = require('../../services/repository');
const utils = require('../../services/utils'); const utils = require('../../services/utils');
const sync_table = require('../../services/sync_table');
const options = require('../../services/options'); const options = require('../../services/options');
const RecentNote = require('../../entities/recent_note');
async function getRecentNotes() { async function getRecentNotes() {
return await sql.getRows(` return await repository.getEntities(`
SELECT SELECT
recent_notes.* recent_notes.*
FROM FROM
@ -20,19 +20,18 @@ async function getRecentNotes() {
LIMIT 200`); LIMIT 200`);
} }
async function addRecentNote(req) { async function addRecentNote(req) {
const branchId = req.params.branchId; const branchId = req.params.branchId;
const notePath = req.params.notePath; const notePath = req.params.notePath;
await sql.replace('recent_notes', { const recentNote = new RecentNote({
branchId: branchId, branchId: branchId,
notePath: notePath, notePath: notePath,
dateAccessed: utils.nowDate(), dateAccessed: utils.nowDate(),
isDeleted: 0 isDeleted: 0
}); });
await sync_table.addRecentNoteSync(branchId); await recentNote.save();
await options.setOption('start_note_path', notePath); await options.setOption('start_note_path', notePath);

View File

@ -5,21 +5,17 @@ const script = require('../../services/script');
const repository = require('../../services/repository'); const repository = require('../../services/repository');
async function exec(req) { async function exec(req) {
const ret = await script.executeScript(req, req.body.script, req.body.params, req.body.startNoteId, req.body.currentNoteId); const result = await script.executeScript(req.body.script, req.body.params, req.body.startNoteId, req.body.currentNoteId);
return { return { executionResult: result };
executionResult: ret
};
} }
async function run(req) { async function run(req) {
const note = await repository.getNote(req.params.noteId); const note = await repository.getNote(req.params.noteId);
const ret = await script.executeNote(req, note); const result = await script.executeNote(req, note);
return { return { executionResult: result };
executionResult: ret
};
} }
async function getStartupBundles(req) { async function getStartupBundles(req) {

View File

@ -27,12 +27,10 @@ async function moveBranchToParent(req) {
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]); const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
const now = utils.nowDate(); const branch = await repository.getBranch(branchId);
branch.parentNoteId = parentNoteId;
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?", branch.notePosition = newNotePos;
[parentNoteId, newNotePos, now, branchId]); await branch.save();
await sync_table.addBranchSync(branchId);
return { success: true }; return { success: true };
} }
@ -57,10 +55,10 @@ async function moveBranchBeforeNote(req) {
await sync_table.addNoteReorderingSync(beforeNote.parentNoteId); await sync_table.addNoteReorderingSync(beforeNote.parentNoteId);
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?", const branch = await repository.getBranch(branchId);
[beforeNote.parentNoteId, beforeNote.notePosition, utils.nowDate(), branchId]); branch.parentNoteId = beforeNote.parentNoteId;
branch.notePosition = beforeNote.notePosition;
await sync_table.addBranchSync(branchId); await branch.save();
return { success: true }; return { success: true };
} }
@ -85,10 +83,10 @@ async function moveBranchAfterNote(req) {
await sync_table.addNoteReorderingSync(afterNote.parentNoteId); await sync_table.addNoteReorderingSync(afterNote.parentNoteId);
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?", const branch = await repository.getBranch(branchId);
[afterNote.parentNoteId, afterNote.notePosition + 1, utils.nowDate(), branchId]); branch.parentNoteId = afterNote.parentNoteId;
branch.notePosition = afterNote.notePosition + 1;
await sync_table.addBranchSync(branchId); await branch.save();
return { success: true }; return { success: true };
} }

View File

@ -6,7 +6,7 @@ const my_scrypt = require('./my_scrypt');
const utils = require('./utils'); const utils = require('./utils');
const password_encryption = require('./password_encryption'); const password_encryption = require('./password_encryption');
async function changePassword(currentPassword, newPassword, req) { async function changePassword(currentPassword, newPassword) {
if (!await password_encryption.verifyPassword(currentPassword)) { if (!await password_encryption.verifyPassword(currentPassword)) {
return { return {
success: false, success: false,