From e466c393eb80d7d9052dd9e70446bee8c6f75114 Mon Sep 17 00:00:00 2001 From: zadam Date: Mon, 26 Apr 2021 22:18:14 +0200 Subject: [PATCH] converted NoteRevision entity to the becca --- src/routes/api/note_revisions.js | 9 +++++---- src/routes/api/tree.js | 4 ++-- src/services/becca/becca.js | 23 ++++++++++++++++++++++- src/services/becca/becca_service.js | 4 ++-- src/services/becca/entities/note.js | 2 +- 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/routes/api/note_revisions.js b/src/routes/api/note_revisions.js index 88fd5d590..00ed71709 100644 --- a/src/routes/api/note_revisions.js +++ b/src/routes/api/note_revisions.js @@ -7,6 +7,7 @@ const noteRevisionService = require('../../services/note_revisions'); const utils = require('../../services/utils'); const sql = require('../../services/sql'); const path = require('path'); +const becca = require("../../services/becca/becca.js"); function getNoteRevisions(req) { return repository.getEntities(` @@ -19,11 +20,11 @@ function getNoteRevisions(req) { } function getNoteRevision(req) { - const noteRevision = repository.getNoteRevision(req.params.noteRevisionId); + const noteRevision = becca.getNoteRevision(req.params.noteRevisionId); if (noteRevision.type === 'file') { if (noteRevision.isStringNote()) { - noteRevision.content = (noteRevision.getContent()).substr(0, 10000); + noteRevision.content = noteRevision.getContent().substr(0, 10000); } } else { @@ -62,7 +63,7 @@ function getRevisionFilename(noteRevision) { } function downloadNoteRevision(req, res) { - const noteRevision = repository.getNoteRevision(req.params.noteRevisionId); + const noteRevision = becca.getNoteRevision(req.params.noteRevisionId); if (noteRevision.noteId !== req.params.noteId) { return res.status(400).send(`Note revision ${req.params.noteRevisionId} does not belong to note ${req.params.noteId}`); @@ -92,7 +93,7 @@ function eraseNoteRevision(req) { } function restoreNoteRevision(req) { - const noteRevision = repository.getNoteRevision(req.params.noteRevisionId); + const noteRevision = becca.getNoteRevision(req.params.noteRevisionId); if (noteRevision) { const note = noteRevision.getNote(); diff --git a/src/routes/api/tree.js b/src/routes/api/tree.js index 371faae18..49bb8bf37 100644 --- a/src/routes/api/tree.js +++ b/src/routes/api/tree.js @@ -23,7 +23,7 @@ function getNotesAndBranchesAndAttributes(noteIds) { } for (const childNote of note.children) { - const childBranch = becca.getBranch(childNote.noteId, note.noteId); + const childBranch = becca.getBranchFromChildAndParent(childNote.noteId, note.noteId); collectedBranchIds.add(childBranch.branchId); } @@ -127,7 +127,7 @@ function getTree(req) { for (const childNote of parentNote.children) { collectedNoteIds.add(childNote.noteId); - const childBranch = becca.getBranch(childNote.noteId, parentNote.noteId); + const childBranch = becca.getBranchFromChildAndParent(childNote.noteId, parentNote.noteId); if (childBranch.isExpanded) { collect(childBranch.childNote); diff --git a/src/services/becca/becca.js b/src/services/becca/becca.js index 27662ed95..8118a7252 100644 --- a/src/services/becca/becca.js +++ b/src/services/becca/becca.js @@ -1,5 +1,8 @@ "use strict"; +const sql = require("../sql.js"); +const NoteRevision = require("./entities/note_revision.js"); + class Becca { constructor() { this.reset(); @@ -45,9 +48,27 @@ class Becca { } } - getBranch(childNoteId, parentNoteId) { + getNote(noteId) { + return this.notes[noteId]; + } + + getBranch(branchId) { + return this.branches[branchId]; + } + + getAttribute(attributeId) { + return this.attributes[attributeId]; + } + + getBranchFromChildAndParent(childNoteId, parentNoteId) { return this.childParentToBranch[`${childNoteId}-${parentNoteId}`]; } + + getNoteRevision(noteRevisionId) { + const row = sql.getRow("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [noteRevisionId]); + + return row ? new NoteRevision(row) : null; + } } const becca = new Becca(); diff --git a/src/services/becca/becca_service.js b/src/services/becca/becca_service.js index 2a36fb672..65d29af09 100644 --- a/src/services/becca/becca_service.js +++ b/src/services/becca/becca_service.js @@ -80,7 +80,7 @@ function getNoteTitle(childNoteId, parentNoteId) { title = childNote.title; } - const branch = parentNote ? becca.getBranch(childNote.noteId, parentNote.noteId) : null; + const branch = parentNote ? becca.getBranchFromChildAndParent(childNote.noteId, parentNote.noteId) : null; return ((branch && branch.prefix) ? `${branch.prefix} - ` : '') + title; } @@ -175,7 +175,7 @@ function getNotePath(noteId) { return { noteId: noteId, - branchId: becca.getBranch(noteId, parentNote.noteId).branchId, + branchId: becca.getBranchFromChildAndParent(noteId, parentNote.noteId).branchId, title: noteTitle, notePath: retPath, path: retPath.join('/') diff --git a/src/services/becca/entities/note.js b/src/services/becca/entities/note.js index dc251dd72..ff5b47558 100644 --- a/src/services/becca/entities/note.js +++ b/src/services/becca/entities/note.js @@ -107,7 +107,7 @@ class Note extends AbstractEntity { } getChildBranches() { - return this.children.map(childNote => this.becca.getBranch(childNote.noteId, this.noteId)); + return this.children.map(childNote => this.becca.getBranchFromChildAndParent(childNote.noteId, this.noteId)); } /*