moving notes across parents now deletes the old branch and creates new which makes detecting changes much easier

This commit is contained in:
zadam 2020-01-28 22:15:33 +01:00
parent 368d3b1b97
commit d2e3aedf7f
2 changed files with 50 additions and 15 deletions

View File

@ -67,8 +67,22 @@ class Branch extends Entity {
// cannot be static! // cannot be static!
updatePojo(pojo) { updatePojo(pojo) {
// FIXME remove
delete pojo.origParentNoteId; delete pojo.origParentNoteId;
} }
getClone(parentNoteId, notePosition) {
return new Branch({
noteId: this.noteId,
parentNoteId: parentNoteId,
notePosition: notePosition,
prefix: this.prefix,
isExpanded: this.isExpanded,
isDeleted: false,
utcDateCreated: this.utcDateCreated,
utcDateModified: this.utcDateModified
});
}
} }
module.exports = Branch; module.exports = Branch;

View File

@ -2,7 +2,7 @@
const sql = require('../../services/sql'); const sql = require('../../services/sql');
const utils = require('../../services/utils'); const utils = require('../../services/utils');
const sync_table = require('../../services/sync_table'); const syncTableService = require('../../services/sync_table');
const tree = require('../../services/tree'); const tree = require('../../services/tree');
const notes = require('../../services/notes'); const notes = require('../../services/notes');
const repository = require('../../services/repository'); const repository = require('../../services/repository');
@ -18,6 +18,10 @@ async function moveBranchToParent(req) {
const noteToMove = await tree.getBranch(branchId); const noteToMove = await tree.getBranch(branchId);
if (noteToMove.parentNoteId === parentNoteId) {
return { success: true }; // no-op
}
const validationResult = await tree.validateParentChild(parentNoteId, noteToMove.noteId, branchId); const validationResult = await tree.validateParentChild(parentNoteId, noteToMove.noteId, branchId);
if (!validationResult.success) { if (!validationResult.success) {
@ -27,10 +31,11 @@ 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 + 10; const newNotePos = maxNotePos === null ? 0 : maxNotePos + 10;
const branch = await repository.getBranch(branchId); const newBranch = noteToMove.getClone(parentNoteId, newNotePos);
branch.parentNoteId = parentNoteId; await newBranch.save();
branch.notePosition = newNotePos;
await branch.save(); noteToMove.isDeleted = true;
await noteToMove.save();
return { success: true }; return { success: true };
} }
@ -52,12 +57,19 @@ async function moveBranchBeforeNote(req) {
await sql.execute("UPDATE branches SET notePosition = notePosition + 10 WHERE parentNoteId = ? AND notePosition >= ? AND isDeleted = 0", await sql.execute("UPDATE branches SET notePosition = notePosition + 10 WHERE parentNoteId = ? AND notePosition >= ? AND isDeleted = 0",
[beforeNote.parentNoteId, beforeNote.notePosition]); [beforeNote.parentNoteId, beforeNote.notePosition]);
await sync_table.addNoteReorderingSync(beforeNote.parentNoteId); await syncTableService.addNoteReorderingSync(beforeNote.parentNoteId);
const branch = await repository.getBranch(branchId); if (noteToMove.parentNoteId === beforeNote.parentNoteId) {
branch.parentNoteId = beforeNote.parentNoteId; noteToMove.notePosition = beforeNote.notePosition;
branch.notePosition = beforeNote.notePosition; await noteToMove.save();
await branch.save(); }
else {
const newBranch = noteToMove.getClone(beforeNote.parentNoteId, beforeNote.notePosition);
await newBranch.save();
noteToMove.isDeleted = true;
await noteToMove.save();
}
return { success: true }; return { success: true };
} }
@ -79,12 +91,21 @@ async function moveBranchAfterNote(req) {
await sql.execute("UPDATE branches SET notePosition = notePosition + 10 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0", await sql.execute("UPDATE branches SET notePosition = notePosition + 10 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
[afterNote.parentNoteId, afterNote.notePosition]); [afterNote.parentNoteId, afterNote.notePosition]);
await sync_table.addNoteReorderingSync(afterNote.parentNoteId); await syncTableService.addNoteReorderingSync(afterNote.parentNoteId);
const branch = await repository.getBranch(branchId); const movedNotePosition = afterNote.notePosition + 10;
branch.parentNoteId = afterNote.parentNoteId;
branch.notePosition = afterNote.notePosition + 10; if (noteToMove.parentNoteId === afterNote.parentNoteId) {
await branch.save(); noteToMove.notePosition = movedNotePosition;
await noteToMove.save();
}
else {
const newBranch = noteToMove.getClone(afterNote.parentNoteId, movedNotePosition);
await newBranch.save();
noteToMove.isDeleted = true;
await noteToMove.save();
}
return { success: true }; return { success: true };
} }