current state
This commit is contained in:
109
src/routes/api/v1/categories.ts
Normal file
109
src/routes/api/v1/categories.ts
Normal file
@ -0,0 +1,109 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { prisma, __path, log } from '../../../index.js';
|
||||
|
||||
// Get category.
|
||||
function get(req: Request, res: Response) {
|
||||
// Check if required fields are present.
|
||||
if (!req.query.name) {
|
||||
res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
|
||||
return;
|
||||
}
|
||||
|
||||
prisma.itemCategory
|
||||
.findUnique({
|
||||
where: {
|
||||
name: req.query.name.toString()
|
||||
}
|
||||
})
|
||||
.then((item) => {
|
||||
if (item) {
|
||||
res.status(200).json(JSON.stringify(item));
|
||||
} else {
|
||||
res.status(410).json({error: 'Item does not exist'});
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
res.status(500).render(__path + '/src/frontend/errors/dbError.eta.html', { error: err });
|
||||
});
|
||||
}
|
||||
|
||||
// Create category.
|
||||
function post(req: Request, res: Response) {
|
||||
// Check if required fields are present.
|
||||
if (!req.body.name) {
|
||||
res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
|
||||
return;
|
||||
}
|
||||
|
||||
// Save data.
|
||||
prisma.itemCategory
|
||||
.create({
|
||||
data: {
|
||||
name: req.body.name,
|
||||
description: req.body.description
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
res.status(201).json({ status: 'created' });
|
||||
})
|
||||
.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 });
|
||||
});
|
||||
}
|
||||
|
||||
// Update category.
|
||||
function patch(req: Request, res: Response) {
|
||||
// Check if required fields are present.
|
||||
if (!req.body.id || !req.body.name) {
|
||||
res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
|
||||
return;
|
||||
}
|
||||
|
||||
prisma.itemCategory
|
||||
.update({
|
||||
where: {
|
||||
id: parseInt(req.body.id)
|
||||
},
|
||||
data: {
|
||||
name: req.body.name,
|
||||
description: req.body.description
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
res.status(201).json({ status: 'updated' });
|
||||
})
|
||||
.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 });
|
||||
});
|
||||
}
|
||||
|
||||
// Delete category.
|
||||
function del(req: Request, res: Response) {
|
||||
// Check if required fields are present.
|
||||
if (!req.body.id) {
|
||||
res.status(400).render(__path + '/src/frontend/errors/400.eta.html');
|
||||
return;
|
||||
}
|
||||
|
||||
prisma.itemCategory
|
||||
.delete({
|
||||
where: {
|
||||
id: parseInt(req.body.id)
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
res.status(201).json({ status: 'deleted' });
|
||||
})
|
||||
.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, patch, del };
|
@ -2,11 +2,12 @@ import express from 'express';
|
||||
|
||||
// Route imports
|
||||
import testRoute from './test.js';
|
||||
import categoryRoute from './categories.js';
|
||||
|
||||
// Router base is '/api/v1'
|
||||
const Router = express.Router({ strict: false });
|
||||
|
||||
Router.route('/categories').get(categoryRoute.get).post(categoryRoute.post).patch(categoryRoute.patch).delete(categoryRoute.del);
|
||||
Router.route('/test').get(testRoute.get);
|
||||
|
||||
|
||||
export default Router;
|
||||
|
Reference in New Issue
Block a user