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 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");

View file

@ -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
};
}