implements a category route
This commit is contained in:
49
routes/category.js
Normal file
49
routes/category.js
Normal file
@@ -0,0 +1,49 @@
|
||||
const router = require('express').Router();
|
||||
const CategoryService = require('../services/CategoryService');
|
||||
const CategoryInstance = new CategoryService();
|
||||
|
||||
module.exports = (app) => {
|
||||
app.use('/api/category', router);
|
||||
|
||||
router.get('/', async (req, res, next) => {
|
||||
const { name } = req.query;
|
||||
|
||||
if (name) {
|
||||
try {
|
||||
const response = await CategoryInstance.getByName(name);
|
||||
res.status(200).send(response);
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
const response = await CategoryInstance.getAll();
|
||||
res.status(200).send(response);
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
router.get('/:productid', async (req, res, next) => {
|
||||
const { productid } = req.params;
|
||||
|
||||
try {
|
||||
const response = await CategoryInstance.getById(productid);
|
||||
res.status(200).send(response);
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
})
|
||||
|
||||
router.post('/', async (req, res, next) => {
|
||||
const data = req.body;
|
||||
|
||||
try {
|
||||
const response = await CategoryInstance.create(data);
|
||||
res.status(201).send(response);
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user