converted cloning and label routes

This commit is contained in:
azivner
2018-03-30 13:20:36 -04:00
parent efffc29649
commit 8550ed72f2
6 changed files with 121 additions and 107 deletions

View File

@@ -1,59 +1,53 @@
"use strict";
const express = require('express');
const router = express.Router();
const sql = require('../../services/sql');
const auth = require('../../services/auth');
const sync_table = require('../../services/sync_table');
const utils = require('../../services/utils');
const wrap = require('express-promise-wrap').wrap;
const labels = require('../../services/labels');
router.get('/notes/:noteId/labels', auth.checkApiAuth, wrap(async (req, res, next) => {
async function getNoteLabels(req) {
const noteId = req.params.noteId;
res.send(await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]));
}));
return await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
}
router.put('/notes/:noteId/labels', auth.checkApiAuth, wrap(async (req, res, next) => {
async function updateNoteLabels(req, res, next) {
const noteId = req.params.noteId;
const labels = req.body;
const now = utils.nowDate();
await sql.doInTransaction(async () => {
for (const attr of labels) {
if (attr.labelId) {
await sql.execute("UPDATE labels SET name = ?, value = ?, dateModified = ?, isDeleted = ?, position = ? WHERE labelId = ?",
[attr.name, attr.value, now, attr.isDeleted, attr.position, attr.labelId]);
}
else {
// if it was "created" and then immediatelly deleted, we just don't create it at all
if (attr.isDeleted) {
continue;
}
attr.labelId = utils.newLabelId();
await sql.insert("labels", {
labelId: attr.labelId,
noteId: noteId,
name: attr.name,
value: attr.value,
position: attr.position,
dateCreated: now,
dateModified: now,
isDeleted: false
});
}
await sync_table.addLabelSync(attr.labelId);
for (const attr of labels) {
if (attr.labelId) {
await sql.execute("UPDATE labels SET name = ?, value = ?, dateModified = ?, isDeleted = ?, position = ? WHERE labelId = ?",
[attr.name, attr.value, now, attr.isDeleted, attr.position, attr.labelId]);
}
});
else {
// if it was "created" and then immediatelly deleted, we just don't create it at all
if (attr.isDeleted) {
continue;
}
res.send(await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]));
}));
attr.labelId = utils.newLabelId();
router.get('/labels/names', auth.checkApiAuth, wrap(async (req, res, next) => {
await sql.insert("labels", {
labelId: attr.labelId,
noteId: noteId,
name: attr.name,
value: attr.value,
position: attr.position,
dateCreated: now,
dateModified: now,
isDeleted: false
});
}
await sync_table.addLabelSync(attr.labelId);
}
return await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
}
async function getAllLabelNames(req) {
const names = await sql.getColumn("SELECT DISTINCT name FROM labels WHERE isDeleted = 0");
for (const attr of labels.BUILTIN_LABELS) {
@@ -64,15 +58,18 @@ router.get('/labels/names', auth.checkApiAuth, wrap(async (req, res, next) => {
names.sort();
res.send(names);
}));
return names;
}
router.get('/labels/values/:labelName', auth.checkApiAuth, wrap(async (req, res, next) => {
async function getValuesForLabel(req) {
const labelName = req.params.labelName;
const values = await sql.getColumn("SELECT DISTINCT value FROM labels WHERE isDeleted = 0 AND name = ? AND value != '' ORDER BY value", [labelName]);
return await sql.getColumn("SELECT DISTINCT value FROM labels WHERE isDeleted = 0 AND name = ? AND value != '' ORDER BY value", [labelName]);
}
res.send(values);
}));
module.exports = router;
module.exports = {
getNoteLabels,
updateNoteLabels,
getAllLabelNames,
getValuesForLabel
};