2017-10-21 21:10:33 -04:00
|
|
|
"use strict";
|
|
|
|
|
2017-10-14 23:31:44 -04:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
2017-11-05 10:41:54 -05:00
|
|
|
const auth = require('../../services/auth');
|
2017-10-15 19:47:05 -04:00
|
|
|
const sql = require('../../services/sql');
|
2017-11-05 10:41:54 -05:00
|
|
|
const notes = require('../../services/notes');
|
2017-11-26 23:10:23 -05:00
|
|
|
const log = require('../../services/log');
|
2017-11-12 21:40:26 -05:00
|
|
|
const protected_session = require('../../services/protected_session');
|
|
|
|
const data_encryption = require('../../services/data_encryption');
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2017-10-15 16:32:49 -04:00
|
|
|
router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => {
|
2017-11-15 00:04:26 -05:00
|
|
|
const noteId = req.params.noteId;
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2017-11-20 23:51:28 -05:00
|
|
|
const detail = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]);
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2017-11-26 23:10:23 -05:00
|
|
|
if (!detail) {
|
|
|
|
log.info("Note " + noteId + " has not been found.");
|
|
|
|
|
|
|
|
return res.status(404).send({});
|
|
|
|
}
|
|
|
|
|
2017-11-14 21:54:12 -05:00
|
|
|
if (detail.is_protected) {
|
2017-11-12 21:40:26 -05:00
|
|
|
const dataKey = protected_session.getDataKey(req);
|
|
|
|
|
2017-11-18 17:05:50 -05:00
|
|
|
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-12 21:40:26 -05:00
|
|
|
}
|
|
|
|
|
2017-10-14 23:31:44 -04:00
|
|
|
res.send({
|
2017-12-10 12:56:59 -05:00
|
|
|
detail: detail
|
2017-10-14 23:31:44 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-23 09:57:20 -05:00
|
|
|
router.post('/:parentNoteId/children', auth.checkApiAuth, async (req, res, next) => {
|
2017-12-16 20:48:34 -05:00
|
|
|
const sourceId = req.headers.source_id;
|
2017-11-22 23:16:54 -05:00
|
|
|
const parentNoteId = req.params.parentNoteId;
|
2017-10-14 23:31:44 -04:00
|
|
|
const note = req.body;
|
|
|
|
|
2017-12-16 20:48:34 -05:00
|
|
|
const { noteId, noteTreeId } = await notes.createNewNote(parentNoteId, note, sourceId);
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2017-11-05 10:41:54 -05:00
|
|
|
res.send({
|
2017-11-18 17:05:50 -05:00
|
|
|
'note_id': noteId,
|
|
|
|
'note_tree_id': noteTreeId
|
2017-10-29 18:50:28 -04:00
|
|
|
});
|
2017-10-14 23:31:44 -04:00
|
|
|
});
|
|
|
|
|
2017-12-23 09:57:20 -05:00
|
|
|
router.put('/:noteId', auth.checkApiAuth, async (req, res, next) => {
|
2017-11-14 21:54:12 -05:00
|
|
|
const note = req.body;
|
2017-11-15 00:04:26 -05:00
|
|
|
const noteId = req.params.noteId;
|
2017-12-16 21:23:35 -05:00
|
|
|
const sourceId = req.headers.source_id;
|
|
|
|
const dataKey = protected_session.getDataKey(req);
|
2017-11-05 10:41:54 -05:00
|
|
|
|
2017-12-16 21:23:35 -05:00
|
|
|
await notes.updateNote(noteId, note, dataKey, sourceId);
|
2017-10-14 23:31:44 -04:00
|
|
|
|
|
|
|
res.send({});
|
|
|
|
});
|
|
|
|
|
2017-12-23 09:57:20 -05:00
|
|
|
router.delete('/:noteTreeId', auth.checkApiAuth, async (req, res, next) => {
|
2017-11-28 17:24:08 -05:00
|
|
|
await sql.doInTransaction(async () => {
|
2017-12-16 20:48:34 -05:00
|
|
|
await notes.deleteNote(req.params.noteTreeId, req.headers.source_id);
|
2017-10-14 23:31:44 -04:00
|
|
|
});
|
|
|
|
|
2017-11-05 10:41:54 -05:00
|
|
|
res.send({});
|
2017-10-14 23:31:44 -04:00
|
|
|
});
|
|
|
|
|
2017-12-23 09:57:20 -05:00
|
|
|
router.get('/', auth.checkApiAuth, async (req, res, next) => {
|
2017-10-14 23:31:44 -04:00
|
|
|
const search = '%' + req.query.search + '%';
|
|
|
|
|
2017-11-20 23:51:28 -05:00
|
|
|
const result = await sql.getResults("SELECT note_id FROM notes WHERE note_title liKE ? OR note_text LIKE ?", [search, search]);
|
2017-10-14 23:31:44 -04:00
|
|
|
|
|
|
|
const noteIdList = [];
|
|
|
|
|
|
|
|
for (const res of result) {
|
2017-10-31 00:15:49 -04:00
|
|
|
noteIdList.push(res.note_id);
|
2017-10-14 23:31:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
res.send(noteIdList);
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|