refactoring of note creation APIs WIP

This commit is contained in:
zadam 2019-11-16 12:28:47 +01:00
parent 13c0411533
commit 60231de0ed
3 changed files with 28 additions and 5 deletions

View File

@ -649,11 +649,9 @@ async function createNote(node, parentNoteId, target, extraOptions = {}) {
const newNoteName = extraOptions.title || "new note";
const {note, branch} = await server.post('notes/' + parentNoteId + '/children', {
const {note, branch} = await server.post(`notes/${parentNoteId}/children?target=${target}&targetBranchId=${node.data.branchId}`, {
title: newNoteName,
content: extraOptions.content,
target: target,
target_branchId: node.data.branchId,
isProtected: extraOptions.isProtected,
type: extraOptions.type
});

View File

@ -56,7 +56,9 @@ async function createNote(req) {
const params = Object.assign({}, req.body); // clone
params.parentNoteId = req.params.parentNoteId;
const { note, branch } = await noteService.createNewNote(params);
const { target, targetBranchId } = req.query;
const { note, branch } = await noteService.createNewNoteWithTarget(target, targetBranchId, params);
note.cssClass = (await note.getLabels("cssClass")).map(label => label.value).join(" ");

View File

@ -76,7 +76,7 @@ async function copyChildAttributes(parentNote, childNote) {
* - {string} parentNoteId
* - {string} title
* - {*} content
* - {string} type
* - {string} type - text, code, file, image, search, book, relation-map
*
* Following are optional (have defaults)
* - {string} mime - value is derived from default mimes for type
@ -128,6 +128,28 @@ async function createNewNote(params) {
};
}
async function createNewNoteWithTarget(target, targetBranchId, params) {
if (target === 'into') {
return await createNewNote(params);
}
else if (target === 'after') {
const afterNote = await sql.getRow('SELECT notePosition FROM branches WHERE branchId = ?', [noteData.target_branchId]);
// not updating utcDateModified to avoig having to sync whole rows
await sql.execute('UPDATE branches SET notePosition = notePosition + 10 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0',
[params.parentNoteId, afterNote.notePosition]);
params.notePosition = afterNote.notePosition + 10;
await createNewNote(params);
await syncTableService.addNoteReorderingSync(params.parentNoteId);
}
else {
throw new Error(`Unknown target ${target}`);
}
}
// methods below should be probably just backend API methods
async function createJsonNote(parentNoteId, title, content = {}, params = {}) {
params.parentNoteId = parentNoteId;
@ -512,6 +534,7 @@ sqlInit.dbReady.then(() => {
module.exports = {
createNewNote,
createNewNoteWithTarget,
updateNote,
deleteBranch,
protectNoteRecursively,