Notes/src/routes/api/date_notes.js

124 lines
3.3 KiB
JavaScript
Raw Normal View History

"use strict";
const dateNoteService = require('../../services/date_notes');
const sql = require('../../services/sql');
const dateUtils = require('../../services/date_utils');
const noteService = require('../../services/notes');
2020-11-27 22:21:13 +01:00
const attributeService = require('../../services/attributes');
const cls = require('../../services/cls');
const repository = require('../../services/repository');
2020-11-27 22:21:13 +01:00
function getInboxNote(req) {
return attributeService.getNoteWithLabel('inbox')
|| dateNoteService.getDateNote(req.params.date);
}
2020-06-20 12:31:38 +02:00
function getDateNote(req) {
return dateNoteService.getDateNote(req.params.date);
}
2020-06-20 12:31:38 +02:00
function getMonthNote(req) {
return dateNoteService.getMonthNote(req.params.month);
}
2020-06-20 12:31:38 +02:00
function getYearNote(req) {
return dateNoteService.getYearNote(req.params.year);
}
2020-06-20 12:31:38 +02:00
function getDateNotesForMonth(req) {
const month = req.params.month;
return sql.getMap(`
SELECT
attr.value AS date,
notes.noteId
FROM notes
JOIN attributes attr USING(noteId)
WHERE notes.isDeleted = 0
AND attr.isDeleted = 0
AND attr.type = 'label'
AND attr.name = 'dateNote'
AND attr.value LIKE '${month}%'`);
}
2020-06-20 12:31:38 +02:00
function createSqlConsole() {
const today = dateUtils.localNowDate();
2020-12-07 09:19:27 +01:00
const sqlConsoleHome =
attributeService.getNoteWithLabel('sqlConsoleHome')
|| dateNoteService.getDateNote(today);
2020-06-20 12:31:38 +02:00
const {note} = noteService.createNewNote({
2020-12-07 09:19:27 +01:00
parentNoteId: sqlConsoleHome.noteId,
title: 'SQL Console',
content: "SELECT title, isDeleted, isProtected FROM notes WHERE noteId = ''\n\n\n\n",
type: 'code',
mime: 'text/x-sqlite;schema=trilium'
});
2020-06-20 12:31:38 +02:00
note.setLabel("sqlConsole", today);
return note;
}
function createSearchNote(req) {
const params = req.body;
const searchString = params.searchString || "";
let ancestorNoteId = params.ancestorNoteId;
2021-02-27 21:19:54 +01:00
const hoistedNote = cls.getHoistedNoteId() && cls.getHoistedNoteId() !== 'root'
? repository.getNote(cls.getHoistedNoteId())
: null;
let searchHome;
if (hoistedNote) {
([searchHome] = hoistedNote.getDescendantNotesWithLabel('hoistedSearchHome'));
}
if (!searchHome) {
const today = dateUtils.localNowDate();
searchHome = attributeService.getNoteWithLabel('searchHome')
|| dateNoteService.getDateNote(today);
}
2021-02-27 21:19:54 +01:00
if (hoistedNote) {
if (!hoistedNote.getDescendantNoteIds().includes(searchHome.noteId)) {
// otherwise the note would be saved outside of the hoisted context which is weird
searchHome = hoistedNote;
}
if (!ancestorNoteId) {
ancestorNoteId = hoistedNote.noteId;
}
}
const {note} = noteService.createNewNote({
2020-12-04 22:57:54 +01:00
parentNoteId: searchHome.noteId,
title: 'Search: ' + searchString,
content: "",
type: 'search',
mime: 'application/json'
});
note.setLabel('searchString', searchString);
if (ancestorNoteId) {
note.setRelation('ancestor', ancestorNoteId);
}
return note;
}
module.exports = {
2020-11-27 22:21:13 +01:00
getInboxNote,
getDateNote,
getMonthNote,
getYearNote,
getDateNotesForMonth,
createSqlConsole,
createSearchNote
2020-06-20 12:31:38 +02:00
};