initial work on new router model

This commit is contained in:
azivner
2018-03-30 12:57:22 -04:00
parent 0ec909fd7a
commit efffc29649
5 changed files with 161 additions and 136 deletions

View File

@@ -15,15 +15,17 @@ const wrap = require('express-promise-wrap').wrap;
* for not deleted note trees. There may be multiple deleted note-parent note relationships.
*/
router.put('/:branchId/move-to/:parentNoteId', auth.checkApiAuth, wrap(async (req, res, next) => {
async function moveBranchToParent(req) {
const branchId = req.params.branchId;
const parentNoteId = req.params.parentNoteId;
const sourceId = req.headers.source_id;
const noteToMove = await tree.getBranch(branchId);
if (!await tree.validateParentChild(res, parentNoteId, noteToMove.noteId, branchId)) {
return;
const validationResult = await tree.validateParentChild(parentNoteId, noteToMove.noteId, branchId);
if (!validationResult.success) {
return [400, validationResult];
}
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
@@ -31,17 +33,15 @@ router.put('/:branchId/move-to/:parentNoteId', auth.checkApiAuth, wrap(async (re
const now = utils.nowDate();
await sql.doInTransaction(async () => {
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?",
[parentNoteId, newNotePos, now, branchId]);
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?",
[parentNoteId, newNotePos, now, branchId]);
await sync_table.addBranchSync(branchId, sourceId);
});
await sync_table.addBranchSync(branchId, sourceId);
res.send({ success: true });
}));
return { success: true };
}
router.put('/:branchId/move-before/:beforeBranchId', auth.checkApiAuth, wrap(async (req, res, next) => {
async function moveBranchBeforeNote(req) {
const branchId = req.params.branchId;
const beforeBranchId = req.params.beforeBranchId;
const sourceId = req.headers.source_id;
@@ -49,28 +49,28 @@ router.put('/:branchId/move-before/:beforeBranchId', auth.checkApiAuth, wrap(asy
const noteToMove = await tree.getBranch(branchId);
const beforeNote = await tree.getBranch(beforeBranchId);
if (!await tree.validateParentChild(res, beforeNote.parentNoteId, noteToMove.noteId, branchId)) {
return;
const validationResult = await tree.validateParentChild(beforeNote.parentNoteId, noteToMove.noteId, branchId);
if (!validationResult.success) {
return [400, validationResult];
}
await sql.doInTransaction(async () => {
// we don't change dateModified so other changes are prioritized in case of conflict
// also we would have to sync all those modified note trees otherwise hash checks would fail
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition >= ? AND isDeleted = 0",
[beforeNote.parentNoteId, beforeNote.notePosition]);
// we don't change dateModified so other changes are prioritized in case of conflict
// also we would have to sync all those modified note trees otherwise hash checks would fail
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition >= ? AND isDeleted = 0",
[beforeNote.parentNoteId, beforeNote.notePosition]);
await sync_table.addNoteReorderingSync(beforeNote.parentNoteId, sourceId);
await sync_table.addNoteReorderingSync(beforeNote.parentNoteId, sourceId);
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?",
[beforeNote.parentNoteId, beforeNote.notePosition, utils.nowDate(), branchId]);
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?",
[beforeNote.parentNoteId, beforeNote.notePosition, utils.nowDate(), branchId]);
await sync_table.addBranchSync(branchId, sourceId);
});
await sync_table.addBranchSync(branchId, sourceId);
res.send({ success: true });
}));
return { success: true };
}
router.put('/:branchId/move-after/:afterBranchId', auth.checkApiAuth, wrap(async (req, res, next) => {
async function moveBranchAfterNote(req) {
const branchId = req.params.branchId;
const afterBranchId = req.params.afterBranchId;
const sourceId = req.headers.source_id;
@@ -78,46 +78,43 @@ router.put('/:branchId/move-after/:afterBranchId', auth.checkApiAuth, wrap(async
const noteToMove = await tree.getBranch(branchId);
const afterNote = await tree.getBranch(afterBranchId);
if (!await tree.validateParentChild(res, afterNote.parentNoteId, noteToMove.noteId, branchId)) {
return;
const validationResult = await tree.validateParentChild(afterNote.parentNoteId, noteToMove.noteId, branchId);
if (!validationResult.success) {
return [400, validationResult];
}
await sql.doInTransaction(async () => {
// we don't change dateModified so other changes are prioritized in case of conflict
// also we would have to sync all those modified note trees otherwise hash checks would fail
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
[afterNote.parentNoteId, afterNote.notePosition]);
// we don't change dateModified so other changes are prioritized in case of conflict
// also we would have to sync all those modified note trees otherwise hash checks would fail
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
[afterNote.parentNoteId, afterNote.notePosition]);
await sync_table.addNoteReorderingSync(afterNote.parentNoteId, sourceId);
await sync_table.addNoteReorderingSync(afterNote.parentNoteId, sourceId);
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?",
[afterNote.parentNoteId, afterNote.notePosition + 1, utils.nowDate(), branchId]);
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?",
[afterNote.parentNoteId, afterNote.notePosition + 1, utils.nowDate(), branchId]);
await sync_table.addBranchSync(branchId, sourceId);
});
await sync_table.addBranchSync(branchId, sourceId);
res.send({ success: true });
}));
return { success: true };
}
router.put('/:branchId/expanded/:expanded', auth.checkApiAuth, wrap(async (req, res, next) => {
async function setExpanded(req) {
const branchId = req.params.branchId;
const expanded = req.params.expanded;
await sql.doInTransaction(async () => {
await sql.execute("UPDATE branches SET isExpanded = ? WHERE branchId = ?", [expanded, branchId]);
await sql.execute("UPDATE branches SET isExpanded = ? WHERE branchId = ?", [expanded, branchId]);
// we don't sync expanded label
}
// we don't sync expanded label
});
async function deleteBranch(req) {
await notes.deleteNote(req.params.branchId, req.headers.source_id);
}
res.send({});
}));
router.delete('/:branchId', auth.checkApiAuth, wrap(async (req, res, next) => {
await sql.doInTransaction(async () => {
await notes.deleteNote(req.params.branchId, req.headers.source_id);
});
res.send({});
}));
module.exports = router;
module.exports = {
moveBranchToParent,
moveBranchBeforeNote,
moveBranchAfterNote,
setExpanded,
deleteBranch
};