initial work on new router model

This commit is contained in:
azivner
2018-03-30 12:57:22 -04:00
parent 0ec909fd7a
commit efffc29649
5 changed files with 161 additions and 136 deletions

View File

@@ -5,100 +5,88 @@ const router = express.Router();
const auth = require('../../services/auth');
const sql = require('../../services/sql');
const notes = require('../../services/notes');
const log = require('../../services/log');
const utils = require('../../services/utils');
const protected_session = require('../../services/protected_session');
const tree = require('../../services/tree');
const sync_table = require('../../services/sync_table');
const wrap = require('express-promise-wrap').wrap;
router.get('/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
async function getNote(req) {
const noteId = req.params.noteId;
const detail = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
if (!detail) {
log.info("Note " + noteId + " has not been found.");
return res.status(404).send({});
if (!note) {
return [404, "Note " + noteId + " has not been found."];
}
protected_session.decryptNote(req, detail);
protected_session.decryptNote(req, note);
if (detail.type === 'file') {
if (note.type === 'file') {
// no need to transfer (potentially large) file payload for this request
detail.content = null;
note.content = null;
}
res.send(detail);
}));
return note;
}
router.post('/:parentNoteId/children', auth.checkApiAuth, wrap(async (req, res, next) => {
async function createNote(req) {
const sourceId = req.headers.source_id;
const parentNoteId = req.params.parentNoteId;
const newNote = req.body;
await sql.doInTransaction(async () => {
const { noteId, branchId, note } = await notes.createNewNote(parentNoteId, newNote, req, sourceId);
const { noteId, branchId, note } = await notes.createNewNote(parentNoteId, newNote, req, sourceId);
res.send({
'noteId': noteId,
'branchId': branchId,
'note': note
});
});
}));
return {
'noteId': noteId,
'branchId': branchId,
'note': note
};
}
router.put('/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
async function updateNote(req) {
const note = req.body;
const noteId = req.params.noteId;
const sourceId = req.headers.source_id;
const dataKey = protected_session.getDataKey(req);
await sql.doInTransaction(async () => {
await notes.updateNote(noteId, note, dataKey, sourceId);
});
await notes.updateNote(noteId, note, dataKey, sourceId);
}
res.send({});
}));
router.put('/:noteId/sort', auth.checkApiAuth, wrap(async (req, res, next) => {
async function sortNotes(req) {
const noteId = req.params.noteId;
const sourceId = req.headers.source_id;
const dataKey = protected_session.getDataKey(req);
await tree.sortNotesAlphabetically(noteId, dataKey, sourceId);
}
res.send({});
}));
router.put('/:noteId/protect-sub-tree/:isProtected', auth.checkApiAuth, wrap(async (req, res, next) => {
async function protectBranch(req) {
const noteId = req.params.noteId;
const isProtected = !!parseInt(req.params.isProtected);
const dataKey = protected_session.getDataKey(req);
const sourceId = req.headers.source_id;
await sql.doInTransaction(async () => {
await notes.protectNoteRecursively(noteId, dataKey, isProtected, sourceId);
});
await notes.protectNoteRecursively(noteId, dataKey, isProtected, sourceId);
}
res.send({});
}));
router.put(/\/(.*)\/type\/(.*)\/mime\/(.*)/, auth.checkApiAuth, wrap(async (req, res, next) => {
async function setNoteTypeMime(req) {
const noteId = req.params[0];
const type = req.params[1];
const mime = req.params[2];
const sourceId = req.headers.source_id;
await sql.doInTransaction(async () => {
await sql.execute("UPDATE notes SET type = ?, mime = ?, dateModified = ? WHERE noteId = ?",
[type, mime, utils.nowDate(), noteId]);
await sql.execute("UPDATE notes SET type = ?, mime = ?, dateModified = ? WHERE noteId = ?",
[type, mime, utils.nowDate(), noteId]);
await sync_table.addNoteSync(noteId, sourceId);
});
await sync_table.addNoteSync(noteId, sourceId);
}
res.send({});
}));
module.exports = router;
module.exports = {
getNote,
updateNote,
createNote,
sortNotes,
protectBranch,
setNoteTypeMime
};