Notes/src/routes/api/attributes.js

238 lines
6.8 KiB
JavaScript
Raw Normal View History

"use strict";
const sql = require('../../services/sql');
const log = require('../../services/log');
const attributeService = require('../../services/attributes');
const repository = require('../../services/repository');
const Attribute = require('../../entities/attribute');
2021-05-02 19:59:16 +02:00
const becca = require("../../services/becca/becca");
2020-06-20 12:31:38 +02:00
function getEffectiveNoteAttributes(req) {
2021-05-02 11:23:58 +02:00
const note = becca.getNote(req.params.noteId);
2020-06-20 12:31:38 +02:00
return note.getAttributes();
}
2020-06-20 12:31:38 +02:00
function updateNoteAttribute(req) {
const noteId = req.params.noteId;
const body = req.body;
let attribute;
if (body.attributeId) {
2021-05-02 11:23:58 +02:00
attribute = becca.getAttribute(body.attributeId);
if (attribute.noteId !== noteId) {
return [400, `Attribute ${body.attributeId} is not owned by ${noteId}`];
}
if (body.type !== attribute.type
|| body.name !== attribute.name
|| (body.type === 'relation' && body.value !== attribute.value)) {
let newAttribute;
if (body.type !== 'relation' || !!body.value.trim()) {
newAttribute = attribute.createClone(body.type, body.name, body.value);
2020-06-20 12:31:38 +02:00
newAttribute.save();
}
attribute.markAsDeleted();
return {
attributeId: newAttribute ? newAttribute.attributeId : null
};
}
}
else {
2018-11-12 23:34:22 +01:00
if (body.type === 'relation' && !body.value.trim()) {
return {};
}
attribute = new Attribute();
attribute.noteId = noteId;
attribute.name = body.name;
attribute.type = body.type;
}
2020-10-23 00:11:44 +02:00
if (attribute.type === 'label' || body.value.trim()) {
2018-11-12 23:34:22 +01:00
attribute.value = body.value;
attribute.isDeleted = false;
2018-11-12 23:34:22 +01:00
}
else {
// relations should never have empty target
attribute.markAsDeleted();
2018-11-12 23:34:22 +01:00
}
2020-06-20 12:31:38 +02:00
attribute.save();
return {
attributeId: attribute.attributeId
};
}
function setNoteAttribute(req) {
const noteId = req.params.noteId;
const body = req.body;
let attr = repository.getEntity(`SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = ? AND name = ?`, [noteId, body.type, body.name]);
if (attr) {
attr.value = body.value;
} else {
attr = new Attribute(body);
attr.noteId = noteId;
}
attr.save();
}
function addNoteAttribute(req) {
const noteId = req.params.noteId;
const body = req.body;
const attr = new Attribute(body);
attr.noteId = noteId;
attr.save();
}
2020-06-20 12:31:38 +02:00
function deleteNoteAttribute(req) {
const noteId = req.params.noteId;
const attributeId = req.params.attributeId;
2021-04-26 22:24:55 +02:00
const attribute = becca.getAttribute(attributeId);
if (attribute) {
if (attribute.noteId !== noteId) {
return [400, `Attribute ${attributeId} is not owned by ${noteId}`];
}
attribute.markAsDeleted();
}
}
2020-07-04 10:29:17 +02:00
function updateNoteAttributes(req) {
const noteId = req.params.noteId;
const incomingAttributes = req.body;
2021-05-02 11:23:58 +02:00
const note = becca.getNote(noteId);
let existingAttrs = note.getOwnedAttributes();
let position = 0;
for (const incAttr of incomingAttributes) {
position += 10;
const perfectMatchAttr = existingAttrs.find(attr =>
attr.type === incAttr.type &&
attr.name === incAttr.name &&
attr.isInheritable === incAttr.isInheritable &&
attr.value === incAttr.value);
if (perfectMatchAttr) {
existingAttrs = existingAttrs.filter(attr => attr.attributeId !== perfectMatchAttr.attributeId);
if (perfectMatchAttr.position !== position) {
perfectMatchAttr.position = position;
2020-06-20 12:31:38 +02:00
perfectMatchAttr.save();
}
continue; // nothing to update
}
if (incAttr.type === 'relation') {
2021-05-02 11:23:58 +02:00
const targetNote = becca.getNote(incAttr.value);
if (!targetNote || targetNote.isDeleted) {
log.error(`Target note of relation ${JSON.stringify(incAttr)} does not exist or is deleted`);
continue;
}
}
const matchedAttr = existingAttrs.find(attr =>
attr.type === incAttr.type &&
attr.name === incAttr.name &&
attr.isInheritable === incAttr.isInheritable);
if (matchedAttr) {
matchedAttr.value = incAttr.value;
matchedAttr.position = position;
2020-06-20 12:31:38 +02:00
matchedAttr.save();
existingAttrs = existingAttrs.filter(attr => attr.attributeId !== matchedAttr.attributeId);
continue;
}
// no existing attribute has been matched so we need to create a new one
// type, name and isInheritable are immutable so even if there is an attribute with matching type & name, we need to create a new one and delete the former one
2020-06-20 12:31:38 +02:00
note.addAttribute(incAttr.type, incAttr.name, incAttr.value, incAttr.isInheritable, position);
}
// all the remaining existing attributes are not defined anymore and should be deleted
for (const toDeleteAttr of existingAttrs) {
if (!toDeleteAttr.isAutoLink()) {
toDeleteAttr.markAsDeleted();
}
}
}
2020-06-20 12:31:38 +02:00
function getAttributeNames(req) {
const type = req.query.type;
const query = req.query.query;
return attributeService.getAttributeNames(type, query);
}
2020-06-20 12:31:38 +02:00
function getValuesForAttribute(req) {
const attributeName = req.params.attributeName;
2020-06-20 12:31:38 +02:00
return sql.getColumn("SELECT DISTINCT value FROM attributes WHERE isDeleted = 0 AND name = ? AND type = 'label' AND value != '' ORDER BY value", [attributeName]);
}
2020-06-20 12:31:38 +02:00
function createRelation(req) {
const sourceNoteId = req.params.noteId;
const targetNoteId = req.params.targetNoteId;
const name = req.params.name;
2020-06-20 12:31:38 +02:00
let attribute = repository.getEntity(`SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [sourceNoteId, name, targetNoteId]);
if (!attribute) {
attribute = new Attribute();
attribute.noteId = sourceNoteId;
attribute.name = name;
attribute.type = 'relation';
attribute.value = targetNoteId;
2020-06-20 12:31:38 +02:00
attribute.save();
}
return attribute;
}
2020-06-20 12:31:38 +02:00
function deleteRelation(req) {
const sourceNoteId = req.params.noteId;
const targetNoteId = req.params.targetNoteId;
const name = req.params.name;
2020-06-20 12:31:38 +02:00
let attribute = repository.getEntity(`SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [sourceNoteId, name, targetNoteId]);
if (attribute) {
attribute.markAsDeleted();
}
}
module.exports = {
updateNoteAttributes,
updateNoteAttribute,
setNoteAttribute,
addNoteAttribute,
deleteNoteAttribute,
getAttributeNames,
getValuesForAttribute,
getEffectiveNoteAttributes,
createRelation,
deleteRelation
};