Compare commits

..

No commits in common. "b514e81764f4e940344f6b38e8b3bc679acbec02" and "2285b3dd33dfc465ded914bf40e087704e123efc" have entirely different histories.

2 changed files with 8 additions and 43 deletions

View File

@ -1,6 +0,0 @@
{
"$schema": "http://json.schemastore.org/vsls",
"gitignore": "hide",
"excludeFiles": ["!node_modules"],
"hideFiles": ["node_modules"]
}

View File

@ -48,43 +48,20 @@ function post(req: Request, res: Response) {
res.status(201).json({ status: 'created' }); res.status(201).json({ status: 'created' });
}) })
.catch((err) => { .catch((err) => {
// Check if an entry already exists. // TODO Catch if is a duplicate error and show a message to the user
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({ error: 'Category already exists.' });
} else {
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 });
}
}); });
} }
// Update category. // Update category.
async function patch(req: Request, res: Response) { function patch(req: Request, res: Response) {
// Check if required fields are present. // Check if required fields are present.
if (!req.body.id || !req.body.name) { if (!req.body.id || !req.body.name) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
return; return;
} }
// Check if the category id exists. 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
.update({ .update({
where: { where: {
@ -99,15 +76,9 @@ async function patch(req: Request, res: Response) {
res.status(201).json({ status: 'updated' }); res.status(201).json({ status: 'updated' });
}) })
.catch((err) => { .catch((err) => {
// Check if an entry already exists. // TODO Catch if is a duplicate error and show a message to the user
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({ error: 'Category already exists.' });
} else {
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 });
}
}); });
} }
@ -143,7 +114,7 @@ async function del(req: Request, res: Response) {
} }
}) })
.then(() => { .then(() => {
res.status(200).json({ status: 'deleted' }); res.status(201).json({ status: 'deleted' });
}) })
.catch((err) => { .catch((err) => {
log.db.error(err); log.db.error(err);