trilium/src/routes/api/labels.js

70 lines
2 KiB
JavaScript
Raw Normal View History

"use strict";
const sql = require('../../services/sql');
const labelService = require('../../services/labels');
2018-04-01 23:42:12 +08:00
const repository = require('../../services/repository');
const Label = require('../../entities/label');
2018-03-31 01:20:36 +08:00
async function getNoteLabels(req) {
const noteId = req.params.noteId;
2018-04-01 23:42:12 +08:00
return await repository.getEntities("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
2018-03-31 01:20:36 +08:00
}
2018-04-01 23:42:12 +08:00
async function updateNoteLabels(req) {
const noteId = req.params.noteId;
const labels = req.body;
for (const label of labels) {
2018-04-01 23:42:12 +08:00
let labelEntity;
if (label.labelId) {
2018-04-01 23:42:12 +08:00
labelEntity = await repository.getLabel(label.labelId);
2018-03-31 01:20:36 +08:00
}
else {
// if it was "created" and then immediatelly deleted, we just don't create it at all
if (label.isDeleted) {
2018-03-31 01:20:36 +08:00
continue;
}
2018-04-01 23:42:12 +08:00
labelEntity = new Label();
labelEntity.noteId = noteId;
}
2018-04-01 23:42:12 +08:00
labelEntity.name = label.name;
labelEntity.value = label.value;
labelEntity.position = label.position;
labelEntity.isDeleted = label.isDeleted;
await labelEntity.save();
2018-03-31 01:20:36 +08:00
}
2018-04-01 23:42:12 +08:00
return await repository.getEntities("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
2018-03-31 01:20:36 +08:00
}
2018-04-01 23:42:12 +08:00
async function getAllLabelNames() {
const names = await sql.getColumn("SELECT DISTINCT name FROM labels WHERE isDeleted = 0");
for (const label of labelService.BUILTIN_LABELS) {
if (!names.includes(label)) {
names.push(label);
}
}
names.sort();
2018-03-31 01:20:36 +08:00
return names;
}
2018-03-31 01:20:36 +08:00
async function getValuesForLabel(req) {
const labelName = req.params.labelName;
2018-03-31 01:20:36 +08:00
return await sql.getColumn("SELECT DISTINCT value FROM labels WHERE isDeleted = 0 AND name = ? AND value != '' ORDER BY value", [labelName]);
}
2018-03-31 01:20:36 +08:00
module.exports = {
getNoteLabels,
updateNoteLabels,
getAllLabelNames,
getValuesForLabel
};