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,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 };

View File

@ -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,

View File

@ -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);