|
1 | 1 | const express = require('express');
|
2 | 2 | const logger = require('../../libraries/log/logger');
|
| 3 | +const { AppError } = require('../../libraries/error-handling/AppError'); |
3 | 4 |
|
4 | 5 | const {
|
5 | 6 | createProduct,
|
6 |
| - getAllProducts, |
| 7 | + filterProducts, |
7 | 8 | getProductById,
|
8 | 9 | updateProductById,
|
9 | 10 | deleteProductById,
|
10 | 11 | } = require('./service');
|
11 | 12 |
|
| 13 | +const { createSchema, updateSchema, idSchema } = require('./request'); |
| 14 | +const { validateRequest } = require('../../middlewares/request-validate'); |
| 15 | +const { logRequest } = require('../../middlewares/log'); |
| 16 | + |
12 | 17 | // CRUD for product entity
|
13 | 18 | const routes = () => {
|
14 | 19 | const router = express.Router();
|
15 |
| - |
16 |
| - router.get('/', async (req, res, next) => { |
17 |
| - logger.info('GET /api/v1/products', { query: req.query }); |
| 20 | + logger.info('Setting up product routes'); |
| 21 | + router.get('/', logRequest({}), async (req, res, next) => { |
18 | 22 | try {
|
19 |
| - const products = await getAllProducts(); |
| 23 | + // TODO: Add pagination and filtering |
| 24 | + const products = await filterProducts(req.query); |
20 | 25 | res.json(products);
|
21 | 26 | } catch (error) {
|
22 | 27 | next(error);
|
23 | 28 | }
|
24 | 29 | });
|
25 | 30 |
|
26 |
| - router.post('/', async (req, res) => { |
27 |
| - logger.info('POST /api/v1/products', { body: req.body }); |
28 |
| - const product = await createProduct(req.body); |
29 |
| - res.status(201).json(product); |
30 |
| - }); |
| 31 | + router.post( |
| 32 | + '/', |
| 33 | + logRequest({}), |
| 34 | + validateRequest({ schema: createSchema }), |
| 35 | + async (req, res, next) => { |
| 36 | + try { |
| 37 | + const product = await createProduct(req.body); |
| 38 | + res.status(201).json(product); |
| 39 | + } catch (error) { |
| 40 | + next(error); |
| 41 | + } |
| 42 | + } |
| 43 | + ); |
31 | 44 |
|
32 |
| - router.get('/:id', async (req, res) => { |
33 |
| - logger.info('GET /api/v1/products/:id', { params: req.params }); |
34 |
| - const product = await getProductById(req.params.id); |
35 |
| - res.status(200).json(product); |
36 |
| - }); |
| 45 | + router.get( |
| 46 | + '/:id', |
| 47 | + logRequest({}), |
| 48 | + validateRequest({ schema: idSchema, isParam: true }), |
| 49 | + async (req, res, next) => { |
| 50 | + try { |
| 51 | + const product = await getProductById(req.params.id); |
| 52 | + if (!product) { |
| 53 | + throw new AppError('Product not found', 'Product not found', 404); |
| 54 | + } |
| 55 | + res.status(200).json(product); |
| 56 | + } catch (error) { |
| 57 | + next(error); |
| 58 | + } |
| 59 | + } |
| 60 | + ); |
37 | 61 |
|
38 |
| - router.put('/:id', async (req, res) => { |
39 |
| - res.json({ status: 'UP' }); |
40 |
| - }); |
| 62 | + router.put( |
| 63 | + '/:id', |
| 64 | + logRequest({}), |
| 65 | + validateRequest({ schema: idSchema, isParam: true }), |
| 66 | + validateRequest({ schema: updateSchema }), |
| 67 | + async (req, res, next) => { |
| 68 | + try { |
| 69 | + const product = await updateProductById(req.params.id, req.body); |
| 70 | + if (!product) { |
| 71 | + throw new AppError('Product not found', 'Product not found', 404); |
| 72 | + } |
| 73 | + res.status(200).json(product); |
| 74 | + } catch (error) { |
| 75 | + next(error); |
| 76 | + } |
| 77 | + } |
| 78 | + ); |
41 | 79 |
|
42 |
| - router.delete('/:id', async (req, res) => { |
43 |
| - res.json({ status: 'UP' }); |
44 |
| - }); |
| 80 | + router.delete( |
| 81 | + '/:id', |
| 82 | + logRequest({}), |
| 83 | + validateRequest({ schema: idSchema, isParam: true }), |
| 84 | + async (req, res, next) => { |
| 85 | + try { |
| 86 | + await deleteProductById(req.params.id); |
| 87 | + res.status(204).json({ message: 'Product deleted' }); |
| 88 | + } catch (error) { |
| 89 | + next(error); |
| 90 | + } |
| 91 | + } |
| 92 | + ); |
45 | 93 |
|
46 | 94 | return router;
|
47 | 95 | };
|
|
0 commit comments