refactoring of note creation

This commit is contained in:
azivner 2018-03-31 22:23:40 -04:00
parent 5f7e74e15c
commit 4f200c73dc
2 changed files with 21 additions and 28 deletions

View File

@ -14,6 +14,7 @@ import infoService from "./info.js";
import treeBuilder from "./tree_builder.js"; import treeBuilder from "./tree_builder.js";
import treeKeyBindings from "./tree_keybindings.js"; import treeKeyBindings from "./tree_keybindings.js";
import Branch from '../entities/branch.js'; import Branch from '../entities/branch.js';
import NoteShort from '../entities/note_short.js';
const $tree = $("#tree"); const $tree = $("#tree");
const $parentList = $("#parent-list"); const $parentList = $("#parent-list");

View File

@ -3,15 +3,13 @@ const options = require('./options');
const utils = require('./utils'); const utils = require('./utils');
const sync_table = require('./sync_table'); const sync_table = require('./sync_table');
const labels = require('./labels'); const labels = require('./labels');
const protected_session = require('./protected_session');
const repository = require('./repository'); const repository = require('./repository');
const Note = require('../entities/note');
const NoteImage = require('../entities/note_image'); const NoteImage = require('../entities/note_image');
const NoteRevision = require('../entities/note_revision'); const NoteRevision = require('../entities/note_revision');
const Branch = require('../entities/branch');
async function createNewNote(parentNoteId, noteOpts) { async function getNewNotePosition(parentNoteId, noteOpts) {
const noteId = utils.newNoteId();
const branchId = utils.newBranchId();
let newNotePos = 0; let newNotePos = 0;
if (noteOpts.target === 'into') { if (noteOpts.target === 'into') {
@ -33,9 +31,14 @@ async function createNewNote(parentNoteId, noteOpts) {
else { else {
throw new Error('Unknown target: ' + noteOpts.target); throw new Error('Unknown target: ' + noteOpts.target);
} }
return newNotePos;
}
async function createNewNote(parentNoteId, noteOpts) {
const newNotePos = await getNewNotePosition(parentNoteId, noteOpts);
if (parentNoteId !== 'root') { if (parentNoteId !== 'root') {
const parent = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]); const parent = await repository.getNote(parentNoteId);
if (!noteOpts.type) { if (!noteOpts.type) {
noteOpts.type = parent.type; noteOpts.type = parent.type;
@ -46,42 +49,31 @@ async function createNewNote(parentNoteId, noteOpts) {
} }
} }
const now = utils.nowDate(); const note = new Note({
noteId: utils.newNoteId(),
const note = {
noteId: noteId,
title: noteOpts.title, title: noteOpts.title,
content: noteOpts.content ? noteOpts.content : '', content: noteOpts.content ? noteOpts.content : '',
isProtected: noteOpts.isProtected, isProtected: noteOpts.isProtected,
type: noteOpts.type ? noteOpts.type : 'text', type: noteOpts.type ? noteOpts.type : 'text',
mime: noteOpts.mime ? noteOpts.mime : 'text/html', mime: noteOpts.mime ? noteOpts.mime : 'text/html'
dateCreated: now, });
dateModified: now
};
if (note.isProtected) { await repository.updateEntity(note);
protected_session.encryptNote(note);
}
await sql.insert("notes", note); const branch = new Branch({
branchId: utils.newBranchId(),
await sync_table.addNoteSync(noteId); noteId: note.noteId,
await sql.insert("branches", {
branchId: branchId,
noteId: noteId,
parentNoteId: parentNoteId, parentNoteId: parentNoteId,
notePosition: newNotePos, notePosition: newNotePos,
isExpanded: 0, isExpanded: 0,
dateModified: now,
isDeleted: 0 isDeleted: 0
}); });
await sync_table.addBranchSync(branchId); await repository.updateEntity(branch);
return { return {
noteId, noteId: note.noteId,
branchId, branchId: branch.branchId,
note note
}; };
} }