Implemented storageUnit api / Added error handling

- AFLOW-13
- Added check if an category entry already exists.
- Update precheck if all IDs are existing
This commit is contained in:
Leon Meier 2023-05-16 17:47:32 +02:00
parent b514e81764
commit e4295493f2

View File

@ -1,18 +1,19 @@
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 { contactType } from '@prisma/client';
// Get storageUnit. // Get storageUnit.
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.name) { if (!req.query.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;
} }
prisma.storageUnit prisma.storageUnit
.findUnique({ .findUnique({
where: { where: {
name: req.query.name.toString() id: parseInt(req.query.id.toString())
}, },
// Get category name from relation. // Get category name from relation.
include: { include: {
@ -54,37 +55,121 @@ function get(req: Request, res: Response) {
// Create storageUnit. // Create storageUnit.
function post(req: Request, res: Response) { function post(req: Request, res: Response) {
log.web.debug(JSON.stringify(req.body)); // If the frontend wants to create a StorageUnit with non-existing ContactInfo.
/* // Check if required fields are present. if (req.body.locationId === 'META_CREATENEW') {
if (!req.body.name) { // 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) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
return; return;
} }
// Save data. // Create storageUnit and location.
prisma.storageUnit prisma.storageUnit
.create({ .create({
data: { data: {
name: req.body.name name: req.body.name,
contactInfo: {
create: {
type: contactType.storageUnit,
street: req.body.street,
houseNumber: req.body.houseNumber,
zipCode: req.body.zipCode,
city: req.body.city,
country: req.body.country
}
}
} }
}) })
.then(() => { .then(() => {
res.status(201).json({ status: 'created' }); res.status(201).json({ status: 'created' });
}) })
.catch((err) => { .catch((err) => {
// TODO Catch if is a duplicate error and show a message to the user // 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({ error: 'storageUnit 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. } else {
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.name || !req.body.locationId) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
return; return;
} }
// Create storageUnit with existing location.
prisma.storageUnit
.create({
data: {
name: req.body.name,
contactInfo: {
connect: {
id: parseInt(req.body.locationId)
}
}
}
})
.then(() => {
res.status(201).json({ status: 'created' });
})
.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({ error: 'storageUnit already exists.' });
} else {
log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
}
});
}
}
// Update storageUnit. -> Only existing contactInfo.
async function patch(req: Request, res: Response) {
// Check if required fields are present.
if (!req.body.id || !req.body.name || !req.body.locationId) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
return;
}
// Check if the storageUnit id exists. If not return 410 Gone.
try {
const result = await prisma.storageUnit.findUnique({
where: {
id: parseInt(req.body.id)
}
});
if (result === null) {
res.status(410).json({ error: 'storageUnit does not exist.' });
return;
}
} catch (err) {
log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
}
// Check if the locationId(contactInfo) exists. If not return 410 Gone.
try {
const result = await prisma.contactInfo.findUnique({
where: {
id: parseInt(req.body.locationId)
}
});
if (result === null) {
res.status(410).json({ error: 'locationId does not exist.' });
return;
}
} catch (err) {
log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
}
prisma.storageUnit prisma.storageUnit
.update({ .update({
@ -92,20 +177,31 @@ function patch(req: Request, res: Response) {
id: parseInt(req.body.id) id: parseInt(req.body.id)
}, },
data: { data: {
name: req.body.name name: req.body.name,
contactInfo: {
connect: {
id: parseInt(req.body.locationId)
}
}
} }
}) })
.then(() => { .then(() => {
res.status(201).json({ status: 'updated' }); res.status(201).json({ status: 'updated' });
}) })
.catch((err) => { .catch((err) => {
// TODO Catch if is a duplicate error and show a message to the user // 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({ error: 'storageUnit 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 });
}
}); });
} }
// Delete category. // Delete storageUnit.
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) {
@ -122,7 +218,7 @@ async function del(req: Request, res: Response) {
}); });
if (result === null) { if (result === null) {
res.status(410).json({ error: 'Storage Unit does not exist.' }); res.status(410).json({ error: 'storageUnit does not exist.' });
return; return;
} }
} catch (err) { } catch (err) {
@ -137,7 +233,7 @@ async function del(req: Request, res: Response) {
} }
}) })
.then(() => { .then(() => {
res.status(201).json({ status: 'deleted' }); res.status(200).json({ status: 'deleted' });
}) })
.catch((err) => { .catch((err) => {
log.db.error(err); log.db.error(err);