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 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 // Render the page
if (req.method === 'GET') { prisma.itemCategory
// Render the page
prisma.itemCategory
.findMany({}) .findMany({})
.then((items) => { .then((items) => {
// Count amount of total items // Count amount of total items
@ -15,51 +13,55 @@ 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 { }
console.log(req.body);
// Check if required fields are filled in function post(req: Request, res: Response) {
if (!req.body.name || !req.body.description) { console.log(req.body);
res.status(400).render(__path + '/src/frontend/errors/400.eta.html'); // Check if required fields are filled in
return; 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 });
});
}
} }
}; 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) => { 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);