Skip to content

Commit

Permalink
[finishes #187906865 ] add reviews to products using single fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
amin-leon committed Jul 4, 2024
1 parent 44883ed commit 7f9d100
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/controllers/productsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -277,7 +280,10 @@ 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) {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/productRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 7f9d100

Please sign in to comment.