Implement storageLocation api

This commit is contained in:
Leon Meier 2023-05-16 22:19:36 +02:00
parent 90fc8068a0
commit 04b5bd60f2

View File

@ -0,0 +1,189 @@
import { Request, Response } from 'express';
import { prisma, __path, log } from '../../../index.js';
import { contactType } from '@prisma/client';
// Get storageLocation.
function get(req: Request, res: Response) {
if (req.query.getAll === undefined) {
// Check if required fields are present.
if (!req.query.id) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
return;
}
prisma.storageLocation
.findUnique({
where: {
id: parseInt(req.query.id.toString())
},
// Get storageUnit from relation.
include: {
storageUnit: true
}
})
.then((items) => {
if (items) {
res.status(200).json(JSON.stringify(items));
} else {
res.status(410).json({ error: 'it seems that there is no storageLocation present.' });
}
})
.catch((err) => {
console.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
});
} else {
prisma.storageLocation
.findMany({
// Get storageUnit from relation.
include: {
storageUnit: true
}
})
.then((items) => {
if (items) {
res.status(200).json(JSON.stringify(items));
} else {
res.status(410).json({ error: 'storageLocation does not exist.' });
}
})
.catch((err) => {
console.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
});
}
}
// Create storageLocation.
function post(req: Request, res: Response) {
// Check if required fields are present.
if (!req.body.name) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
return;
}
// Create storageLocation with existing storageUnit.
prisma.storageLocation
.create({
data: {
name: req.body.name,
storageUnitId: parseInt(req.body.storageUnitId) || undefined
},
select: {
id: true
}
})
.then((data) => {
res.status(201).json({ status: 'created', 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({ error: 'storageLocation already exists.' });
} else if (err.code == 'P2003') {
// P2003 -> "Foreign key constraint failed on the field: {field_name}"
// https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(404).json({ error: 'specified storageUnitId does not exist' });
} else {
log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
}
});
}
// Update storageLocation. -> 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.storageUnitId) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
return;
}
// Check if the storageLocation id exists. If not return 410 Gone.
try {
const result = await prisma.storageLocation.findUnique({
where: {
id: parseInt(req.body.id)
}
});
if (result === null) {
res.status(404).json({ error: 'storageLocation does not exist.' });
return;
}
} catch (err) {
log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
}
prisma.storageLocation
.update({
where: {
id: parseInt(req.body.id)
},
data: {
name: req.body.name,
storageUnitId: parseInt(req.body.storageUnitId) || undefined
}
})
.then(() => {
res.status(201).json({ status: 'updated' });
})
.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: 'storageLocation already exists.' });
} else if (err.code == 'P2003') {
// P2003 -> "Foreign key constraint failed on the field: {field_name}"
// https://www.prisma.io/docs/reference/api-reference/error-reference
res.status(404).json({ error: 'specified storageUnitId does not exist' });
} else {
log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
}
});
}
// Delete storageLocation.
async function del(req: Request, res: Response) {
// Check if required fields are present.
if (!req.body.id) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
return;
}
// Does the id exist? If not return 410 Gone.
try {
const result = await prisma.storageLocation.findUnique({
where: {
id: parseInt(req.body.id)
}
});
if (result === null) {
res.status(410).json({ error: 'storageLocation does not exist.' });
return;
}
} catch (err) {
log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
}
prisma.storageLocation
.delete({
where: {
id: parseInt(req.body.id)
}
})
.then(() => {
res.status(200).json({ status: 'deleted' });
})
.catch((err) => {
log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
});
}
export default { get, post, patch, del };