Added validation for category api route.

This commit is contained in:
Leon Meier 2023-05-15 19:07:13 +02:00
parent 3b9813a680
commit 6afdb4fcdd

View File

@ -19,7 +19,7 @@ function get(req: Request, res: Response) {
if (item) { if (item) {
res.status(200).json(JSON.stringify(item)); res.status(200).json(JSON.stringify(item));
} else { } else {
res.status(410).json({error: 'Item does not exist'}); res.status(410).json({ error: 'Category does not exist.' });
} }
}) })
.catch((err) => { .catch((err) => {
@ -83,13 +83,30 @@ function patch(req: Request, res: Response) {
} }
// Delete category. // Delete category.
function del(req: Request, res: Response) { async function del(req: Request, res: Response) {
// Check if required fields are present. // Check if required fields are present.
if (!req.body.id) { if (!req.body.id) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
return; return;
} }
// Does the id exist? If not return 410 Gone.
try {
const result = await prisma.itemCategory.findUnique({
where: {
id: parseInt(req.body.id)
}
});
if (result === null) {
res.status(410).json({ error: 'Category does not exist.' });
return;
}
} catch (err) {
log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
}
prisma.itemCategory prisma.itemCategory
.delete({ .delete({
where: { where: {
@ -100,7 +117,6 @@ function del(req: Request, res: Response) {
res.status(201).json({ status: 'deleted' }); res.status(201).json({ status: 'deleted' });
}) })
.catch((err) => { .catch((err) => {
// TODO Catch if is a duplicate error and show a message to the user
log.db.error(err); log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
}); });