From fad0ec757b6f12042b4f4c20c31582081b5d204c Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 1 Apr 2018 11:05:09 -0400 Subject: [PATCH] smaller refactorings (mostly entitization) --- src/entities/image.js | 19 +++++++++++++++++++ src/entities/note_image.js | 13 +++++++++++++ src/routes/api/cleanup.js | 12 ++++++++---- src/routes/api/cloning.js | 17 +++++++---------- src/routes/api/event_log.js | 4 +--- src/routes/api/export.js | 12 ++++-------- src/routes/api/note_revisions.js | 8 ++------ src/routes/api/tree.js | 14 +++++--------- src/routes/routes.js | 2 +- src/services/notes.js | 7 ++++--- src/services/protected_session.js | 12 ------------ src/services/repository.js | 5 +++++ 12 files changed, 69 insertions(+), 56 deletions(-) create mode 100644 src/entities/image.js diff --git a/src/entities/image.js b/src/entities/image.js new file mode 100644 index 000000000..5116ce2b8 --- /dev/null +++ b/src/entities/image.js @@ -0,0 +1,19 @@ +"use strict"; + +const Entity = require('./entity'); +const utils = require('../services/utils'); + +class Image extends Entity { + static get tableName() { return "images"; } + static get primaryKeyName() { return "imageId"; } + + beforeSaving() { + if (!this.dateCreated) { + this.dateCreated = utils.nowDate(); + } + + this.dateModified = utils.nowDate(); + } +} + +module.exports = Image; \ No newline at end of file diff --git a/src/entities/note_image.js b/src/entities/note_image.js index 434a27070..2a4edefa5 100644 --- a/src/entities/note_image.js +++ b/src/entities/note_image.js @@ -2,6 +2,7 @@ const Entity = require('./entity'); const repository = require('../services/repository'); +const utils = require('../services/utils'); class NoteImage extends Entity { static get tableName() { return "note_images"; } @@ -10,6 +11,18 @@ class NoteImage extends Entity { async getNote() { return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]); } + + async getImage() { + return await repository.getEntity("SELECT * FROM images WHERE imageId = ?", [this.imageId]); + } + + beforeSaving() { + if (!this.dateCreated) { + this.dateCreated = utils.nowDate(); + } + + this.dateModified = utils.nowDate(); + } } module.exports = NoteImage; \ No newline at end of file diff --git a/src/routes/api/cleanup.js b/src/routes/api/cleanup.js index 16da38bd0..02968799f 100644 --- a/src/routes/api/cleanup.js +++ b/src/routes/api/cleanup.js @@ -4,6 +4,7 @@ const sql = require('../../services/sql'); const utils = require('../../services/utils'); const sync_table = require('../../services/sync_table'); const log = require('../../services/log'); +const repository = require('../../services/repository'); async function cleanupSoftDeletedItems() { const noteIdsToDelete = await sql.getColumn("SELECT noteId FROM notes WHERE isDeleted = 1"); @@ -33,6 +34,9 @@ async function cleanupSoftDeletedItems() { await sync_table.cleanupSyncRowsForMissingEntities("branches", "branchId"); await sync_table.cleanupSyncRowsForMissingEntities("note_revisions", "noteRevisionId"); await sync_table.cleanupSyncRowsForMissingEntities("recent_notes", "branchId"); + await sync_table.cleanupSyncRowsForMissingEntities("images", "imageId"); + await sync_table.cleanupSyncRowsForMissingEntities("note_images", "noteImageId"); + await sync_table.cleanupSyncRowsForMissingEntities("labels", "labelId"); log.info("Following notes has been completely cleaned from database: " + noteIdsSql); } @@ -51,10 +55,10 @@ async function cleanupUnusedImages() { for (const imageId of unusedImageIds) { log.info(`Deleting unused image: ${imageId}`); - await sql.execute("UPDATE images SET isDeleted = 1, data = null, dateModified = ? WHERE imageId = ?", - [now, imageId]); - - await sync_table.addImageSync(imageId); + const image = await repository.getImage(imageId); + image.isDeleted = true; + image.data = null; + await image.save(); } } diff --git a/src/routes/api/cloning.js b/src/routes/api/cloning.js index fdf39cfca..b32f35d99 100644 --- a/src/routes/api/cloning.js +++ b/src/routes/api/cloning.js @@ -4,6 +4,7 @@ const sql = require('../../services/sql'); const utils = require('../../services/utils'); const sync_table = require('../../services/sync_table'); const tree = require('../../services/tree'); +const Branch = require('../../entities/branch'); async function cloneNoteToParent(req) { const parentNoteId = req.params.parentNoteId; @@ -19,7 +20,7 @@ async function cloneNoteToParent(req) { const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]); const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; - const branch = { + const branch = new Branch({ branchId: utils.newBranchId(), noteId: childNoteId, parentNoteId: parentNoteId, @@ -28,11 +29,9 @@ async function cloneNoteToParent(req) { isExpanded: 0, dateModified: utils.nowDate(), isDeleted: 0 - }; + }); - await sql.replace("branches", branch); - - await sync_table.addBranchSync(branch.branchId); + await branch.save(); await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]); @@ -58,7 +57,7 @@ async function cloneNoteAfter(req) { await sync_table.addNoteReorderingSync(afterNote.parentNoteId); - const branch = { + const branch = new Branch({ branchId: utils.newBranchId(), noteId: noteId, parentNoteId: afterNote.parentNoteId, @@ -66,11 +65,9 @@ async function cloneNoteAfter(req) { isExpanded: 0, dateModified: utils.nowDate(), isDeleted: 0 - }; + }); - await sql.replace("branches", branch); - - await sync_table.addBranchSync(branch.branchId); + await branch.save(); return { success: true }; } diff --git a/src/routes/api/event_log.js b/src/routes/api/event_log.js index 9644d4a7a..42b14752d 100644 --- a/src/routes/api/event_log.js +++ b/src/routes/api/event_log.js @@ -12,9 +12,7 @@ async function deleteOld() { const cutoffId = await sql.getValue("SELECT id FROM event_log ORDER BY id DESC LIMIT 1000, 1"); if (cutoffId) { - await sql.doInTransaction(async () => { - await sql.execute("DELETE FROM event_log WHERE id < ?", [cutoffId]); - }); + await sql.execute("DELETE FROM event_log WHERE id < ?", [cutoffId]); } } diff --git a/src/routes/api/export.js b/src/routes/api/export.js index 2186b877f..a988e0904 100644 --- a/src/routes/api/export.js +++ b/src/routes/api/export.js @@ -24,8 +24,8 @@ async function exportNote(req, res) { } async function exportNoteInner(branchId, directory, pack) { - const branch = await sql.getRow("SELECT * FROM branches WHERE branchId = ?", [branchId]); - const note = await repository.getEntity("SELECT notes.* FROM notes WHERE noteId = ?", [branch.noteId]); + const branch = await repository.getBranch(branchId); + const note = await branch.getNote(); if (note.isProtected) { return; @@ -46,12 +46,8 @@ async function exportNoteInner(branchId, directory, pack) { pack.entry({ name: childFileName + ".dat", size: content.length }, content); - const children = await sql.getRows("SELECT * FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [note.noteId]); - - if (children.length > 0) { - for (const child of children) { - await exportNoteInner(child.branchId, childFileName + "/", pack); - } + for (const child of await note.getChildBranches()) { + await exportNoteInner(child.branchId, childFileName + "/", pack); } return childFileName; diff --git a/src/routes/api/note_revisions.js b/src/routes/api/note_revisions.js index d72f19282..e35f509e7 100644 --- a/src/routes/api/note_revisions.js +++ b/src/routes/api/note_revisions.js @@ -1,14 +1,10 @@ "use strict"; -const sql = require('../../services/sql'); -const protected_session = require('../../services/protected_session'); +const repository = require('../../services/repository'); async function getNoteRevisions(req) { const noteId = req.params.noteId; - const revisions = await sql.getRows("SELECT * FROM note_revisions WHERE noteId = ? order by dateModifiedTo desc", [noteId]); - protected_session.decryptNoteRevisions(revisions); - - return revisions; + return await repository.getEntities("SELECT * FROM note_revisions WHERE noteId = ? order by dateModifiedTo desc", [noteId]); } module.exports = { diff --git a/src/routes/api/tree.js b/src/routes/api/tree.js index 1041b6ad0..656d889bf 100644 --- a/src/routes/api/tree.js +++ b/src/routes/api/tree.js @@ -5,9 +5,9 @@ const options = require('../../services/options'); const utils = require('../../services/utils'); const config = require('../../services/config'); const protected_session = require('../../services/protected_session'); -const sync_table = require('../../services/sync_table'); +const repository = require('../../services/repository'); -async function getTree(req) { +async function getTree() { const branches = await sql.getRows(` SELECT branchId, @@ -64,13 +64,9 @@ async function setPrefix(req) { const branchId = req.params.branchId; const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix; - await sql.doInTransaction(async () => { - await sql.execute("UPDATE branches SET prefix = ?, dateModified = ? WHERE branchId = ?", [prefix, utils.nowDate(), branchId]); - - await sync_table.addBranchSync(branchId); - }); - - return {}; + const branch = await repository.getBranch(branchId); + branch.prefix = prefix; + await branch.save(); } module.exports = { diff --git a/src/routes/routes.js b/src/routes/routes.js index 00793b8ea..441089f76 100644 --- a/src/routes/routes.js +++ b/src/routes/routes.js @@ -52,7 +52,7 @@ function apiResultHandler(res, result) { } } else if (result === undefined) { - res.status(200).send(); + res.status(204).send(); } else { res.status(200).send(result); diff --git a/src/services/notes.js b/src/services/notes.js index 831e0a208..b78a8d523 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -58,7 +58,7 @@ async function createNewNote(parentNoteId, noteOpts) { mime: noteOpts.mime ? noteOpts.mime : 'text/html' }); - await repository.updateEntity(note); + await note.save(); const branch = new Branch({ branchId: utils.newBranchId(), @@ -69,12 +69,13 @@ async function createNewNote(parentNoteId, noteOpts) { isDeleted: 0 }); - await repository.updateEntity(branch); + await branch.save(); return { noteId: note.noteId, + note, branchId: branch.branchId, - note + branch }; } diff --git a/src/services/protected_session.js b/src/services/protected_session.js index 069ad51df..c0fff7f0b 100644 --- a/src/services/protected_session.js +++ b/src/services/protected_session.js @@ -27,10 +27,6 @@ function getDataKey() { return dataKeyMap[protectedSessionId]; } -function getDataKeyForProtectedSessionId(protectedSessionId) { - return dataKeyMap[protectedSessionId]; -} - function isProtectedSessionAvailable(req) { const protectedSessionId = getProtectedSessionId(req); @@ -84,12 +80,6 @@ function decryptNoteRevision(hist) { } } -function decryptNoteRevisions(noteRevisions) { - for (const revision of noteRevisions) { - decryptNoteRevision(revision); - } -} - function encryptNote(note) { const dataKey = getDataKey(); @@ -107,12 +97,10 @@ function encryptNoteRevision(revision) { module.exports = { setDataKey, getDataKey, - getDataKeyForProtectedSessionId, isProtectedSessionAvailable, decryptNote, decryptNotes, decryptNoteRevision, - decryptNoteRevisions, encryptNote, encryptNoteRevision, setProtectedSessionId diff --git a/src/services/repository.js b/src/services/repository.js index e337df724..810ba5841 100644 --- a/src/services/repository.js +++ b/src/services/repository.js @@ -33,6 +33,10 @@ async function getBranch(branchId) { return await getEntity("SELECT * FROM branches WHERE branchId = ?", [branchId]); } +async function getImage(imageId) { + return await getEntity("SELECT * FROM images WHERE imageId = ?", [imageId]); +} + async function updateEntity(entity) { if (entity.beforeSaving) { entity.beforeSaving(); @@ -54,6 +58,7 @@ module.exports = { getEntity, getNote, getBranch, + getImage, updateEntity, setEntityConstructor }; \ No newline at end of file