Add (partially-) working categoryManager.

- Added Body-Parser.
This commit is contained in:
2023-05-08 23:30:19 +02:00
parent 43ef7fd395
commit 7cfca9abac
5 changed files with 119 additions and 13 deletions

View File

@ -2,7 +2,10 @@ import express, { Request, Response } from 'express';
import { prisma, __path } from '../../index.js';
export default (req: Request, res: Response) => {
prisma.category
// If method is get, render the page
if (req.method === 'GET') {
// Render the page
prisma.category
.findMany({})
.then((items) => {
// Count amount of total items
@ -12,4 +15,28 @@ export default (req: Request, res: Response) => {
console.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
});
} else {
console.log(req.body);
// Check if required fields are filled in
if (!req.body.name || !req.body.description) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
return;
}
// Save data to category table
prisma.category.create({
data: {
name: req.body.name,
description: req.body.description,
},
})
.then(() => {
res.redirect('/allCategories');
})
.catch((err) => {
// TODO Catch if is a duplicate error and show a message to the user
// TODO Fix this as it throws an error on the error page
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
});
}
};