converted option operations to repository

This commit is contained in:
azivner 2018-05-22 00:22:43 -04:00
parent 49a53f7a45
commit cd45bcfd03
4 changed files with 30 additions and 13 deletions

View File

@ -6,6 +6,7 @@ const Branch = require('../entities/branch');
const Label = require('../entities/label');
const RecentNote = require('../entities/recent_note');
const ApiToken = require('../entities/api_token');
const Option = require('../entities/option');
const repository = require('../services/repository');
function createEntityFromRow(row) {
@ -35,6 +36,9 @@ function createEntityFromRow(row) {
else if (row.noteId) {
entity = new Note(row);
}
else if (row.name) {
entity = new Option(row);
}
else {
throw new Error('Unknown entity type for row: ' + JSON.stringify(row));
}

View File

@ -1,11 +1,18 @@
"use strict";
const Entity = require('./entity');
const dateUtils = require('../services/date_utils');
class Option extends Entity {
static get tableName() { return "options"; }
static get primaryKeyName() { return "name"; }
static get syncedProperties() { return ["name", "value"]; }
beforeSaving() {
super.beforeSaving();
this.dateModified = dateUtils.nowDate();
}
}
module.exports = Option;

View File

@ -1,41 +1,42 @@
const sql = require('./sql');
const repository = require('./repository');
const utils = require('./utils');
const dateUtils = require('./date_utils');
const syncTableService = require('./sync_table');
const appInfo = require('./app_info');
const Option = require('../entities/option');
async function getOption(name) {
const row = await await sql.getRowOrNull("SELECT value FROM options WHERE name = ?", [name]);
const option = await repository.getOption(name);
if (!row) {
if (!option) {
throw new Error("Option " + name + " doesn't exist");
}
return row.value;
return option.value;
}
async function setOption(name, value) {
const opt = await sql.getRow("SELECT * FROM options WHERE name = ?", [name]);
const option = await repository.getOption(name);
if (!opt) {
if (!option) {
throw new Error(`Option ${name} doesn't exist`);
}
if (opt.isSynced) {
if (option.isSynced) {
await syncTableService.addOptionsSync(name);
}
await sql.execute("UPDATE options SET value = ?, dateModified = ? WHERE name = ?",
[value, dateUtils.nowDate(), name]);
option.value = value;
await option.save();
}
async function createOption(name, value, isSynced) {
await sql.insert("options", {
await new Option({
name: name,
value: value,
isSynced: isSynced,
dateModified: dateUtils.nowDate()
});
isSynced: isSynced
}).save();
if (isSynced) {
await syncTableService.addOptionsSync(name);

View File

@ -41,6 +41,10 @@ async function getLabel(labelId) {
return await getEntity("SELECT * FROM labels WHERE labelId = ?", [labelId]);
}
async function getOption(name) {
return await getEntity("SELECT * FROM options WHERE name = ?", [name]);
}
async function updateEntity(entity) {
if (entity.beforeSaving) {
await entity.beforeSaving();
@ -66,6 +70,7 @@ module.exports = {
getBranch,
getImage,
getLabel,
getOption,
updateEntity,
setEntityConstructor
};