Migrated route / Fixed import

- Migrate categoryManager to new route schema.
- Fiex csv Import description.
This commit is contained in:
Leon Meier 2023-05-15 15:27:55 +02:00
parent b1a73ebd4a
commit 38cd29943f
3 changed files with 58 additions and 56 deletions

View File

@ -1,9 +1,7 @@
import express, { Request, Response } from 'express'; import express, { Request, Response } from 'express';
import { prisma, __path, log } from '../../../index.js'; import { prisma, __path, log } from '../../../index.js';
export default (req: Request, res: Response) => { function get(req: Request, res: Response) {
// If method is get, render the page
if (req.method === 'GET') {
// Render the page // Render the page
prisma.itemCategory prisma.itemCategory
.findMany({}) .findMany({})
@ -15,10 +13,12 @@ export default (req: Request, res: Response) => {
console.error(err); console.error(err);
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err }); res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
}); });
} else { }
function post(req: Request, res: Response) {
console.log(req.body); console.log(req.body);
// Check if required fields are filled in // Check if required fields are filled in
if (!req.body.name || !req.body.description) { if (!req.body.name) {
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
return; return;
} }
@ -26,11 +26,12 @@ export default (req: Request, res: Response) => {
if (!req.body.editCategoryModalIsEdit) { if (!req.body.editCategoryModalIsEdit) {
console.log('is not edit'); console.log('is not edit');
// Save data to category table // Save data to category table
prisma.itemCategory.create({ prisma.itemCategory
.create({
data: { data: {
name: req.body.name, name: req.body.name,
description: req.body.description, description: req.body.description
}, }
}) })
.then(() => { .then(() => {
res.redirect('categories'); res.redirect('categories');
@ -42,14 +43,15 @@ export default (req: Request, res: Response) => {
}); });
} else { } else {
// Save data to category table // Save data to category table
prisma.itemCategory.update({ prisma.itemCategory
.update({
where: { where: {
id: parseInt(req.body.editCategoryModalId) id: parseInt(req.body.editCategoryModalId)
}, },
data: { data: {
name: req.body.name, name: req.body.name,
description: req.body.description, description: req.body.description
}, }
}) })
.then(() => { .then(() => {
res.redirect('categories'); res.redirect('categories');
@ -62,4 +64,4 @@ export default (req: Request, res: Response) => {
} }
} }
}; export default { get, post };

View File

@ -42,7 +42,8 @@ function post(req: Request, res: Response) {
categories.forEach((category: string) => { categories.forEach((category: string) => {
const promise = prisma.itemCategory.create({ const promise = prisma.itemCategory.create({
data: { data: {
name: category name: category,
description: ''
} }
}); });
categoryPromises.push(promise); categoryPromises.push(promise);
@ -61,7 +62,7 @@ function post(req: Request, res: Response) {
comment: record.comment, comment: record.comment,
category: { category: {
connect: { connect: {
name: record.category name: record.category,
} }
}, },
SKU: record.sku, SKU: record.sku,

View File

@ -11,8 +11,7 @@ import startpageRoute from './startpage.js';
const Router = express.Router({ strict: false }); const Router = express.Router({ strict: false });
Router.route('/test').get(testRoute.get); Router.route('/test').get(testRoute.get);
Router.route('/categories').get(categoryManager.get).post(categoryManager.post);
Router.use('/categories', categoryManager); // TODO: Needs refactoring to new route format.
Router.route('/storages').get(storageManager.get); Router.route('/storages').get(storageManager.get);
Router.route('/import/csv').get(csvImportRoute.get).post(csvImportRoute.post); Router.route('/import/csv').get(csvImportRoute.get).post(csvImportRoute.post);
Router.route('/').get(startpageRoute.get); Router.route('/').get(startpageRoute.get);