253 lines
6.6 KiB
TypeScript
253 lines
6.6 KiB
TypeScript
import { Request, Response } from 'express';
|
|
import { prisma, __path, log } from '../../../index.js';
|
|
import { contactType } from '@prisma/client';
|
|
|
|
// Get storageUnit.
|
|
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.storageUnit
|
|
.findUnique({
|
|
where: {
|
|
id: parseInt(req.query.id.toString())
|
|
},
|
|
// Get contactInfo and StorageLocation from relation.
|
|
include: {
|
|
contactInfo: true,
|
|
StorageLocation: true
|
|
}
|
|
})
|
|
.then((items) => {
|
|
if (items) {
|
|
res.status(200).json(JSON.stringify(items));
|
|
} else {
|
|
res.status(410).json({ error: 'it seems that there is no storageUnit present.' });
|
|
}
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
|
|
});
|
|
} else {
|
|
prisma.storageUnit
|
|
.findMany({
|
|
// Get contactInfo and StorageLocation from relation.
|
|
include: {
|
|
contactInfo: true,
|
|
StorageLocation: true
|
|
}
|
|
})
|
|
.then((items) => {
|
|
if (items) {
|
|
res.status(200).json(JSON.stringify(items));
|
|
} else {
|
|
res.status(410).json({ error: 'storageUnit does not exist.' });
|
|
}
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
|
|
});
|
|
}
|
|
}
|
|
|
|
// Create storageUnit.
|
|
function post(req: Request, res: Response) {
|
|
// If the frontend wants to create a StorageUnit with non-existing ContactInfo.
|
|
if (req.body.locationId === 'META_CREATENEW') {
|
|
// 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');
|
|
return;
|
|
}
|
|
|
|
// Create storageUnit and location.
|
|
prisma.storageUnit
|
|
.create({
|
|
data: {
|
|
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
|
|
}
|
|
}
|
|
},
|
|
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: 'storageUnit already exists.' });
|
|
} else {
|
|
log.db.error(err);
|
|
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
|
|
}
|
|
});
|
|
} else {
|
|
// Check if required fields are present.
|
|
if (!req.body.name || !req.body.locationId) {
|
|
res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
|
|
return;
|
|
}
|
|
// Create storageUnit with existing location.
|
|
prisma.storageUnit
|
|
.create({
|
|
data: {
|
|
name: req.body.name,
|
|
contactInfo: {
|
|
connect: {
|
|
id: parseInt(req.body.locationId)
|
|
}
|
|
}
|
|
},
|
|
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: '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
|
|
.update({
|
|
where: {
|
|
id: parseInt(req.body.id)
|
|
},
|
|
data: {
|
|
name: req.body.name,
|
|
contactInfo: {
|
|
connect: {
|
|
id: parseInt(req.body.locationId)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
.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: 'storageUnit already exists.' });
|
|
} else {
|
|
log.db.error(err);
|
|
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
|
|
}
|
|
});
|
|
}
|
|
|
|
// Delete storageUnit.
|
|
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.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 });
|
|
}
|
|
|
|
prisma.storageUnit
|
|
.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 };
|