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

View File

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

View File

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

View File

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