From 4f200c73dce6403c625f06ed860fe5d0262853fd Mon Sep 17 00:00:00 2001 From: azivner Date: Sat, 31 Mar 2018 22:23:40 -0400 Subject: [PATCH] refactoring of note creation --- src/public/javascripts/services/tree.js | 1 + src/services/notes.js | 48 +++++++++++-------------- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js index 0c961951f..7e3a4e15c 100644 --- a/src/public/javascripts/services/tree.js +++ b/src/public/javascripts/services/tree.js @@ -14,6 +14,7 @@ import infoService from "./info.js"; import treeBuilder from "./tree_builder.js"; import treeKeyBindings from "./tree_keybindings.js"; import Branch from '../entities/branch.js'; +import NoteShort from '../entities/note_short.js'; const $tree = $("#tree"); const $parentList = $("#parent-list"); diff --git a/src/services/notes.js b/src/services/notes.js index 2c8b05a05..eae565286 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -3,15 +3,13 @@ const options = require('./options'); const utils = require('./utils'); const sync_table = require('./sync_table'); const labels = require('./labels'); -const protected_session = require('./protected_session'); const repository = require('./repository'); +const Note = require('../entities/note'); const NoteImage = require('../entities/note_image'); const NoteRevision = require('../entities/note_revision'); +const Branch = require('../entities/branch'); -async function createNewNote(parentNoteId, noteOpts) { - const noteId = utils.newNoteId(); - const branchId = utils.newBranchId(); - +async function getNewNotePosition(parentNoteId, noteOpts) { let newNotePos = 0; if (noteOpts.target === 'into') { @@ -33,9 +31,14 @@ async function createNewNote(parentNoteId, noteOpts) { else { throw new Error('Unknown target: ' + noteOpts.target); } + return newNotePos; +} + +async function createNewNote(parentNoteId, noteOpts) { + const newNotePos = await getNewNotePosition(parentNoteId, noteOpts); if (parentNoteId !== 'root') { - const parent = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]); + const parent = await repository.getNote(parentNoteId); if (!noteOpts.type) { noteOpts.type = parent.type; @@ -46,42 +49,31 @@ async function createNewNote(parentNoteId, noteOpts) { } } - const now = utils.nowDate(); - - const note = { - noteId: noteId, + const note = new Note({ + noteId: utils.newNoteId(), title: noteOpts.title, content: noteOpts.content ? noteOpts.content : '', isProtected: noteOpts.isProtected, type: noteOpts.type ? noteOpts.type : 'text', - mime: noteOpts.mime ? noteOpts.mime : 'text/html', - dateCreated: now, - dateModified: now - }; + mime: noteOpts.mime ? noteOpts.mime : 'text/html' + }); - if (note.isProtected) { - protected_session.encryptNote(note); - } + await repository.updateEntity(note); - await sql.insert("notes", note); - - await sync_table.addNoteSync(noteId); - - await sql.insert("branches", { - branchId: branchId, - noteId: noteId, + const branch = new Branch({ + branchId: utils.newBranchId(), + noteId: note.noteId, parentNoteId: parentNoteId, notePosition: newNotePos, isExpanded: 0, - dateModified: now, isDeleted: 0 }); - await sync_table.addBranchSync(branchId); + await repository.updateEntity(branch); return { - noteId, - branchId, + noteId: note.noteId, + branchId: branch.branchId, note }; }