Spaces:
Sleeping
Sleeping
| const Review = require('../models/Review'); | |
| const User = require('../models/User'); | |
| const createReview = async (req, res) => { | |
| try { | |
| const { photographerId, bookingId, rating, comment } = req.body; | |
| const customerId = req.user._id; | |
| // Check if review already exists for this booking | |
| const existingReview = await Review.findOne({ bookingId }); | |
| if (existingReview) { | |
| return res.status(400).json({ message: 'You have already reviewed this booking' }); | |
| } | |
| const review = await Review.create({ | |
| photographerId, | |
| customerId, | |
| bookingId, | |
| rating, | |
| comment | |
| }); | |
| // Update photographer's average rating and reviews count | |
| const photographer = await User.findById(photographerId); | |
| if (photographer) { | |
| const allReviews = await Review.find({ photographerId }); | |
| const totalRating = allReviews.reduce((sum, r) => sum + r.rating, 0); | |
| photographer.rating = totalRating / allReviews.length; | |
| photographer.reviewsCount = allReviews.length; | |
| await photographer.save(); | |
| } | |
| res.status(201).json(review); | |
| } catch (error) { | |
| res.status(500).json({ message: error.message }); | |
| } | |
| }; | |
| const getPhotographerReviews = async (req, res) => { | |
| try { | |
| const reviews = await Review.find({ photographerId: req.params.id }) | |
| .populate('customerId', 'firstName lastName profilePicture') | |
| .sort({ createdAt: -1 }); | |
| res.json(reviews); | |
| } catch (error) { | |
| res.status(500).json({ message: error.message }); | |
| } | |
| }; | |
| module.exports = { createReview, getPhotographerReviews }; | |