From e0e6803dc5d3e724d6381fff37f013758fbfc07d Mon Sep 17 00:00:00 2001 From: NPL Codes <104311071+amin-leon@users.noreply.github.com> Date: Thu, 4 Jul 2024 15:59:09 +0200 Subject: [PATCH] [finishes #187906865 ] add reviews to products using single fetch --- src/controllers/productsController.ts | 7 +++++-- src/routes/productRoutes.ts | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/controllers/productsController.ts b/src/controllers/productsController.ts index f33ebc03..b2246333 100644 --- a/src/controllers/productsController.ts +++ b/src/controllers/productsController.ts @@ -246,7 +246,10 @@ export const getAllProduct = async (req: Request, res: Response) => { try { // Fetch all products with their associated sizes const products: any = await Product.findAll({ - include: [{ model: Size, as: 'sizes' }], + include: [ + { model: Size, as: 'sizes' }, + { model: Review, as: 'reviews', include: [{ model: User, as: 'user', attributes: ['photoUrl', 'firstName'] }] }, + ], }); const currentDate = new Date(); @@ -277,7 +280,7 @@ export const getProductById = async (req: Request, res: Response) => { try { const { productId } = req.params; const product = await Product.findByPk(productId, { - include: [{ model: Size, as: 'sizes' }], + include: [{ model: Size, as: 'sizes' },{ model: Review, as: 'reviews', include: [{ model: User, as: 'user', attributes: ['photoUrl', 'firstName'] }] },], }); if (!product) { diff --git a/src/routes/productRoutes.ts b/src/routes/productRoutes.ts index a60b4843..f2de27ec 100644 --- a/src/routes/productRoutes.ts +++ b/src/routes/productRoutes.ts @@ -45,7 +45,7 @@ router.put('/:sizeId/available', isAuthenticated, checkUserRoles('seller'), mark router.put('/:sizeId/unavailable', isAuthenticated, checkUserRoles('seller'), markProductAsUnavailable); router.post('/:productId/review/', isAuthenticated, multerUpload.single('feedbackImage'), provideReviewToProduct); router.delete('/:productId/review/:reviewId', isAuthenticated, deleteReview); -router.get('/:productId/review/statistics', isAuthenticated, calculateAverageRating); +router.get('/:productId/review/statistics', calculateAverageRating); router.get('/:productId/reviews', getProductReviewsById); export default router;