49 lines
1.1 KiB
JavaScript
49 lines
1.1 KiB
JavaScript
const express = require('express');
|
|
const { check } = require('express-validator');
|
|
const {
|
|
getProducts,
|
|
getProduct,
|
|
createProduct,
|
|
updateProduct,
|
|
deleteProduct
|
|
} = require('../controllers/productController');
|
|
const { protect } = require('../middleware/auth');
|
|
|
|
const router = express.Router();
|
|
|
|
// Protect all routes
|
|
router.use(protect);
|
|
|
|
// Get all products
|
|
router.get('/', getProducts);
|
|
|
|
// Get single product
|
|
router.get('/:id', getProduct);
|
|
|
|
// Create product
|
|
router.post(
|
|
'/',
|
|
[
|
|
check('name', 'Product name is required').not().isEmpty(),
|
|
check('quantity', 'Quantity must be a number').isNumeric(),
|
|
check('description', 'Description is required').not().isEmpty()
|
|
],
|
|
createProduct
|
|
);
|
|
|
|
// Update product
|
|
router.put(
|
|
'/:id',
|
|
[
|
|
check('name', 'Product name is required').optional().not().isEmpty(),
|
|
check('quantity', 'Quantity must be a number').optional().isNumeric(),
|
|
check('description', 'Description is required').optional().not().isEmpty()
|
|
],
|
|
updateProduct
|
|
);
|
|
|
|
// Delete product
|
|
router.delete('/:id', deleteProduct);
|
|
|
|
module.exports = router;
|