note content refactoring, WIP

This commit is contained in:
zadam
2019-02-08 21:01:26 +01:00
parent 6952b643ae
commit 4a093000be
7 changed files with 135 additions and 151 deletions

View File

@@ -2,6 +2,7 @@
const Entity = require('./entity');
const Attribute = require('./attribute');
const NoteContent = require('./note_content');
const protectedSessionService = require('../services/protected_session');
const repository = require('../services/repository');
const sql = require('../services/sql');
@@ -62,6 +63,10 @@ class Note extends Entity {
async getNoteContent() {
if (!this.noteContent) {
this.noteContent = await repository.getEntity(`SELECT * FROM note_contents WHERE noteId = ?`, [this.noteId]);
if (!this.noteContent) {
throw new Error("Note content not found for noteId=" + this.noteId);
}
}
return this.noteContent;
@@ -81,6 +86,23 @@ class Note extends Entity {
return JSON.parse(content);
}
/** @returns {Promise} */
async setContent(content) {
if (!this.noteContent) {
// make sure it is loaded
await this.getNoteContent();
}
this.noteContent.content = content;
await this.noteContent.save();
}
/** @returns {Promise} */
async setJsonContent(content) {
await this.setContent(JSON.stringify(content));
}
/** @returns {boolean} true if this note is the root of the note tree. Root note has "root" noteId */
isRoot() {
return this.noteId === 'root';
@@ -620,9 +642,6 @@ class Note extends Entity {
}
beforeSaving() {
// we do this here because encryption needs the note ID for the IV
this.generateIdIfNecessary();
if (!this.isDeleted) {
this.isDeleted = false;
}