2018-01-14 07:02:41 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const sql = require('../../services/sql');
|
|
|
|
const auth = require('../../services/auth');
|
|
|
|
const utils = require('../../services/utils');
|
|
|
|
const sync_table = require('../../services/sync_table');
|
|
|
|
const wrap = require('express-promise-wrap').wrap;
|
|
|
|
const tree = require('../../services/tree');
|
|
|
|
|
|
|
|
router.put('/:childNoteId/clone-to/:parentNoteId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
|
|
|
const parentNoteId = req.params.parentNoteId;
|
|
|
|
const childNoteId = req.params.childNoteId;
|
|
|
|
const prefix = req.body.prefix;
|
2018-01-29 10:57:46 +08:00
|
|
|
const sourceId = req.headers.source_id;
|
2018-01-14 07:02:41 +08:00
|
|
|
|
|
|
|
if (!await tree.validateParentChild(res, parentNoteId, childNoteId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-30 06:41:59 +08:00
|
|
|
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
|
2018-01-14 07:02:41 +08:00
|
|
|
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
|
|
|
|
|
|
|
|
await sql.doInTransaction(async () => {
|
|
|
|
const noteTree = {
|
2018-01-29 08:30:14 +08:00
|
|
|
noteTreeId: utils.newNoteTreeId(),
|
|
|
|
noteId: childNoteId,
|
|
|
|
parentNoteId: parentNoteId,
|
2018-01-14 07:02:41 +08:00
|
|
|
prefix: prefix,
|
2018-01-29 08:30:14 +08:00
|
|
|
notePosition: newNotePos,
|
|
|
|
isExpanded: 0,
|
|
|
|
dateModified: utils.nowDate(),
|
|
|
|
isDeleted: 0
|
2018-01-14 07:02:41 +08:00
|
|
|
};
|
|
|
|
|
2018-01-29 08:38:05 +08:00
|
|
|
await sql.replace("note_tree", noteTree);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await sync_table.addNoteTreeSync(noteTree.noteTreeId, sourceId);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-01-29 08:38:05 +08:00
|
|
|
await sql.execute("UPDATE note_tree SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]);
|
2018-01-14 07:02:41 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
res.send({ success: true });
|
|
|
|
}));
|
|
|
|
|
|
|
|
router.put('/:noteId/clone-after/:afterNoteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
|
|
|
const noteId = req.params.noteId;
|
|
|
|
const afterNoteTreeId = req.params.afterNoteTreeId;
|
2018-01-29 10:57:46 +08:00
|
|
|
const sourceId = req.headers.source_id;
|
2018-01-14 07:02:41 +08:00
|
|
|
|
|
|
|
const afterNote = await tree.getNoteTree(afterNoteTreeId);
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
if (!await tree.validateParentChild(res, afterNote.parentNoteId, noteId)) {
|
2018-01-14 07:02:41 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await sql.doInTransaction(async () => {
|
2018-01-29 08:30:14 +08:00
|
|
|
// we don't change dateModified so other changes are prioritized in case of conflict
|
2018-01-14 07:02:41 +08:00
|
|
|
// also we would have to sync all those modified note trees otherwise hash checks would fail
|
2018-01-29 08:38:05 +08:00
|
|
|
await sql.execute("UPDATE note_tree SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
|
2018-01-29 08:30:14 +08:00
|
|
|
[afterNote.parentNoteId, afterNote.notePosition]);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await sync_table.addNoteReorderingSync(afterNote.parentNoteId, sourceId);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
|
|
|
const noteTree = {
|
2018-01-29 08:30:14 +08:00
|
|
|
noteTreeId: utils.newNoteTreeId(),
|
|
|
|
noteId: noteId,
|
|
|
|
parentNoteId: afterNote.parentNoteId,
|
|
|
|
notePosition: afterNote.notePosition + 1,
|
|
|
|
isExpanded: 0,
|
|
|
|
dateModified: utils.nowDate(),
|
|
|
|
isDeleted: 0
|
2018-01-14 07:02:41 +08:00
|
|
|
};
|
|
|
|
|
2018-01-29 08:38:05 +08:00
|
|
|
await sql.replace("note_tree", noteTree);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await sync_table.addNoteTreeSync(noteTree.noteTreeId, sourceId);
|
2018-01-14 07:02:41 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
res.send({ success: true });
|
|
|
|
}));
|
|
|
|
|
|
|
|
module.exports = router;
|