Notes/src/etapi/attributes.js

65 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-01-07 19:33:59 +01:00
const becca = require("../becca/becca");
2022-01-10 17:09:20 +01:00
const eu = require("./etapi_utils");
2022-01-07 19:33:59 +01:00
const mappers = require("./mappers");
const attributeService = require("../services/attributes");
2022-01-10 17:09:20 +01:00
const validators = require("./validators");
2022-01-07 19:33:59 +01:00
function register(router) {
2022-01-10 17:09:20 +01:00
eu.route(router, 'get', '/etapi/attributes/:attributeId', (req, res, next) => {
const attribute = eu.getAndCheckAttribute(req.params.attributeId);
2022-01-07 19:33:59 +01:00
res.json(mappers.mapAttributeToPojo(attribute));
});
2022-01-10 17:09:20 +01:00
eu.route(router, 'post' ,'/etapi/attributes', (req, res, next) => {
2022-01-07 19:33:59 +01:00
const params = req.body;
2022-01-10 17:09:20 +01:00
eu.getAndCheckNote(params.noteId);
2022-01-07 19:33:59 +01:00
if (params.type === 'relation') {
2022-01-10 17:09:20 +01:00
eu.getAndCheckNote(params.value);
2022-01-07 19:33:59 +01:00
}
if (params.type !== 'relation' && params.type !== 'label') {
2022-01-10 17:09:20 +01:00
throw new eu.EtapiError(400, eu.GENERIC_CODE, `Only "relation" and "label" are supported attribute types, "${params.type}" given.`);
2022-01-07 19:33:59 +01:00
}
try {
const attr = attributeService.createAttribute(params);
res.json(mappers.mapAttributeToPojo(attr));
}
catch (e) {
2022-01-10 17:09:20 +01:00
throw new eu.EtapiError(400, eu.GENERIC_CODE, e.message);
2022-01-07 19:33:59 +01:00
}
});
const ALLOWED_PROPERTIES_FOR_PATCH = {
'value': validators.isString
};
2022-01-10 17:09:20 +01:00
eu.route(router, 'patch' ,'/etapi/attributes/:attributeId', (req, res, next) => {
const attribute = eu.getAndCheckAttribute(req.params.attributeId);
2022-01-07 19:33:59 +01:00
2022-01-10 17:09:20 +01:00
eu.validateAndPatch(attribute, req.body, ALLOWED_PROPERTIES_FOR_PATCH);
2022-01-07 19:33:59 +01:00
res.json(mappers.mapAttributeToPojo(attribute));
});
2022-01-10 17:09:20 +01:00
eu.route(router, 'delete' ,'/etapi/attributes/:attributeId', (req, res, next) => {
2022-01-07 19:33:59 +01:00
const attribute = becca.getAttribute(req.params.attributeId);
2022-01-08 12:01:54 +01:00
if (!attribute || attribute.isDeleted) {
2022-01-07 19:33:59 +01:00
return res.sendStatus(204);
}
attribute.markAsDeleted();
res.sendStatus(204);
});
}
module.exports = {
register
2022-01-08 12:01:54 +01:00
};