2017-10-21 21:10:33 -04:00
|
|
|
"use strict";
|
|
|
|
|
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');
|
2018-01-15 20:54:22 -05:00
|
|
|
const utils = require('../../services/utils');
|
2018-01-13 20:53:00 -05:00
|
|
|
const tree = require('../../services/tree');
|
2018-01-20 21:56:03 -05:00
|
|
|
const sync_table = require('../../services/sync_table');
|
2018-03-31 10:51:37 -04:00
|
|
|
const repository = require('../../services/repository');
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
async function getNote(req) {
|
2017-11-15 00:04:26 -05:00
|
|
|
const noteId = req.params.noteId;
|
2018-04-01 11:42:12 -04:00
|
|
|
const note = await repository.getNote(noteId);
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
if (!note) {
|
|
|
|
return [404, "Note " + noteId + " has not been found."];
|
2017-11-26 23:10:23 -05:00
|
|
|
}
|
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
if (note.type === 'file') {
|
2018-03-27 22:11:06 -04:00
|
|
|
// no need to transfer (potentially large) file payload for this request
|
2018-03-30 12:57:22 -04:00
|
|
|
note.content = null;
|
2018-02-18 21:28:24 -05:00
|
|
|
}
|
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
return note;
|
|
|
|
}
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
async function createNote(req) {
|
2017-11-22 23:16:54 -05:00
|
|
|
const parentNoteId = req.params.parentNoteId;
|
2018-01-28 10:37:43 -05:00
|
|
|
const newNote = req.body;
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2018-04-01 11:42:12 -04:00
|
|
|
const { note, branch } = await notes.createNewNote(parentNoteId, newNote, req);
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
return {
|
2018-04-01 11:42:12 -04:00
|
|
|
note,
|
|
|
|
branch
|
2018-03-30 12:57:22 -04:00
|
|
|
};
|
|
|
|
}
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
async function updateNote(req) {
|
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-11-05 10:41:54 -05:00
|
|
|
|
2018-03-31 08:53:52 -04:00
|
|
|
await notes.updateNote(noteId, note);
|
2018-03-30 12:57:22 -04:00
|
|
|
}
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
async function sortNotes(req) {
|
2018-01-13 17:00:40 -05:00
|
|
|
const noteId = req.params.noteId;
|
|
|
|
|
2018-03-31 08:53:52 -04:00
|
|
|
await tree.sortNotesAlphabetically(noteId);
|
2018-03-30 12:57:22 -04:00
|
|
|
}
|
2018-01-13 17:00:40 -05:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
async function protectBranch(req) {
|
2018-01-13 20:53:00 -05:00
|
|
|
const noteId = req.params.noteId;
|
2018-03-31 10:51:37 -04:00
|
|
|
const note = repository.getNote(noteId);
|
|
|
|
const protect = !!parseInt(req.params.isProtected);
|
2018-01-13 17:00:40 -05:00
|
|
|
|
2018-03-31 10:51:37 -04:00
|
|
|
await notes.protectNoteRecursively(note, protect);
|
2018-03-30 12:57:22 -04:00
|
|
|
}
|
2018-01-13 17:00:40 -05:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
async function setNoteTypeMime(req) {
|
2018-01-23 23:41:22 -05:00
|
|
|
const noteId = req.params[0];
|
|
|
|
const type = req.params[1];
|
|
|
|
const mime = req.params[2];
|
2018-01-20 21:56:03 -05:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
await sql.execute("UPDATE notes SET type = ?, mime = ?, dateModified = ? WHERE noteId = ?",
|
|
|
|
[type, mime, utils.nowDate(), noteId]);
|
2018-01-20 21:56:03 -05:00
|
|
|
|
2018-03-30 19:41:54 -04:00
|
|
|
await sync_table.addNoteSync(noteId);
|
2018-03-30 12:57:22 -04:00
|
|
|
}
|
2018-01-20 21:56:03 -05:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
module.exports = {
|
|
|
|
getNote,
|
|
|
|
updateNote,
|
|
|
|
createNote,
|
|
|
|
sortNotes,
|
|
|
|
protectBranch,
|
|
|
|
setNoteTypeMime
|
|
|
|
};
|