Fixed AFLOW-26 and AFLOW-14 in categories API route

This commit is contained in:
Sören Oesterwind 2023-07-09 22:29:52 +02:00
parent 3f55b22ede
commit 3be376b214

View File

@ -5,7 +5,7 @@ import { prisma, __path, log } from '../../../index.js';
function get(req: Request, res: Response) {
// Check if required fields are present.
if (!req.query.name) {
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'One or more required fields are missing' });
return;
}
@ -19,12 +19,12 @@ function get(req: Request, res: Response) {
if (item) {
res.status(200).json(item);
} else {
res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Category does not exist' });
res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Category does not exist' });
}
})
.catch((err) => {
log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err });
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
});
}
@ -32,7 +32,7 @@ function get(req: Request, res: Response) {
function post(req: Request, res: Response) {
// Check if required fields are present.
if (!req.body.name) {
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'One or more required fields are missing' });
return;
}
@ -48,18 +48,17 @@ function post(req: Request, res: Response) {
}
})
.then((data) => {
res.status(201).json({ status: 'created', id: data.id });
res.status(201).json({ status: 'CREATED', message: 'Successfully created category', id: data.id });
})
.catch((err) => {
// Check if an entry already exists.
if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}"
// https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(409).json({ errorcode: 'EXISTING', error: 'Category already exists' });
res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'Category already exists' });
} else {
log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err });
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
}
});
}
@ -68,7 +67,7 @@ function post(req: Request, res: Response) {
async function patch(req: Request, res: Response) {
// Check if required fields are present.
if (!req.body.id || !req.body.name) {
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'One or more required fields are missing' });
return;
}
@ -81,12 +80,12 @@ async function patch(req: Request, res: Response) {
});
if (result === null) {
res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Category does not exist' });
res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Category does not exist' });
return;
}
} catch (err) {
log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err });
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
}
prisma.itemCategory
@ -97,20 +96,23 @@ async function patch(req: Request, res: Response) {
data: {
name: req.body.name,
description: req.body.description
},
select: {
id: true
}
})
.then(() => {
res.status(201).json({ status: 'updated' });
.then((data) => {
res.status(201).json({ status: 'UPDATED', message: 'Successfully updated category', id: data.id });
})
.catch((err) => {
// Check if an entry already exists.
if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}"
// https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(409).json({ errorcode: 'EXISTING', error: 'Category already exists' });
res.status(409).json({ status: 'ERROR', errorcode: 'EXISTING', message: 'Category already exists' });
} else {
log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err });
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
}
});
}
@ -119,7 +121,7 @@ async function patch(req: Request, res: Response) {
async function del(req: Request, res: Response) {
// Check if required fields are present.
if (!req.body.id) {
res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: 'One or more required fields are missing' });
return;
}
@ -132,12 +134,12 @@ async function del(req: Request, res: Response) {
});
if (result === null) {
res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Category does not exist' });
res.status(410).json({ status: 'ERROR', errorcode: 'NOT_EXISTING', message: 'Category does not exist' });
return;
}
} catch (err) {
log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err });
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
}
prisma.itemCategory
@ -147,11 +149,11 @@ async function del(req: Request, res: Response) {
}
})
.then(() => {
res.status(200).json({ errorcode: 'DELETED', error: 'Sucessfully deleted entry' });
res.status(200).json({ status: 'DELETED', message: 'Successfully deleted category' });
})
.catch((err) => {
log.db.error(err);
res.status(500).json({ errorcode: 'DB_ERROR', error: err });
res.status(500).json({ status: 'ERROR', errorcode: 'DB_ERROR', error: err, message: 'An error occurred during the database operation' });
});
}