2018-03-24 22:02:26 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const sql = require('./sql');
|
|
|
|
const utils = require('./utils');
|
|
|
|
const sync_table = require('./sync_table');
|
2018-03-31 09:07:58 -04:00
|
|
|
const repository = require('./repository');
|
2018-03-31 23:19:54 -04:00
|
|
|
const Label = require('../entities/label');
|
2018-03-24 22:02:26 -04:00
|
|
|
|
|
|
|
const BUILTIN_LABELS = [
|
|
|
|
'frontend_startup',
|
|
|
|
'backend_startup',
|
|
|
|
'disable_versioning',
|
|
|
|
'calendar_root',
|
|
|
|
'hide_in_autocomplete',
|
|
|
|
'exclude_from_export',
|
|
|
|
'run',
|
|
|
|
'manual_transaction_handling',
|
|
|
|
'disable_inclusion',
|
|
|
|
'app_css'
|
|
|
|
];
|
|
|
|
|
|
|
|
async function getNoteLabelMap(noteId) {
|
|
|
|
return await sql.getMap(`SELECT name, value FROM labels WHERE noteId = ? AND isDeleted = 0`, [noteId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getNoteIdWithLabel(name, value) {
|
|
|
|
return await sql.getValue(`SELECT notes.noteId FROM notes JOIN labels USING(noteId)
|
|
|
|
WHERE notes.isDeleted = 0
|
|
|
|
AND labels.isDeleted = 0
|
|
|
|
AND labels.name = ?
|
|
|
|
AND labels.value = ?`, [name, value]);
|
|
|
|
}
|
|
|
|
|
2018-03-31 09:07:58 -04:00
|
|
|
async function getNotesWithLabel(name, value) {
|
2018-03-24 22:02:26 -04:00
|
|
|
let notes;
|
|
|
|
|
|
|
|
if (value !== undefined) {
|
|
|
|
notes = await repository.getEntities(`SELECT notes.* FROM notes JOIN labels USING(noteId)
|
|
|
|
WHERE notes.isDeleted = 0 AND labels.isDeleted = 0 AND labels.name = ? AND labels.value = ?`, [name, value]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
notes = await repository.getEntities(`SELECT notes.* FROM notes JOIN labels USING(noteId)
|
|
|
|
WHERE notes.isDeleted = 0 AND labels.isDeleted = 0 AND labels.name = ?`, [name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return notes;
|
|
|
|
}
|
|
|
|
|
2018-03-31 09:07:58 -04:00
|
|
|
async function getNoteWithLabel(name, value) {
|
|
|
|
const notes = getNotesWithLabel(name, value);
|
2018-03-24 22:02:26 -04:00
|
|
|
|
|
|
|
return notes.length > 0 ? notes[0] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getNoteIdsWithLabel(name) {
|
|
|
|
return await sql.getColumn(`SELECT DISTINCT notes.noteId FROM notes JOIN labels USING(noteId)
|
|
|
|
WHERE notes.isDeleted = 0 AND labels.isDeleted = 0 AND labels.name = ? AND labels.isDeleted = 0`, [name]);
|
|
|
|
}
|
|
|
|
|
2018-03-30 19:41:54 -04:00
|
|
|
async function createLabel(noteId, name, value = "") {
|
2018-03-24 22:02:26 -04:00
|
|
|
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]);
|
|
|
|
|
2018-03-31 23:19:54 -04:00
|
|
|
await (new Label({
|
2018-03-24 22:02:26 -04:00
|
|
|
labelId: labelId,
|
|
|
|
noteId: noteId,
|
|
|
|
name: name,
|
|
|
|
value: value,
|
|
|
|
position: position,
|
|
|
|
isDeleted: false
|
2018-03-31 23:19:54 -04:00
|
|
|
})).save();
|
2018-03-24 22:02:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getNoteLabelMap,
|
|
|
|
getNoteIdWithLabel,
|
|
|
|
getNotesWithLabel,
|
|
|
|
getNoteWithLabel,
|
|
|
|
getNoteIdsWithLabel,
|
|
|
|
createLabel,
|
|
|
|
BUILTIN_LABELS
|
|
|
|
};
|