trilium/routes/api/notes.js

81 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-10-22 09:10:33 +08:00
"use strict";
const express = require('express');
const router = express.Router();
const auth = require('../../services/auth');
2017-10-16 07:47:05 +08:00
const sql = require('../../services/sql');
const utils = require('../../services/utils');
const notes = require('../../services/notes');
const log = require('../../services/log');
2017-11-13 10:40:26 +08:00
const protected_session = require('../../services/protected_session');
const data_encryption = require('../../services/data_encryption');
const RequestContext = require('../../services/request_context');
2017-10-16 04:32:49 +08:00
router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => {
2017-11-15 13:04:26 +08:00
const noteId = req.params.noteId;
2017-11-21 12:51:28 +08:00
const detail = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]);
if (!detail) {
log.info("Note " + noteId + " has not been found.");
return res.status(404).send({});
}
if (detail.is_protected) {
2017-11-13 10:40:26 +08:00
const dataKey = protected_session.getDataKey(req);
detail.note_title = data_encryption.decryptString(dataKey, data_encryption.noteTitleIv(detail.note_id), detail.note_title);
detail.note_text = data_encryption.decryptString(dataKey, data_encryption.noteTextIv(detail.note_id), detail.note_text);
2017-11-13 10:40:26 +08:00
}
res.send({
detail: detail
});
});
router.post('/:parentNoteId/children', async (req, res, next) => {
const parentNoteId = req.params.parentNoteId;
const note = req.body;
2017-11-25 09:51:44 +08:00
const { noteId, noteTreeId } = await notes.createNewNote(parentNoteId, note);
res.send({
'note_id': noteId,
'note_tree_id': noteTreeId
});
});
router.put('/:noteId', async (req, res, next) => {
const note = req.body;
2017-11-15 13:04:26 +08:00
const noteId = req.params.noteId;
const reqCtx = new RequestContext(req);
await notes.updateNote(noteId, note, reqCtx);
res.send({});
});
2017-11-20 12:12:39 +08:00
router.delete('/:noteTreeId', async (req, res, next) => {
await sql.doInTransaction(async () => {
await notes.deleteNote(req.params.noteTreeId);
});
res.send({});
});
router.get('/', async (req, res, next) => {
const search = '%' + req.query.search + '%';
2017-11-21 12:51:28 +08:00
const result = await sql.getResults("SELECT note_id FROM notes WHERE note_title liKE ? OR note_text LIKE ?", [search, search]);
const noteIdList = [];
for (const res of result) {
noteIdList.push(res.note_id);
}
res.send(noteIdList);
});
module.exports = router;