From 15d951b04e6c84a31af792bc3ef90ecc68abeef2 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 1 Apr 2018 12:45:35 -0400 Subject: [PATCH] smaller refactorings continued --- src/entities/label.js | 20 +++++++++++++++++++- src/services/labels.js | 21 +++++++-------------- src/services/repository.js | 10 ++++++---- src/services/script_context.js | 11 ++++++----- src/services/source_id.js | 8 +++----- src/services/sync.js | 8 ++------ 6 files changed, 43 insertions(+), 35 deletions(-) diff --git a/src/entities/label.js b/src/entities/label.js index da6dab27a..5c346c511 100644 --- a/src/entities/label.js +++ b/src/entities/label.js @@ -3,6 +3,7 @@ const Entity = require('./entity'); const repository = require('../services/repository'); const utils = require('../services/utils'); +const sql = require('../services/sql'); class Label extends Entity { static get tableName() { return "labels"; } @@ -12,7 +13,24 @@ class Label extends Entity { return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]); } - beforeSaving() { + async beforeSaving() { + if (!this.labelId) { + this.labelId = utils.newLabelId(); + } + + if (this.value) { + // null value isn't allowed + this.value = ""; + } + + if (this.position === undefined) { + this.position = 1 + await sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM labels WHERE noteId = ?`, [noteId]); + } + + if (!this.isDeleted) { + this.isDeleted = false; + } + if (!this.dateCreated) { this.dateCreated = utils.nowDate(); } diff --git a/src/services/labels.js b/src/services/labels.js index 661294859..407e94e65 100644 --- a/src/services/labels.js +++ b/src/services/labels.js @@ -2,7 +2,6 @@ const sql = require('./sql'); const utils = require('./utils'); -const sync_table = require('./sync_table'); const repository = require('./repository'); const Label = require('../entities/label'); @@ -58,21 +57,15 @@ async function getNoteIdsWithLabel(name) { } async function createLabel(noteId, name, value = "") { - if (value === null || value === undefined) { - value = ""; - } - - const labelId = utils.newLabelId(); - const position = 1 + await sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM labels WHERE noteId = ?`, [noteId]); - - await (new Label({ - labelId: labelId, + const label = new Label({ noteId: noteId, name: name, - value: value, - position: position, - isDeleted: false - })).save(); + value: value + }); + + await label.save(); + + return label; } module.exports = { diff --git a/src/services/repository.js b/src/services/repository.js index e20b05979..504e6c648 100644 --- a/src/services/repository.js +++ b/src/services/repository.js @@ -43,18 +43,20 @@ async function getLabel(labelId) { async function updateEntity(entity) { if (entity.beforeSaving) { - entity.beforeSaving(); + await entity.beforeSaving(); } const clone = Object.assign({}, entity); delete clone.jsonContent; - await sql.replace(entity.constructor.tableName, clone); + await sql.doInTransaction(async () => { + await sql.replace(entity.constructor.tableName, clone); - const primaryKey = entity[entity.constructor.primaryKeyName]; + const primaryKey = entity[entity.constructor.primaryKeyName]; - await sync_table.addEntitySync(entity.constructor.tableName, primaryKey); + await sync_table.addEntitySync(entity.constructor.tableName, primaryKey); + }); } module.exports = { diff --git a/src/services/script_context.js b/src/services/script_context.js index 963fae670..cf38410b2 100644 --- a/src/services/script_context.js +++ b/src/services/script_context.js @@ -40,9 +40,12 @@ function ScriptApi(startNote, currentNote) { this.getInstanceName = () => config.General ? config.General.instanceName : null; - this.getNoteById = async function(noteId) { - return await repository.getNote(noteId); - }; + this.getNote = repository.getNote; + this.getBranch = repository.getBranch; + this.getLabel = repository.getLabel; + this.getImage = repository.getImage; + this.getEntity = repository.getEntity; + this.getEntities = repository.getEntities; this.getNotesWithLabel = async function (labelName, labelValue) { return await labels.getNotesWithLabel(labelName, labelValue); @@ -60,8 +63,6 @@ function ScriptApi(startNote, currentNote) { this.createLabel = labels.createLabel; - this.updateEntity = repository.updateEntity; - this.log = message => log.info(`Script ${currentNote.noteId}: ${message}`); this.getRootCalendarNoteId = date_notes.getRootCalendarNoteId; diff --git a/src/services/source_id.js b/src/services/source_id.js index f5b89f4f1..4862ddf04 100644 --- a/src/services/source_id.js +++ b/src/services/source_id.js @@ -4,11 +4,9 @@ const sql = require('./sql'); const cls = require('./cls'); async function saveSourceId(sourceId) { - await sql.doInTransaction(async () => { - await sql.insert("source_ids", { - sourceId: sourceId, - dateCreated: utils.nowDate() - }); + await sql.insert("source_ids", { + sourceId: sourceId, + dateCreated: utils.nowDate() }); await refreshSourceIds(); diff --git a/src/services/sync.js b/src/services/sync.js index 8c7fc9d1d..239beb367 100644 --- a/src/services/sync.js +++ b/src/services/sync.js @@ -95,9 +95,7 @@ async function getLastSyncedPull() { } async function setLastSyncedPull(syncId) { - await sql.doInTransaction(async () => { - await options.setOption('last_synced_pull', syncId); - }); + await options.setOption('last_synced_pull', syncId); } async function pullSync(syncContext) { @@ -168,9 +166,7 @@ async function getLastSyncedPush() { } async function setLastSyncedPush(lastSyncedPush) { - await sql.doInTransaction(async () => { - await options.setOption('last_synced_push', lastSyncedPush); - }); + await options.setOption('last_synced_push', lastSyncedPush); } async function pushSync(syncContext) {