Notes/src/etapi/attributes.ts

87 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-04-07 15:15:50 +03:00
import becca = require('../becca/becca');
import eu = require('./etapi_utils');
import mappers = require('./mappers');
import attributeService = require('../services/attributes');
import v = require('./validators');
import { Router } from 'express';
import { AttributeRow } from '../becca/entities/rows';
import { ValidatorMap } from './etapi-interface';
2024-04-07 15:15:50 +03:00
function register(router: 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));
});
2024-04-07 15:15:50 +03:00
const ALLOWED_PROPERTIES_FOR_CREATE_ATTRIBUTE: ValidatorMap = {
2022-01-12 19:32:23 +01:00
'attributeId': [v.mandatory, v.notNull, v.isValidEntityId],
'noteId': [v.mandatory, v.notNull, v.isNoteId],
'type': [v.mandatory, v.notNull, v.isAttributeType],
'name': [v.mandatory, v.notNull, v.isString],
'value': [v.notNull, v.isString],
'isInheritable': [v.notNull, v.isBoolean],
'position': [v.notNull, v.isInteger]
2022-01-12 19:32:23 +01:00
};
2024-04-07 14:56:22 +03:00
eu.route(router, 'post', '/etapi/attributes', (req, res, next) => {
2022-01-12 19:32:23 +01:00
if (req.body.type === 'relation') {
eu.getAndCheckNote(req.body.value);
2022-01-07 19:33:59 +01:00
}
2024-04-07 15:15:50 +03:00
const _params = {};
eu.validateAndPatch(_params, req.body, ALLOWED_PROPERTIES_FOR_CREATE_ATTRIBUTE);
const params: AttributeRow = _params as AttributeRow;
2022-01-07 19:33:59 +01:00
try {
const attr = attributeService.createAttribute(params);
res.status(201).json(mappers.mapAttributeToPojo(attr));
2022-01-07 19:33:59 +01:00
}
2024-04-07 15:15:50 +03:00
catch (e: any) {
2022-01-12 19:32:23 +01:00
throw new eu.EtapiError(500, eu.GENERIC_CODE, e.message);
2022-01-07 19:33:59 +01:00
}
});
const ALLOWED_PROPERTIES_FOR_PATCH_LABEL = {
'value': [v.notNull, v.isString],
'position': [v.notNull, v.isInteger]
2022-01-07 19:33:59 +01:00
};
const ALLOWED_PROPERTIES_FOR_PATCH_RELATION = {
'position': [v.notNull, v.isInteger]
};
2024-04-07 14:56:22 +03:00
eu.route(router, 'patch', '/etapi/attributes/:attributeId', (req, res, next) => {
2022-01-10 17:09:20 +01:00
const attribute = eu.getAndCheckAttribute(req.params.attributeId);
2022-01-07 19:33:59 +01:00
if (attribute.type === 'label') {
eu.validateAndPatch(attribute, req.body, ALLOWED_PROPERTIES_FOR_PATCH_LABEL);
} else if (attribute.type === 'relation') {
2022-01-12 19:32:23 +01:00
eu.getAndCheckNote(req.body.value);
eu.validateAndPatch(attribute, req.body, ALLOWED_PROPERTIES_FOR_PATCH_RELATION);
}
2022-01-12 19:32:23 +01:00
attribute.save();
2022-01-07 19:33:59 +01:00
res.json(mappers.mapAttributeToPojo(attribute));
});
2024-04-07 14:56:22 +03: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);
2023-06-05 09:23:42 +02:00
if (!attribute) {
2022-01-07 19:33:59 +01:00
return res.sendStatus(204);
}
attribute.markAsDeleted();
res.sendStatus(204);
});
}
2024-04-07 15:15:50 +03:00
export = {
2022-01-07 19:33:59 +01:00
register
2022-01-08 12:01:54 +01:00
};