From fc7e5c66454488819142f56c071553f41269359a Mon Sep 17 00:00:00 2001 From: nourmahdyy Date: Fri, 2 Oct 2026 02:09:55 +0300 Subject: [PATCH] Update rating functionality --- server/src/controllers/ratingController.js | 80 ++++++++++++++++++---- server/src/models/Rating.js | 7 +- server/src/routes/ratings.js | 6 +- 3 files changed, 78 insertions(+), 15 deletions(-) diff --git a/server/src/controllers/ratingController.js b/server/src/controllers/ratingController.js index 4e12a06..9711322 100644 --- a/server/src/controllers/ratingController.js +++ b/server/src/controllers/ratingController.js @@ -1,33 +1,89 @@ import { Rating } from '../models/Rating.js'; +import Joi from 'joi'; + +const createRatingSchema = Joi.object({ + movieCode: Joi.string().required(), + rating: Joi.number().integer().min(1).max(5).required(), + note: Joi.string().optional(), + ratedBy: Joi.string().hex().length(24).optional(), +}); + +const summarySchema = Joi.object({ + movieCode: Joi.string().required(), +}); // GET /api/ratings -// TODO: implement per README.md section 2. export async function getAllRatings(req, res, next) { try { - // TODO - } catch (err) { next(err); } + const ratings = await Rating.find().lean(); + res.status(200).json({ ratings }); + } catch (err) { + next(err); + } } // GET /api/ratings/:id -// TODO: implement per README.md section 2. export async function getRating(req, res, next) { try { - // TODO - } catch (err) { next(err); } + const rating = await Rating.findById(req.params.id).lean(); + if (!rating) { + return res.status(404).json({ message: 'Rating not found' }); + } + res.status(200).json({ rating }); + } catch (err) { + next(err); + } } // POST /api/ratings -// TODO: implement per README.md section 2. export async function createRating(req, res, next) { try { - // TODO - } catch (err) { next(err); } + const { error, value } = createRatingSchema.validate(req.body); + if (error) { + return res.status(400).json({ message: error.details[0].message }); + } + const rating = await Rating.create(value); + res.status(201).json({ rating }); + } catch (err) { + next(err); + } } // GET /api/ratings/summary?movieCode=MV101 -// TODO: implement per README.md section 3. export async function getRatingSummary(req, res, next) { try { - // TODO - } catch (err) { next(err); } + const { error, value } = summarySchema.validate(req.query); + if (error) { + return res.status(400).json({ message: 'movieCode is required' }); + } + + const { movieCode } = value; + const summary = await Rating.aggregate([ + { $match: { movieCode } }, + { + $group: { + _id: '$movieCode', + averageRating: { $avg: '$rating' }, + ratingCount: { $sum: 1 }, + }, + }, + ]); + + if (summary.length === 0) { + return res.status(200).json({ + movieCode, + averageRating: 0, + ratingCount: 0, + }); + } + + const result = summary[0]; + res.status(200).json({ + movieCode: result._id, + averageRating: result.averageRating, + ratingCount: result.ratingCount, + }); + } catch (err) { + next(err); + } } diff --git a/server/src/models/Rating.js b/server/src/models/Rating.js index c1e6a23..7b82142 100644 --- a/server/src/models/Rating.js +++ b/server/src/models/Rating.js @@ -4,11 +4,14 @@ import mongoose from 'mongoose'; const ratingSchema = new mongoose.Schema( { - // TODO + movieCode: { type: String, required: true }, + rating: { type: Number, required: true, min: 1, max: 5 }, + note: { type: String }, + ratedBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, }, { timestamps: true } ); -// TODO: add the compound uniqueness constraint described in README.md section 1. +ratingSchema.index({ movieCode: 1, ratedBy: 1 }, { unique: true }); export const Rating = mongoose.model('Rating', ratingSchema); diff --git a/server/src/routes/ratings.js b/server/src/routes/ratings.js index 8183a49..d01f11b 100644 --- a/server/src/routes/ratings.js +++ b/server/src/routes/ratings.js @@ -8,6 +8,10 @@ import { const router = Router(); -// TODO: wire up the three routes in README.md section 2 and the summary route in section 3. +// Summary must be before /:id +router.get('/summary', getRatingSummary); +router.get('/', getAllRatings); +router.post('/', createRating); +router.get('/:id', getRating); export default router;