2017-10-22 09:10:33 +08:00
|
|
|
"use strict";
|
|
|
|
|
2017-10-15 11:31:44 +08:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
2017-11-05 23:41:54 +08:00
|
|
|
const auth = require('../../services/auth');
|
2017-10-16 07:47:05 +08:00
|
|
|
const sql = require('../../services/sql');
|
|
|
|
const utils = require('../../services/utils');
|
2017-11-05 23:41:54 +08:00
|
|
|
const notes = require('../../services/notes');
|
2017-11-13 10:40:26 +08:00
|
|
|
const protected_session = require('../../services/protected_session');
|
|
|
|
const data_encryption = require('../../services/data_encryption');
|
2017-11-15 10:54:12 +08:00
|
|
|
const RequestContext = require('../../services/request_context');
|
2017-10-15 11:31:44 +08:00
|
|
|
|
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-10-15 11:31:44 +08:00
|
|
|
|
2017-11-21 12:51:28 +08:00
|
|
|
const detail = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-11-15 10:54:12 +08:00
|
|
|
if (detail.is_protected) {
|
2017-11-13 10:40:26 +08:00
|
|
|
const dataKey = protected_session.getDataKey(req);
|
|
|
|
|
2017-11-19 06:05:50 +08: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-13 10:40:26 +08:00
|
|
|
}
|
|
|
|
|
2017-10-15 11:31:44 +08:00
|
|
|
res.send({
|
2017-11-05 09:02:56 +08:00
|
|
|
detail: detail,
|
2017-11-21 12:51:28 +08:00
|
|
|
images: await sql.getResults("SELECT * FROM images WHERE note_id = ? order by note_offset", [detail.note_id]),
|
2017-11-05 09:02:56 +08:00
|
|
|
loadTime: utils.nowTimestamp()
|
2017-10-15 11:31:44 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-23 12:16:54 +08:00
|
|
|
router.post('/:parentNoteId/children', async (req, res, next) => {
|
|
|
|
const parentNoteId = req.params.parentNoteId;
|
2017-11-05 23:41:54 +08:00
|
|
|
const browserId = utils.browserId(req);
|
2017-10-15 11:31:44 +08:00
|
|
|
const note = req.body;
|
|
|
|
|
2017-11-23 12:16:54 +08:00
|
|
|
const { noteId, noteTreeId } = await notes.createNewNote(parentNoteId, note, browserId);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-11-05 23:41:54 +08:00
|
|
|
res.send({
|
2017-11-19 06:05:50 +08:00
|
|
|
'note_id': noteId,
|
|
|
|
'note_tree_id': noteTreeId
|
2017-10-30 06:50:28 +08:00
|
|
|
});
|
2017-10-15 11:31:44 +08:00
|
|
|
});
|
|
|
|
|
2017-11-05 23:41:54 +08:00
|
|
|
router.put('/:noteId', async (req, res, next) => {
|
2017-11-15 10:54:12 +08:00
|
|
|
const note = req.body;
|
2017-11-15 13:04:26 +08:00
|
|
|
const noteId = req.params.noteId;
|
2017-11-15 10:54:12 +08:00
|
|
|
const reqCtx = new RequestContext(req);
|
2017-11-05 23:41:54 +08:00
|
|
|
|
2017-11-15 10:54:12 +08:00
|
|
|
await notes.updateNote(noteId, note, reqCtx);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
|
|
|
res.send({});
|
|
|
|
});
|
|
|
|
|
2017-11-20 12:12:39 +08:00
|
|
|
router.delete('/:noteTreeId', async (req, res, next) => {
|
2017-11-05 23:41:54 +08:00
|
|
|
const browserId = utils.browserId(req);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-10-30 06:50:28 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2017-11-20 12:12:39 +08:00
|
|
|
await notes.deleteNote(req.params.noteTreeId, browserId);
|
2017-10-15 11:31:44 +08:00
|
|
|
});
|
|
|
|
|
2017-11-05 23:41:54 +08:00
|
|
|
res.send({});
|
2017-10-15 11:31:44 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
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]);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
|
|
|
const noteIdList = [];
|
|
|
|
|
|
|
|
for (const res of result) {
|
2017-10-31 12:15:49 +08:00
|
|
|
noteIdList.push(res.note_id);
|
2017-10-15 11:31:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
res.send(noteIdList);
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|