From 7cfca9abacb0201f194d39227cd73765f638046e Mon Sep 17 00:00:00 2001 From: Spacelord Date: Mon, 8 May 2023 23:30:19 +0200 Subject: [PATCH] Add (partially-) working categoryManager. - Added Body-Parser. --- package-lock.json | 54 ++++++++++++++++++++++---- package.json | 1 + src/frontend/categoryManager.eta.html | 40 +++++++++++++++++-- src/index.ts | 8 ++++ src/routes/frontend/categoryManager.ts | 29 +++++++++++++- 5 files changed, 119 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index 93e94a6..b87ea47 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@popperjs/core": "^2.11.7", "@prisma/client": "^4.13.0", + "body-parser": "^1.20.2", "bootstrap": "^5.3.0-alpha3", "bootstrap-icons": "^1.10.5", "csv": "^6.2.11", @@ -1108,12 +1109,12 @@ } }, "node_modules/body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "version": "1.20.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", + "integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==", "dependencies": { "bytes": "3.1.2", - "content-type": "~1.0.4", + "content-type": "~1.0.5", "debug": "2.6.9", "depd": "2.0.0", "destroy": "1.2.0", @@ -1121,7 +1122,7 @@ "iconv-lite": "0.4.24", "on-finished": "2.4.1", "qs": "6.11.0", - "raw-body": "2.5.1", + "raw-body": "2.5.2", "type-is": "~1.6.18", "unpipe": "1.0.0" }, @@ -2052,6 +2053,43 @@ "node": ">=12.0.0" } }, + "node_modules/express/node_modules/body-parser": { + "version": "1.20.1", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", + "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "dependencies": { + "bytes": "3.1.2", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "on-finished": "2.4.1", + "qs": "6.11.0", + "raw-body": "2.5.1", + "type-is": "~1.6.18", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8", + "npm": "1.2.8000 || >= 1.4.16" + } + }, + "node_modules/express/node_modules/raw-body": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", + "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "dependencies": { + "bytes": "3.1.2", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", @@ -4409,9 +4447,9 @@ } }, "node_modules/raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz", + "integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==", "dependencies": { "bytes": "3.1.2", "http-errors": "2.0.0", diff --git a/package.json b/package.json index 51510ae..eefcc18 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "dependencies": { "@popperjs/core": "^2.11.7", "@prisma/client": "^4.13.0", + "body-parser": "^1.20.2", "bootstrap": "^5.3.0-alpha3", "bootstrap-icons": "^1.10.5", "csv": "^6.2.11", diff --git a/src/frontend/categoryManager.eta.html b/src/frontend/categoryManager.eta.html index fa21e16..81b5d2c 100644 --- a/src/frontend/categoryManager.eta.html +++ b/src/frontend/categoryManager.eta.html @@ -5,10 +5,41 @@
+ Create new category +
- + + + + @@ -23,7 +54,8 @@ - + + <% }) %>
<%= user.id %> <%= user.name %><%= user.description %>
<%= user.description %>
diff --git a/src/index.ts b/src/index.ts index 26f0358..b49f120 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,7 @@ import fileUpload from 'express-fileupload'; import { PrismaClient } from '@prisma/client'; import { Status, Category } from '@prisma/client'; import * as eta from 'eta'; +import bodyParser from 'body-parser'; import routes from './routes/index.js'; @@ -46,6 +47,13 @@ export const app = express(); app.set('x-powered-by', false); app.set('strict routing', true); app.engine('html', eta.renderFile); + +// app.use(cors()); +app.use(bodyParser.urlencoded({ extended: false })); + +// Using bodyParser to parse JSON bodies into JS objects +app.use(bodyParser.json()); + app.use(fileUpload()); // Configure static https://expressjs.com/de/starter/static-files.html // app.use('/static', express.static('public')); diff --git a/src/routes/frontend/categoryManager.ts b/src/routes/frontend/categoryManager.ts index c15f823..6f87442 100644 --- a/src/routes/frontend/categoryManager.ts +++ b/src/routes/frontend/categoryManager.ts @@ -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 }); + }); + } + };