Add current state

This commit is contained in:
2023-05-16 00:05:39 +02:00
parent 3819d007a5
commit 532b7b9fe2
9 changed files with 374 additions and 40 deletions

View File

@ -3,11 +3,13 @@ import express from 'express';
// Route imports
import testRoute from './test.js';
import categoryRoute from './categories.js';
import storageUnitRoute from './storageUnits.js';
// Router base is '/api/v1'
const Router = express.Router({ strict: false });
Router.route('/categories').get(categoryRoute.get).post(categoryRoute.post).patch(categoryRoute.patch).delete(categoryRoute.del);
Router.route('/storageUnits').get(storageUnitRoute.get).post(storageUnitRoute.post).patch(storageUnitRoute.patch).delete(storageUnitRoute.del);
Router.route('/test').get(testRoute.get);
export default Router;

View File

@ -0,0 +1,148 @@
import { Request, Response } from 'express';
import { prisma, __path, log } from '../../../index.js';
// Get storageUnit.
function get(req: Request, res: Response) {
if (req.query.getAll === undefined) {
// Check if required fields are present.
if (!req.query.name) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
return;
}
prisma.storageUnit
.findUnique({
where: {
name: req.query.name.toString()
},
// Get category name from relation.
include: {
contactInfo: 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 category name from relation.
include: {
contactInfo: 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) {
log.web.debug(JSON.stringify(req.body));
/* // Check if required fields are present.
if (!req.body.name) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
return;
}
// Save data.
prisma.storageUnit
.create({
data: {
name: req.body.name
}
})
.then(() => {
res.status(201).json({ status: 'created' });
})
.catch((err) => {
// TODO Catch if is a duplicate error and show a message to the user
log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
}); */
}
// Update category.
function patch(req: Request, res: Response) {
// Check if required fields are present.
if (!req.body.id || !req.body.name) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
return;
}
prisma.storageUnit
.update({
where: {
id: parseInt(req.body.id)
},
data: {
name: req.body.name
}
})
.then(() => {
res.status(201).json({ status: 'updated' });
})
.catch((err) => {
// TODO Catch if is a duplicate error and show a message to the user
log.db.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
});
}
// Delete category.
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: 'Storage Unit 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(201).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 };