From b3532ab7e9dcb8615b2a2a0a3bbbc7f5d2c6f91b Mon Sep 17 00:00:00 2001 From: Hicham Haggag Date: Tue, 29 Sep 2026 13:46:26 +0300 Subject: [PATCH] Implement movie rating API --- server/src/controllers/ratingController.js | 30 +++++++++++++++++++--- server/src/models/Rating.js | 22 +++++++++++++--- server/src/routes/ratings.js | 5 +++- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/server/src/controllers/ratingController.js b/server/src/controllers/ratingController.js index 4e12a06..6f8749e 100644 --- a/server/src/controllers/ratingController.js +++ b/server/src/controllers/ratingController.js @@ -4,7 +4,8 @@ import { Rating } from '../models/Rating.js'; // TODO: implement per README.md section 2. export async function getAllRatings(req, res, next) { try { - // TODO + const ratings = await Rating.find().sort({ createdAt: -1 }); + res.json({ ratings }); } catch (err) { next(err); } } @@ -12,7 +13,9 @@ export async function getAllRatings(req, res, next) { // TODO: implement per README.md section 2. export async function getRating(req, res, next) { try { - // TODO + const rating = await Rating.findById(req.params.id); + if (!rating) return res.status(404).json({ message: 'Rating not found' }); + res.json({ rating }); } catch (err) { next(err); } } @@ -20,7 +23,8 @@ export async function getRating(req, res, next) { // TODO: implement per README.md section 2. export async function createRating(req, res, next) { try { - // TODO + const rating = await Rating.create(req.body); + res.status(201).json({ rating }); } catch (err) { next(err); } } @@ -28,6 +32,24 @@ export async function createRating(req, res, next) { // TODO: implement per README.md section 3. export async function getRatingSummary(req, res, next) { try { - // TODO + const { movieCode } = req.query; + if (!movieCode) return res.status(400).json({ message: 'movieCode is required' }); + + const [summary] = await Rating.aggregate([ + { $match: { movieCode } }, + { + $group: { + _id: '$movieCode', + averageRating: { $avg: '$rating' }, + ratingCount: { $sum: 1 } + } + } + ]); + + res.json({ + movieCode, + averageRating: summary ? summary.averageRating : 0, + ratingCount: summary ? summary.ratingCount : 0 + }); } catch (err) { next(err); } } diff --git a/server/src/models/Rating.js b/server/src/models/Rating.js index c1e6a23..704573f 100644 --- a/server/src/models/Rating.js +++ b/server/src/models/Rating.js @@ -1,14 +1,28 @@ import mongoose from 'mongoose'; -// TODO: define the Rating schema per README.md section 1. - 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..cbde8b2 100644 --- a/server/src/routes/ratings.js +++ b/server/src/routes/ratings.js @@ -8,6 +8,9 @@ import { const router = Router(); -// TODO: wire up the three routes in README.md section 2 and the summary route in section 3. +router.post('/', createRating); +router.get('/', getAllRatings); +router.get('/summary', getRatingSummary); +router.get('/:id', getRating); export default router;