2017-10-22 09:10:33 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
const noteService = require('../../services/notes');
|
|
|
|
const treeService = require('../../services/tree');
|
2018-03-31 22:51:37 +08:00
|
|
|
const repository = require('../../services/repository');
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
async function getNote(req) {
|
2017-11-15 13:04:26 +08:00
|
|
|
const noteId = req.params.noteId;
|
2018-04-01 23:42:12 +08:00
|
|
|
const note = await repository.getNote(noteId);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
if (!note) {
|
|
|
|
return [404, "Note " + noteId + " has not been found."];
|
2017-11-27 12:10:23 +08:00
|
|
|
}
|
|
|
|
|
2018-11-08 17:11:00 +08:00
|
|
|
if (note.type === 'file' || note.type === 'image') {
|
|
|
|
// no need to transfer (potentially large) file/image payload for this request
|
2018-03-31 00:57:22 +08:00
|
|
|
note.content = null;
|
2018-02-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
return note;
|
|
|
|
}
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-10-21 16:26:14 +08: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-31 00:57:22 +08:00
|
|
|
async function createNote(req) {
|
2017-11-23 12:16:54 +08:00
|
|
|
const parentNoteId = req.params.parentNoteId;
|
2018-01-28 23:37:43 +08:00
|
|
|
const newNote = req.body;
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
const { note, branch } = await noteService.createNewNote(parentNoteId, newNote, req);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-09-01 19:18:55 +08:00
|
|
|
note.cssClass = (await note.getLabels("cssClass")).map(label => label.value).join(" ");
|
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
return {
|
2018-04-01 23:42:12 +08:00
|
|
|
note,
|
|
|
|
branch
|
2018-03-31 00:57:22 +08:00
|
|
|
};
|
|
|
|
}
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
async function updateNote(req) {
|
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-05 23:41:54 +08:00
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
await noteService.updateNote(noteId, note);
|
2018-03-31 00:57:22 +08:00
|
|
|
}
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
async function sortNotes(req) {
|
2018-01-14 06:00:40 +08:00
|
|
|
const noteId = req.params.noteId;
|
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
await treeService.sortNotesAlphabetically(noteId);
|
2018-03-31 00:57:22 +08:00
|
|
|
}
|
2018-01-14 06:00:40 +08:00
|
|
|
|
2018-09-01 00:22:53 +08:00
|
|
|
async function protectSubtree(req) {
|
2018-01-14 09:53:00 +08:00
|
|
|
const noteId = req.params.noteId;
|
2018-04-20 10:18:19 +08:00
|
|
|
const note = await repository.getNote(noteId);
|
2018-03-31 22:51:37 +08:00
|
|
|
const protect = !!parseInt(req.params.isProtected);
|
2018-01-14 06:00:40 +08:00
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
await noteService.protectNoteRecursively(note, protect);
|
2018-03-31 00:57:22 +08:00
|
|
|
}
|
2018-01-14 06:00:40 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
async function setNoteTypeMime(req) {
|
2018-04-05 11:04:31 +08: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-21 10:56:03 +08:00
|
|
|
|
2018-04-02 05:38:24 +08:00
|
|
|
const note = await repository.getNote(noteId);
|
|
|
|
note.type = type;
|
|
|
|
note.mime = mime;
|
|
|
|
await note.save();
|
2018-03-31 00:57:22 +08:00
|
|
|
}
|
2018-01-21 10:56:03 +08:00
|
|
|
|
2018-10-25 18:06:36 +08:00
|
|
|
async function getRelationMap(req) {
|
|
|
|
const noteIds = req.body.noteIds;
|
|
|
|
const resp = {
|
2018-11-13 19:50:08 +08:00
|
|
|
// noteId => title
|
2018-10-25 18:06:36 +08:00
|
|
|
noteTitles: {},
|
2018-11-13 19:50:08 +08:00
|
|
|
relations: [],
|
|
|
|
// relation name => mirror relation name
|
|
|
|
mirrorRelations: {}
|
2018-10-25 18:06:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (noteIds.length === 0) {
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
|
|
|
const questionMarks = noteIds.map(noteId => '?').join(',');
|
|
|
|
|
2018-11-13 19:50:08 +08:00
|
|
|
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.mirrorRelation) {
|
|
|
|
resp.mirrorRelations[relationDefinition.name] = relationDefinition.value.mirrorRelation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-25 18:06:36 +08:00
|
|
|
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
2018-10-31 05:18:20 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
module.exports = {
|
|
|
|
getNote,
|
|
|
|
updateNote,
|
|
|
|
createNote,
|
|
|
|
sortNotes,
|
2018-09-01 00:22:53 +08:00
|
|
|
protectSubtree,
|
2018-10-21 16:26:14 +08:00
|
|
|
setNoteTypeMime,
|
2018-10-25 18:06:36 +08:00
|
|
|
getChildren,
|
2018-10-31 05:18:20 +08:00
|
|
|
getRelationMap,
|
|
|
|
changeTitle
|
2018-03-31 00:57:22 +08:00
|
|
|
};
|