Notes/src/routes/api/notes.js

201 lines
5.2 KiB
JavaScript
Raw Normal View History

2017-10-21 21:10:33 -04:00
"use strict";
const noteService = require('../../services/notes');
const treeService = require('../../services/tree');
const repository = require('../../services/repository');
2019-10-18 22:27:38 +02:00
const utils = require('../../services/utils');
const TaskContext = require('../../services/task_context');
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);
2018-03-30 12:57:22 -04:00
if (!note) {
return [404, "Note " + noteId + " has not been found."];
}
if (note.isStringNote()) {
await note.getContent();
2019-02-07 22:16:40 +01:00
if (note.type === 'file') {
note.content = note.content.substr(0, 10000);
2019-02-07 22:16:40 +01:00
}
}
note.cssClass = (await note.getLabels("cssClass")).map(label => label.value).join(" ");
2018-03-30 12:57:22 -04:00
return note;
}
2018-10-21 10:26:14 +02:00
async function getChildren(req) {
const parentNoteId = req.params.parentNoteId;
const parentNote = await repository.getNote(parentNoteId);
if (!parentNote) {
return [404, `Note ${parentNoteId} has not been found.`];
}
const ret = [];
for (const childNote of await parentNote.getChildNotes()) {
ret.push({
noteId: childNote.noteId,
title: childNote.title,
relations: (await childNote.getRelations()).map(relation => { return {
attributeId: relation.attributeId,
name: relation.name,
targetNoteId: relation.value
}; })
});
}
return ret;
}
2018-03-30 12:57:22 -04:00
async function createNote(req) {
const parentNoteId = req.params.parentNoteId;
const newNote = req.body;
const { note, branch } = await noteService.createNewNote(parentNoteId, newNote, req);
note.cssClass = (await note.getLabels("cssClass")).map(label => label.value).join(" ");
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
};
}
2018-03-30 12:57:22 -04:00
async function updateNote(req) {
const note = req.body;
2017-11-15 00:04:26 -05:00
const noteId = req.params.noteId;
return await noteService.updateNote(noteId, note);
2018-03-30 12:57:22 -04:00
}
async function deleteNote(req) {
const noteId = req.params.noteId;
2019-10-19 00:11:07 +02:00
const taskId = req.query.taskId;
const last = req.query.last === 'true';
const note = await repository.getNote(noteId);
2019-10-19 00:11:07 +02:00
const taskContext = TaskContext.getInstance(taskId, 'delete-notes');
2019-10-18 22:27:38 +02:00
for (const branch of await note.getBranches()) {
2019-10-18 22:27:38 +02:00
await noteService.deleteBranch(branch, taskContext);
}
2019-10-19 00:11:07 +02:00
if (last) {
await taskContext.taskSucceeded();
}
}
2018-03-30 12:57:22 -04:00
async function sortNotes(req) {
const noteId = req.params.noteId;
await treeService.sortNotesAlphabetically(noteId);
2018-03-30 12:57:22 -04:00
}
async function protectSubtree(req) {
const noteId = req.params.noteId;
2018-04-19 22:18:19 -04:00
const note = await repository.getNote(noteId);
const protect = !!parseInt(req.params.isProtected);
const taskContext = new TaskContext(utils.randomString(10), 'protect-notes', {protect});
await noteService.protectNoteRecursively(note, protect, taskContext);
taskContext.taskSucceeded();
2018-03-30 12:57:22 -04:00
}
2018-03-30 12:57:22 -04:00
async function setNoteTypeMime(req) {
2018-04-04 23:04:31 -04:00
// can't use [] destructuring because req.params is not iterable
const noteId = req.params[0];
const type = req.params[1];
const mime = req.params[2];
2018-01-20 21:56:03 -05:00
2018-04-01 17:38:24 -04:00
const note = await repository.getNote(noteId);
note.type = type;
note.mime = mime;
await note.save();
2018-03-30 12:57:22 -04:00
}
2018-01-20 21:56:03 -05:00
2018-10-25 12:06:36 +02:00
async function getRelationMap(req) {
const noteIds = req.body.noteIds;
const resp = {
// noteId => title
2018-10-25 12:06:36 +02:00
noteTitles: {},
relations: [],
// relation name => inverse relation name
2019-08-19 20:12:00 +02:00
inverseRelations: {}
2018-10-25 12:06:36 +02:00
};
if (noteIds.length === 0) {
return resp;
}
const questionMarks = noteIds.map(noteId => '?').join(',');
const notes = await repository.getEntities(`SELECT * FROM notes WHERE isDeleted = 0 AND noteId IN (${questionMarks})`, noteIds);
for (const note of notes) {
resp.noteTitles[note.noteId] = note.title;
resp.relations = resp.relations.concat((await note.getRelations())
.filter(relation => noteIds.includes(relation.value))
.map(relation => { return {
attributeId: relation.attributeId,
sourceNoteId: relation.noteId,
targetNoteId: relation.value,
name: relation.name
}; }));
for (const relationDefinition of await note.getRelationDefinitions()) {
if (relationDefinition.value.inverseRelation) {
resp.inverseRelations[relationDefinition.name] = relationDefinition.value.inverseRelation;
}
}
}
2018-10-25 12:06:36 +02:00
return resp;
}
async function changeTitle(req) {
const noteId = req.params.noteId;
const title = req.body.title;
const note = await repository.getNote(noteId);
if (!note) {
return [404, `Note ${noteId} has not been found`];
}
if (!note.isContentAvailable) {
return [400, `Note ${noteId} is not available for change`];
}
note.title = title;
await note.save();
}
2019-10-19 12:36:16 +02:00
async function duplicateNote(req) {
const {noteId, parentNoteId} = req.params;
return await noteService.duplicateNote(noteId, parentNoteId);
}
2018-03-30 12:57:22 -04:00
module.exports = {
getNote,
updateNote,
deleteNote,
2018-03-30 12:57:22 -04:00
createNote,
sortNotes,
protectSubtree,
2018-10-21 10:26:14 +02:00
setNoteTypeMime,
2018-10-25 12:06:36 +02:00
getChildren,
getRelationMap,
2019-10-19 12:36:16 +02:00
changeTitle,
duplicateNote
2018-03-30 12:57:22 -04:00
};