Unified error messages/structure / Added item api route.

- Introduce new error output structure to api routes.
- It is now possible to create new items via api.
This commit is contained in:
Leon Meier 2023-06-06 23:48:59 +02:00
parent ce31beb1a8
commit 3b1e4a7cde
4 changed files with 106 additions and 83 deletions

View File

@ -5,7 +5,7 @@ import { prisma, __path, log } from '../../../index.js';
function get(req: Request, res: Response) { function get(req: Request, res: Response) {
// Check if required fields are present. // Check if required fields are present.
if (!req.query.name) { if (!req.query.name) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
@ -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: 'Category does not exist.' }); res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Category does not exist' });
} }
}) })
.catch((err) => { .catch((err) => {
@ -32,7 +32,7 @@ function get(req: Request, res: Response) {
function post(req: Request, res: Response) { function post(req: Request, res: Response) {
// Check if required fields are present. // Check if required fields are present.
if (!req.body.name) { if (!req.body.name) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
@ -49,16 +49,17 @@ function post(req: Request, res: Response) {
}) })
.then((data) => { .then((data) => {
res.status(201).json({ status: 'created', id: data.id }); res.status(201).json({ status: 'created', id: data.id });
}) })
.catch((err) => { .catch((err) => {
// Check if an entry already exists. // Check if an entry already exists.
if (err.code === 'P2002') { if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}" // P2002 -> "Unique constraint failed on the {constraint}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(409).json({ error: 'Category already exists.' }); res.status(409).json({ errorcode: 'EXISTING', error: 'Category already exists' });
} else { } 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).json({ errorcode: 'DB_ERROR', error: err });
} }
}); });
} }
@ -67,7 +68,7 @@ function post(req: Request, res: Response) {
async function patch(req: Request, res: Response) { async 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).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
@ -80,12 +81,12 @@ async function patch(req: Request, res: Response) {
}); });
if (result === null) { if (result === null) {
res.status(410).json({ error: 'Category does not exist.' }); res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Category does not exist' });
return; return;
} }
} catch (err) { } catch (err) {
log.db.error(err); log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
} }
prisma.itemCategory prisma.itemCategory
@ -106,10 +107,10 @@ async function patch(req: Request, res: Response) {
if (err.code === 'P2002') { if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}" // P2002 -> "Unique constraint failed on the {constraint}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(409).json({ error: 'Category already exists.' }); res.status(409).json({ errorcode: 'EXISTING', error: 'Category already exists' });
} else { } 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).json({ errorcode: 'DB_ERROR', error: err });
} }
}); });
} }
@ -118,7 +119,7 @@ async function patch(req: Request, res: Response) {
async 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).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
@ -131,12 +132,12 @@ async function del(req: Request, res: Response) {
}); });
if (result === null) { if (result === null) {
res.status(410).json({ error: 'Category does not exist.' }); res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Category does not exist' });
return; return;
} }
} catch (err) { } catch (err) {
log.db.error(err); log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
} }
prisma.itemCategory prisma.itemCategory
@ -146,11 +147,11 @@ async function del(req: Request, res: Response) {
} }
}) })
.then(() => { .then(() => {
res.status(200).json({ status: 'deleted' }); res.status(200).json({ errorcode: 'DELETED', error: 'Sucessfully deleted entry' });
}) })
.catch((err) => { .catch((err) => {
log.db.error(err); log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
}); });
} }

View File

@ -1,12 +1,13 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { prisma, __path, log } from '../../../index.js'; import { prisma, __path, log } from '../../../index.js';
//import { itemStatus } from '@prisma/client';
// Get item. // Get item.
function get(req: Request, res: Response) { function get(req: Request, res: Response) {
if (req.query.getAll === undefined) { if (req.query.getAll === undefined) {
// Check if required fields are present. // Check if required fields are present.
if (!req.query.id) { if (!req.query.id) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
prisma.item prisma.item
@ -33,12 +34,12 @@ function get(req: Request, res: Response) {
if (items) { if (items) {
res.status(200).json(JSON.stringify(items)); res.status(200).json(JSON.stringify(items));
} else { } else {
res.status(410).json({ error: 'it seems that there is no item present' }); res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Item does not exist' });
} }
}) })
.catch((err) => { .catch((err) => {
console.error(err); console.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
}); });
} else { } else {
prisma.item prisma.item
@ -62,36 +63,53 @@ function get(req: Request, res: Response) {
if (items) { if (items) {
res.status(200).json(JSON.stringify(items)); res.status(200).json(JSON.stringify(items));
} else { } else {
res.status(410).json({ error: 'item does not exist' }); res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Item does not exist' });
} }
}) })
.catch((err) => { .catch((err) => {
console.error(err); console.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
}); });
} }
} }
// Create item. // Create item.
function post(req: Request, res: Response) { function post(req: Request, res: Response) {
/*
// Check if required fields are present. // Check if required fields are present.
if (!req.body.name) { if (!req.body.name) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
// Create storageLocation with existing storageUnit.
prisma.storageLocation prisma.item
.create({ .create({
data: { data: {
SKU: req.body.sku,
amount: req.body.ammount,
name: req.body.name, name: req.body.name,
storageUnitId: parseInt(req.body.storageUnitId) || undefined comment: req.body.comment,
}, status: req.body.status, //itemStatus.normal,
select: {
id: true // Relations
contactInfoId: req.body.contactInfoId,
categoryId: req.body.categoryId,
storageLocationId: req.body.storageLocationId,
manufacturer: req.body.manufacturer,
//contents: {
// connect: [{ id: 1 }, { id: 2 }, { id: 3 }]
//},
//baseItem: {
// connect: {
// id: req.body.baseitemId
// }
//},
createdBy: req.body.createdBy
} }
}) })
.then((data) => { .then((data) => {
// TODO: Check if id is returned correctly
res.status(201).json({ status: 'created', id: data.id }); res.status(201).json({ status: 'created', id: data.id });
}) })
.catch((err) => { .catch((err) => {
@ -99,25 +117,26 @@ function post(req: Request, res: Response) {
if (err.code === 'P2002') { if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}" // P2002 -> "Unique constraint failed on the {constraint}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(409).json({ error: 'storageLocation already exists.' }); res.status(409).json({ errorcode: 'EXISTING', error: 'storageLocation already exists' });
} else if (err.code == 'P2003') { } else if (err.code == 'P2003') {
// P2003 -> "Foreign key constraint failed on the field: {field_name}" // P2003 -> "Foreign key constraint failed on the field: {field_name}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(404).json({ error: 'specified storageUnitId does not exist' }); // FIXME: Is this errormessage right?
res.status(404).json({ errorcode: 'NOT_EXISTING', error: 'specified storageUnitId does not exist' });
} else { } 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).json({ errorcode: 'DB_ERROR', error: err });
} }
}); });
*/
} }
// Update storageLocation. -> Only existing contactInfo. // Update storageLocation. -> Only existing contactInfo.
async function patch(req: Request, res: Response) { async function patch(req: Request, res: Response) {
/* res.status(501).json({ errorcode: 'NOT_IMPLEMENTED', error: 'This endpoint is not yet implemented' });
/*
// Check if required fields are present. // Check if required fields are present.
if (!req.body.id || !req.body.name || !req.body.storageUnitId) { if (!req.body.id || !req.body.name || !req.body.storageUnitId) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
@ -130,12 +149,12 @@ async function patch(req: Request, res: Response) {
}); });
if (result === null) { if (result === null) {
res.status(404).json({ error: 'storageLocation does not exist.' }); res.status(404).json({ errorcode: 'NOT_EXISTING', error: 'specified storageUnitId does not exist' });
return; return;
} }
} catch (err) { } catch (err) {
log.db.error(err); log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
} }
prisma.storageLocation prisma.storageLocation
@ -156,14 +175,14 @@ async function patch(req: Request, res: Response) {
if (err.code === 'P2002') { if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}" // P2002 -> "Unique constraint failed on the {constraint}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(409).json({ error: 'storageLocation already exists.' }); res.status(409).json({ errorcode: 'EXISTING', error: 'storageLocation already exists' });
} else if (err.code == 'P2003') { } else if (err.code == 'P2003') {
// P2003 -> "Foreign key constraint failed on the field: {field_name}" // P2003 -> "Foreign key constraint failed on the field: {field_name}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(404).json({ error: 'specified storageUnitId does not exist' }); res.status(404).json({ error: 'specified storageUnitId does not exist' });
} else { } 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).json({ errorcode: 'DB_ERROR', error: err });
} }
}); });
*/ */
@ -173,7 +192,7 @@ async function patch(req: Request, res: Response) {
async 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).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
@ -186,12 +205,12 @@ async function del(req: Request, res: Response) {
}); });
if (result === null) { if (result === null) {
res.status(410).json({ error: 'item does not exist' }); res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'Item does not exist' });
return; return;
} }
} catch (err) { } catch (err) {
log.db.error(err); log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
} }
prisma.item prisma.item
@ -201,11 +220,11 @@ async function del(req: Request, res: Response) {
} }
}) })
.then(() => { .then(() => {
res.status(200).json({ status: 'deleted' }); res.status(200).json({ errorcode: 'DELETED', error: 'Sucessfully deleted entry' });
}) })
.catch((err) => { .catch((err) => {
log.db.error(err); log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
}); });
} }

View File

@ -6,7 +6,7 @@ function get(req: Request, res: Response) {
if (req.query.getAll === undefined) { if (req.query.getAll === undefined) {
// Check if required fields are present. // Check if required fields are present.
if (!req.query.id) { if (!req.query.id) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
prisma.storageLocation prisma.storageLocation
@ -23,12 +23,12 @@ function get(req: Request, res: Response) {
if (items) { if (items) {
res.status(200).json(JSON.stringify(items)); res.status(200).json(JSON.stringify(items));
} else { } else {
res.status(410).json({ error: 'it seems that there is no storageLocation present.' }); res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageLocation does not exist' });
} }
}) })
.catch((err) => { .catch((err) => {
console.error(err); console.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
}); });
} else { } else {
prisma.storageLocation prisma.storageLocation
@ -42,12 +42,12 @@ function get(req: Request, res: Response) {
if (items) { if (items) {
res.status(200).json(JSON.stringify(items)); res.status(200).json(JSON.stringify(items));
} else { } else {
res.status(410).json({ error: 'storageLocation does not exist.' }); res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageLocation does not exist' });
} }
}) })
.catch((err) => { .catch((err) => {
console.error(err); console.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
}); });
} }
} }
@ -56,7 +56,7 @@ function get(req: Request, res: Response) {
function post(req: Request, res: Response) { function post(req: Request, res: Response) {
// Check if required fields are present. // Check if required fields are present.
if (!req.body.name) { if (!req.body.name) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
// Create storageLocation with existing storageUnit. // Create storageLocation with existing storageUnit.
@ -78,14 +78,15 @@ function post(req: Request, res: Response) {
if (err.code === 'P2002') { if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}" // P2002 -> "Unique constraint failed on the {constraint}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(409).json({ error: 'storageLocation already exists.' }); res.status(409).json({ errorcode: 'EXISTING', error: 'storageLocation already exists' });
} else if (err.code == 'P2003') { } else if (err.code == 'P2003') {
// P2003 -> "Foreign key constraint failed on the field: {field_name}" // P2003 -> "Foreign key constraint failed on the field: {field_name}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
// FIXME: Is this errormessage right?
res.status(404).json({ error: 'specified storageUnitId does not exist' }); res.status(404).json({ error: 'specified storageUnitId does not exist' });
} else { } 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).json({ errorcode: 'DB_ERROR', error: err });
} }
}); });
} }
@ -94,7 +95,7 @@ function post(req: Request, res: Response) {
async function patch(req: Request, res: Response) { async 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 || !req.body.storageUnitId) { if (!req.body.id || !req.body.name || !req.body.storageUnitId) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
@ -112,7 +113,7 @@ async function patch(req: Request, res: Response) {
} }
} catch (err) { } catch (err) {
log.db.error(err); log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
} }
prisma.storageLocation prisma.storageLocation
@ -133,14 +134,15 @@ async function patch(req: Request, res: Response) {
if (err.code === 'P2002') { if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}" // P2002 -> "Unique constraint failed on the {constraint}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(409).json({ error: 'storageLocation already exists.' }); res.status(409).json({ errorcode: 'EXISTING', error: 'storageLocation already exists' });
} else if (err.code == 'P2003') { } else if (err.code == 'P2003') {
// P2003 -> "Foreign key constraint failed on the field: {field_name}" // P2003 -> "Foreign key constraint failed on the field: {field_name}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
// FIXME: Is this errormessage right?
res.status(404).json({ error: 'specified storageUnitId does not exist' }); res.status(404).json({ error: 'specified storageUnitId does not exist' });
} else { } 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).json({ errorcode: 'DB_ERROR', error: err });
} }
}); });
} }
@ -149,7 +151,7 @@ async function patch(req: Request, res: Response) {
async 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).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
@ -162,12 +164,12 @@ async function del(req: Request, res: Response) {
}); });
if (result === null) { if (result === null) {
res.status(410).json({ error: 'storageLocation does not exist.' }); res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageLocation does not exist' });
return; return;
} }
} catch (err) { } catch (err) {
log.db.error(err); log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
} }
prisma.storageLocation prisma.storageLocation
@ -177,11 +179,11 @@ async function del(req: Request, res: Response) {
} }
}) })
.then(() => { .then(() => {
res.status(200).json({ status: 'deleted' }); res.status(200).json({ errorcode: 'DELETED', error: 'Sucessfully deleted entry' });
}) })
.catch((err) => { .catch((err) => {
log.db.error(err); log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
}); });
} }

View File

@ -7,7 +7,7 @@ function get(req: Request, res: Response) {
if (req.query.getAll === undefined) { if (req.query.getAll === undefined) {
// Check if required fields are present. // Check if required fields are present.
if (!req.query.id) { if (!req.query.id) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
prisma.storageUnit prisma.storageUnit
@ -25,12 +25,12 @@ function get(req: Request, res: Response) {
if (items) { if (items) {
res.status(200).json(JSON.stringify(items)); res.status(200).json(JSON.stringify(items));
} else { } else {
res.status(410).json({ error: 'it seems that there is no storageUnit present.' }); res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageUnit does not exist' });
} }
}) })
.catch((err) => { .catch((err) => {
console.error(err); console.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
}); });
} else { } else {
prisma.storageUnit prisma.storageUnit
@ -45,12 +45,12 @@ function get(req: Request, res: Response) {
if (items) { if (items) {
res.status(200).json(JSON.stringify(items)); res.status(200).json(JSON.stringify(items));
} else { } else {
res.status(410).json({ error: 'storageUnit does not exist.' }); res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageUnit does not exist' });
} }
}) })
.catch((err) => { .catch((err) => {
console.error(err); console.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
}); });
} }
} }
@ -61,7 +61,7 @@ function post(req: Request, res: Response) {
if (req.body.locationId === 'META_CREATENEW') { if (req.body.locationId === 'META_CREATENEW') {
// Check if required fields are present. // Check if required fields are present.
if (!req.body.street || !req.body.houseNumber || !req.body.zipCode || !req.body.city || !req.body.country || !req.body.name) { if (!req.body.street || !req.body.houseNumber || !req.body.zipCode || !req.body.city || !req.body.country || !req.body.name) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
@ -93,16 +93,16 @@ function post(req: Request, res: Response) {
if (err.code === 'P2002') { if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}" // P2002 -> "Unique constraint failed on the {constraint}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(409).json({ error: 'storageUnit already exists.' }); res.status(409).json({ errorcode: 'EXISTING', error: 'storageUnit already exists' });
} else { } 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).json({ errorcode: 'DB_ERROR', error: err });
} }
}); });
} else { } else {
// Check if required fields are present. // Check if required fields are present.
if (!req.body.name || !req.body.locationId) { if (!req.body.name || !req.body.locationId) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
// Create storageUnit with existing location. // Create storageUnit with existing location.
@ -128,10 +128,10 @@ function post(req: Request, res: Response) {
if (err.code === 'P2002') { if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}" // P2002 -> "Unique constraint failed on the {constraint}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(409).json({ error: 'storageUnit already exists.' }); res.status(409).json({ errorcode: 'EXISTING', error: 'storageUnit already exists' });
} else { } 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).json({ errorcode: 'DB_ERROR', error: err });
} }
}); });
} }
@ -141,7 +141,7 @@ function post(req: Request, res: Response) {
async function patch(req: Request, res: Response) { async 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 || !req.body.locationId) { if (!req.body.id || !req.body.name || !req.body.locationId) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); res.status(400).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
@ -154,12 +154,12 @@ async function patch(req: Request, res: Response) {
}); });
if (result === null) { if (result === null) {
res.status(410).json({ error: 'storageUnit does not exist.' }); res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageUnit does not exist' });
return; return;
} }
} catch (err) { } catch (err) {
log.db.error(err); log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
} }
// Check if the locationId(contactInfo) exists. If not return 410 Gone. // Check if the locationId(contactInfo) exists. If not return 410 Gone.
@ -171,12 +171,12 @@ async function patch(req: Request, res: Response) {
}); });
if (result === null) { if (result === null) {
res.status(410).json({ error: 'locationId does not exist.' }); res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'locationId does not exist' });
return; return;
} }
} catch (err) { } catch (err) {
log.db.error(err); log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
} }
prisma.storageUnit prisma.storageUnit
@ -201,10 +201,10 @@ async function patch(req: Request, res: Response) {
if (err.code === 'P2002') { if (err.code === 'P2002') {
// P2002 -> "Unique constraint failed on the {constraint}" // P2002 -> "Unique constraint failed on the {constraint}"
// https://www.prisma.io/docs/reference/api-reference/error-reference // https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(409).json({ error: 'storageUnit already exists.' }); res.status(409).json({ errorcode: 'EXISTING', error: 'storageUnit already exists' });
} else { } 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).json({ errorcode: 'DB_ERROR', error: err });
} }
}); });
} }
@ -213,7 +213,7 @@ async function patch(req: Request, res: Response) {
async 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).json({ errorcode: 'VALIDATION_ERROR', error: 'One or more required fields are missing' });
return; return;
} }
@ -226,12 +226,13 @@ async function del(req: Request, res: Response) {
}); });
if (result === null) { if (result === null) {
res.status(410).json({ error: 'storageUnit does not exist.' }); res.status(410).json({ errorcode: 'NOT_EXISTING', error: 'storageUnit does not exist' });
return; return;
} }
} catch (err) { } catch (err) {
log.db.error(err); log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
} }
prisma.storageUnit prisma.storageUnit
@ -241,11 +242,11 @@ async function del(req: Request, res: Response) {
} }
}) })
.then(() => { .then(() => {
res.status(200).json({ status: 'deleted' }); res.status(200).json({ errorcode: 'DELETED', error: 'Sucessfully deleted entry' });
}) })
.catch((err) => { .catch((err) => {
log.db.error(err); log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).json({ errorcode: 'DB_ERROR', error: err });
}); });
} }