refactor: organise API around resources (#798)

This commit is contained in:
Carlos Valente
2024-03-02 23:08:08 +01:00
committed by GitHub
parent d87ba12b2e
commit 088927bbbb
104 changed files with 1741 additions and 1718 deletions
@@ -0,0 +1,49 @@
import { CustomField, CustomFields } from 'ontime-types';
import type { Request, Response } from 'express';
import {
createCustomField,
editCustomField,
getCustomFields as getCustomFieldsFromCache,
removeCustomField,
} from '../../services/rundown-service/rundownCache.js';
export async function getCustomFields(_req: Request, res: Response<CustomFields>) {
const customFields = getCustomFieldsFromCache();
res.json(customFields);
}
// Expects { label: <label> type: 'string | ..' }
export async function postCustomField(req: Request, res: Response) {
try {
const newField = req.body as CustomField;
const allFields = await createCustomField(newField);
res.status(201).send(allFields);
} catch (error) {
res.status(400).send({ message: error.toString() });
}
}
// Expects { label: <oldLabel>, field: { label: <newLabel> type: 'string | ..' } }
export async function putCustomField(req: Request, res: Response) {
try {
const oldLabel = req.params.label;
const { colour, type, label } = req.body;
const newFields = await editCustomField(oldLabel, { label, colour, type });
res.status(200).send(newFields);
} catch (error) {
res.status(400).send({ message: error.toString() });
}
}
// Expects { label: <label> }
export async function deleteCustomField(req: Request, res: Response) {
try {
const fieldToDelete = req.params.label;
await removeCustomField(fieldToDelete);
res.sendStatus(204);
} catch (error) {
res.status(400).send({ message: error.toString() });
}
}