diff --git a/src/routes/frontend/manage/categoryManager.ts b/src/routes/frontend/manage/categoryManager.ts index a5f62d8..89e6d4a 100644 --- a/src/routes/frontend/manage/categoryManager.ts +++ b/src/routes/frontend/manage/categoryManager.ts @@ -1,11 +1,9 @@ import express, { Request, Response } from 'express'; import { prisma, __path, log } from '../../../index.js'; -export default (req: Request, res: Response) => { - // If method is get, render the page - if (req.method === 'GET') { - // Render the page - prisma.itemCategory +function get(req: Request, res: Response) { + // Render the page + prisma.itemCategory .findMany({}) .then((items) => { // Count amount of total items @@ -15,51 +13,55 @@ 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; - } - - if(!req.body.editCategoryModalIsEdit) { - console.log('is not edit'); - // Save data to category table - prisma.itemCategory.create({ - data: { - name: req.body.name, - description: req.body.description, - }, - }) - .then(() => { - res.redirect('categories'); - }) - .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 }); - }); - } else { - // Save data to category table - prisma.itemCategory.update({ - where: { - id: parseInt(req.body.editCategoryModalId) - }, - data: { - name: req.body.name, - description: req.body.description, - }, - }) - .then(() => { - res.redirect('categories'); - }) - .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 }); - }); - } +} + +function post(req: Request, res: Response) { + console.log(req.body); + // Check if required fields are filled in + if (!req.body.name) { + res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); + return; } - -}; + + if (!req.body.editCategoryModalIsEdit) { + console.log('is not edit'); + // Save data to category table + prisma.itemCategory + .create({ + data: { + name: req.body.name, + description: req.body.description + } + }) + .then(() => { + res.redirect('categories'); + }) + .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 }); + }); + } else { + // Save data to category table + prisma.itemCategory + .update({ + where: { + id: parseInt(req.body.editCategoryModalId) + }, + data: { + name: req.body.name, + description: req.body.description + } + }) + .then(() => { + res.redirect('categories'); + }) + .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 }); + }); + } +} + +export default { get, post }; diff --git a/src/routes/frontend/manage/import/csvImport.ts b/src/routes/frontend/manage/import/csvImport.ts index 2631316..5ec3774 100644 --- a/src/routes/frontend/manage/import/csvImport.ts +++ b/src/routes/frontend/manage/import/csvImport.ts @@ -42,7 +42,8 @@ function post(req: Request, res: Response) { categories.forEach((category: string) => { const promise = prisma.itemCategory.create({ data: { - name: category + name: category, + description: '' } }); categoryPromises.push(promise); @@ -61,7 +62,7 @@ function post(req: Request, res: Response) { comment: record.comment, category: { connect: { - name: record.category + name: record.category, } }, SKU: record.sku, diff --git a/src/routes/frontend/manage/index.ts b/src/routes/frontend/manage/index.ts index ebc74bf..8230e36 100644 --- a/src/routes/frontend/manage/index.ts +++ b/src/routes/frontend/manage/index.ts @@ -11,8 +11,7 @@ import startpageRoute from './startpage.js'; const Router = express.Router({ strict: false }); Router.route('/test').get(testRoute.get); - -Router.use('/categories', categoryManager); // TODO: Needs refactoring to new route format. +Router.route('/categories').get(categoryManager.get).post(categoryManager.post); Router.route('/storages').get(storageManager.get); Router.route('/import/csv').get(csvImportRoute.get).post(csvImportRoute.post); Router.route('/').get(startpageRoute.get);