Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[finishes #187906865 ] add reviews to products using single fetch #93

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 5 additions & 29 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,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) {
Expand Down Expand Up @@ -483,30 +486,3 @@ export const calculateAverageRating = async (req: Request, res: Response) => {
}
};

// get product review ---- Buyer
export const getProductReviewsById = async (req: Request, res: Response) => {
try {
const { productId } = req.params;
const product = await Product.findByPk(productId, {
include: [
{ model: Size, as: 'sizes' },
{ model: Review, as: 'reviews', include: [{ model: User, as: 'user', attributes: ['photoUrl', 'firstName'] }] },
],
});

if (!product) {
return res.status(404).json({
ok: false,
message: 'Product not found',
});
}

res.status(200).json({
ok: true,
message: 'Product retrieved successfully',
data: product,
});
} catch (error) {
sendInternalErrorResponse(res, error);
}
};
4 changes: 1 addition & 3 deletions src/routes/productRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
provideReviewToProduct,
calculateAverageRating,
deleteReview,
getProductReviewsById,
} from '../controllers/productsController';
import multerUpload from '../helpers/multer';
import { checkUserRoles, isAuthenticated } from '../middlewares/authMiddlewares';
Expand Down Expand Up @@ -45,7 +44,6 @@ 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/reviews', getProductReviewsById);
router.get('/:productId/review/statistics', calculateAverageRating);

export default router;
Loading