From c035627f0a0e6cad6424814b3470655b64d7580b Mon Sep 17 00:00:00 2001 From: zadam Date: Sun, 2 May 2021 20:32:50 +0200 Subject: [PATCH] use .markAsDeleted() instead of setting .isDeleted manually --- src/entities/note.js | 3 +-- .../widgets/search_actions/execute_script.js | 2 +- src/routes/api/attributes.js | 13 ++++----- src/routes/api/branches.js | 8 +++--- src/routes/api/search.js | 9 +++---- src/services/becca/entities/attribute.js | 12 +++------ src/services/becca/entities/branch.js | 8 ++++++ src/services/becca/entities/note.js | 4 +-- src/services/consistency_checks.js | 27 +++++++------------ src/services/handlers.js | 3 +-- src/services/notes.js | 16 +++-------- src/services/tree.js | 5 ++-- 12 files changed, 42 insertions(+), 68 deletions(-) diff --git a/src/entities/note.js b/src/entities/note.js index f6a4951c5..f24f67bf5 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -543,8 +543,7 @@ class Note extends Entity { for (const attribute of attributes) { if (attribute.type === type && attribute.name === name && (value === undefined || value === attribute.value)) { - attribute.isDeleted = true; - attribute.save(); + attribute.markAsDeleted(); this.invalidateAttributeCache(); } diff --git a/src/public/app/widgets/search_actions/execute_script.js b/src/public/app/widgets/search_actions/execute_script.js index 16ea0a9b9..e84d3b394 100644 --- a/src/public/app/widgets/search_actions/execute_script.js +++ b/src/public/app/widgets/search_actions/execute_script.js @@ -24,7 +24,7 @@ const TPL = ` More complex example would be deleting all matched note's attributes: -
for (const attr of note.getOwnedAttributes) { attr.isDeleted = true; attr.save(); }
+
for (const attr of note.getOwnedAttributes) { attr.markAsDeleted(); }
diff --git a/src/routes/api/attributes.js b/src/routes/api/attributes.js index 0744af675..144174c51 100644 --- a/src/routes/api/attributes.js +++ b/src/routes/api/attributes.js @@ -36,8 +36,7 @@ function updateNoteAttribute(req) { newAttribute.save(); } - attribute.isDeleted = true; - attribute.save(); + attribute.markAsDeleted(); return { attributeId: newAttribute ? newAttribute.attributeId : null @@ -61,7 +60,7 @@ function updateNoteAttribute(req) { } else { // relations should never have empty target - attribute.isDeleted = true; + attribute.markAsDeleted(); } attribute.save(); @@ -108,7 +107,7 @@ function deleteNoteAttribute(req) { return [400, `Attribute ${attributeId} is not owned by ${noteId}`]; } - attribute.markAttributeAsDeleted(); + attribute.markAsDeleted(); } } @@ -174,8 +173,7 @@ function updateNoteAttributes(req) { // all the remaining existing attributes are not defined anymore and should be deleted for (const toDeleteAttr of existingAttrs) { if (!toDeleteAttr.isAutoLink()) { - toDeleteAttr.isDeleted = true; - toDeleteAttr.save(); + toDeleteAttr.markAsDeleted(); } } } @@ -221,8 +219,7 @@ function deleteRelation(req) { let attribute = repository.getEntity(`SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [sourceNoteId, name, targetNoteId]); if (attribute) { - attribute.isDeleted = true; - attribute.save(); + attribute.markAsDeleted(); } } diff --git a/src/routes/api/branches.js b/src/routes/api/branches.js index e95eb3062..823572246 100644 --- a/src/routes/api/branches.js +++ b/src/routes/api/branches.js @@ -44,7 +44,7 @@ function moveBranchToParent(req) { const newBranch = branchToMove.createClone(parentBranch.noteId, newNotePos); newBranch.save(); - branchToMove.isDeleted = true; + branchToMove.markAsDeleted(); branchToMove.save(); return { success: true }; @@ -85,8 +85,7 @@ function moveBranchBeforeNote(req) { const newBranch = branchToMove.createClone(beforeBranch.parentNoteId, beforeBranch.notePosition); newBranch.save(); - branchToMove.isDeleted = true; - branchToMove.save(); + branchToMove.markAsDeleted(); } return { success: true }; @@ -121,8 +120,7 @@ function moveBranchAfterNote(req) { const newBranch = branchToMove.createClone(afterNote.parentNoteId, movedNotePosition); newBranch.save(); - branchToMove.isDeleted = true; - branchToMove.save(); + branchToMove.markAsDeleted(); } return { success: true }; diff --git a/src/routes/api/search.js b/src/routes/api/search.js index 315985fee..efe06f3f2 100644 --- a/src/routes/api/search.js +++ b/src/routes/api/search.js @@ -59,22 +59,19 @@ async function searchFromNote(req) { const ACTION_HANDLERS = { deleteNote: (action, note) => { - note.isDeleted = true; - note.save(); + note.markAsDeleted(); }, deleteNoteRevisions: (action, note) => { noteRevisionService.eraseNoteRevisions(note.getNoteRevisions().map(rev => rev.noteRevisionId)); }, deleteLabel: (action, note) => { for (const label of note.getOwnedLabels(action.labelName)) { - label.isDeleted = true; - label.save(); + label.markAsDeleted(); } }, deleteRelation: (action, note) => { for (const relation of note.getOwnedRelations(action.relationName)) { - relation.isDeleted = true; - relation.save(); + relation.markAsDeleted(); } }, renameLabel: (action, note) => { diff --git a/src/services/becca/entities/attribute.js b/src/services/becca/entities/attribute.js index ba9be9117..b581235a1 100644 --- a/src/services/becca/entities/attribute.js +++ b/src/services/becca/entities/attribute.js @@ -120,8 +120,7 @@ class Attribute extends AbstractEntity { position: this.position, value: this.value, isInheritable: this.isInheritable, - utcDateModified: dateUtils.utcNowDateTime(), - isDeleted: false + utcDateModified: dateUtils.utcNowDateTime() }; } @@ -143,10 +142,6 @@ class Attribute extends AbstractEntity { this.isInheritable = false; } - if (!this.isDeleted) { - this.isDeleted = false; - } - super.beforeSaving(); } @@ -158,13 +153,12 @@ class Attribute extends AbstractEntity { value: value, position: this.position, isInheritable: isInheritable, - isDeleted: false, utcDateModified: this.utcDateModified }); } - markAttributeAsDeleted() { - sql.execute("UPDATE attributes SET isDeleted = 1 WHERE attributeId = ?", [this.attributeId]); + markAsDeleted(deleteId = null) { + sql.execute("UPDATE attributes SET isDeleted = 1, deleteId = ? WHERE attributeId = ?", [deleteId, this.attributeId]); // FIXME: this needs to be published into entity_changes (for sync and becca cleanup) } diff --git a/src/services/becca/entities/branch.js b/src/services/becca/entities/branch.js index 0bfe2376d..5b1c9b3ea 100644 --- a/src/services/becca/entities/branch.js +++ b/src/services/becca/entities/branch.js @@ -99,6 +99,7 @@ class Branch extends AbstractEntity { } beforeSaving() { + // TODO can be refactored into becca if (this.notePosition === undefined || this.notePosition === null) { const maxNotePos = sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [this.parentNoteId]); this.notePosition = maxNotePos === null ? 0 : maxNotePos + 10; @@ -110,6 +111,13 @@ class Branch extends AbstractEntity { super.beforeSaving(); } + + markAsDeleted(deleteId = null) { + sql.execute("UPDATE branches SET isDeleted = 1, deleteId = ? WHERE branchId = ?", + [deleteId, this.branchId]); + + // FIXME: this needs to be published into entity_changes (for sync and becca cleanup) + } } module.exports = Branch; diff --git a/src/services/becca/entities/note.js b/src/services/becca/entities/note.js index 81ddd201a..565e5c759 100644 --- a/src/services/becca/entities/note.js +++ b/src/services/becca/entities/note.js @@ -864,8 +864,8 @@ class Note extends AbstractEntity { this.utcDateModified = dateUtils.utcNowDateTime(); } - markAsDeleted() { - sql.execute("UPDATE notes SET isDeleted = 1 WHERE noteId = ?", [this.noteId]); + markAsDeleted(deleteId = null) { + sql.execute("UPDATE notes SET isDeleted = 1, deleteId = ? WHERE noteId = ?", [deleteId, this.noteId]); // FIXME: this needs to be published into entity_changes (for sync and becca cleanup) } diff --git a/src/services/consistency_checks.js b/src/services/consistency_checks.js index c55c14c57..3b60f8d9b 100644 --- a/src/services/consistency_checks.js +++ b/src/services/consistency_checks.js @@ -103,8 +103,7 @@ class ConsistencyChecks { ({branchId, noteId}) => { if (this.autoFix) { const branch = becca.getBranch(branchId); - branch.isDeleted = true; - branch.save(); + branch.markAsDeleted(); logFix(`Branch ${branchId} has been deleted since it references missing note ${noteId}`); } else { @@ -140,8 +139,7 @@ class ConsistencyChecks { ({attributeId, noteId}) => { if (this.autoFix) { const attribute = becca.getAttribute(attributeId); - attribute.isDeleted = true; - attribute.save(); + attribute.markAsDeleted(); logFix(`Attribute ${attributeId} has been deleted since it references missing source note ${noteId}`); } else { @@ -159,8 +157,7 @@ class ConsistencyChecks { ({attributeId, noteId}) => { if (this.autoFix) { const attribute = becca.getAttribute(attributeId); - attribute.isDeleted = true; - attribute.save(); + attribute.markAsDeleted(); logFix(`Relation ${attributeId} has been deleted since it references missing note ${noteId}`) } else { @@ -185,8 +182,7 @@ class ConsistencyChecks { ({branchId, noteId}) => { if (this.autoFix) { const branch = becca.getBranch(branchId); - branch.isDeleted = true; - branch.save(); + branch.markAsDeleted(); logFix(`Branch ${branchId} has been deleted since associated note ${noteId} is deleted.`); } else { @@ -204,8 +200,7 @@ class ConsistencyChecks { `, ({branchId, parentNoteId}) => { if (this.autoFix) { const branch = becca.getBranch(branchId); - branch.isDeleted = true; - branch.save(); + branch.markAsDeleted(); logFix(`Branch ${branchId} has been deleted since associated parent note ${parentNoteId} is deleted.`); } else { @@ -258,8 +253,7 @@ class ConsistencyChecks { // delete all but the first branch for (const branch of branches.slice(1)) { - branch.isDeleted = true; - branch.save(); + branch.markAsDeleted(); logFix(`Removing branch ${branch.branchId} since it's parent-child duplicate of branch ${origBranch.branchId}`); } @@ -388,8 +382,7 @@ class ConsistencyChecks { ({attributeId}) => { if (this.autoFix) { const relation = becca.getAttribute(attributeId); - relation.isDeleted = true; - relation.save(); + relation.markAsDeleted(); logFix(`Removed relation ${relation.attributeId} of name "${relation.name} with empty target.`); } else { @@ -426,8 +419,7 @@ class ConsistencyChecks { ({attributeId, noteId}) => { if (this.autoFix) { const attribute = becca.getAttribute(attributeId); - attribute.isDeleted = true; - attribute.save(); + attribute.markAsDeleted(); logFix(`Removed attribute ${attributeId} because owning note ${noteId} is also deleted.`); } else { @@ -446,8 +438,7 @@ class ConsistencyChecks { ({attributeId, targetNoteId}) => { if (this.autoFix) { const attribute = becca.getAttribute(attributeId); - attribute.isDeleted = true; - attribute.save(); + attribute.markAsDeleted(); logFix(`Removed attribute ${attributeId} because target note ${targetNoteId} is also deleted.`); } else { diff --git a/src/services/handlers.js b/src/services/handlers.js index 829d89d8d..e41b1b2a5 100644 --- a/src/services/handlers.js +++ b/src/services/handlers.js @@ -154,8 +154,7 @@ eventService.subscribe(eventService.ENTITY_DELETED, ({ entityName, entity }) => note.invalidateAttributeCache(); targetNote.invalidateAttributeCache(); - relation.isDeleted = true; - relation.save(); + relation.markAsDeleted(); } } }); diff --git a/src/services/notes.js b/src/services/notes.js index a6ee511a9..a9e9789cb 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -532,9 +532,7 @@ function deleteBranch(branch, deleteId, taskContext) { throw new Error("Can't delete root branch/note"); } - branch.isDeleted = true; - branch.deleteId = deleteId; - branch.save(); + branch.markAsDeleted(deleteId); const note = branch.getNote(); const notDeletedBranches = note.getBranches(); @@ -546,22 +544,16 @@ function deleteBranch(branch, deleteId, taskContext) { // first delete children and then parent - this will show up better in recent changes - note.isDeleted = true; - note.deleteId = deleteId; - note.save(); + note.markAsDeleted(deleteId); log.info("Deleting note " + note.noteId); for (const attribute of note.getOwnedAttributes()) { - attribute.isDeleted = true; - attribute.deleteId = deleteId; - attribute.save(); + attribute.markAsDeleted(deleteId); } for (const relation of note.getTargetRelations()) { - relation.isDeleted = true; - relation.deleteId = deleteId; - relation.save(); + relation.markAsDeleted(deleteId); } return true; diff --git a/src/services/tree.js b/src/services/tree.js index a0cecf7f7..a5f1be02b 100644 --- a/src/services/tree.js +++ b/src/services/tree.js @@ -196,14 +196,13 @@ function setNoteToParent(noteId, prefix, parentNoteId) { if (branch) { if (!parentNoteId) { - branch.isDeleted = true; + branch.markAsDeleted(); } else { branch.parentNoteId = parentNoteId; branch.prefix = prefix; + branch.save(); } - - branch.save(); } else if (parentNoteId) { const note = becca.getNote(noteId);