From 240106233216963a871a181d5c3f5484c593928b Mon Sep 17 00:00:00 2001 From: Yehia El Shayeb Date: Tue, 29 Sep 2026 13:58:53 +0300 Subject: [PATCH] added files --- .claude/worktrees/rating-api-impl | 1 + server/src/controllers/ratingController.js | 64 ++++++++++++++++++---- server/src/models/Rating.js | 10 ++-- server/src/routes/ratings.js | 6 +- 4 files changed, 66 insertions(+), 15 deletions(-) create mode 160000 .claude/worktrees/rating-api-impl diff --git a/.claude/worktrees/rating-api-impl b/.claude/worktrees/rating-api-impl new file mode 160000 index 0000000..9e77266 --- /dev/null +++ b/.claude/worktrees/rating-api-impl @@ -0,0 +1 @@ +Subproject commit 9e7726645edc62338a459e7a6764355b76bb0316 diff --git a/server/src/controllers/ratingController.js b/server/src/controllers/ratingController.js index 4e12a06..f216967 100644 --- a/server/src/controllers/ratingController.js +++ b/server/src/controllers/ratingController.js @@ -1,33 +1,77 @@ +import Joi from 'joi'; import { Rating } from '../models/Rating.js'; +const createSchema = Joi.object({ + movieCode: Joi.string().required(), + rating: Joi.number().min(1).max(5).required(), + note: Joi.string().optional(), + ratedBy: Joi.string().hex().length(24).optional() +}); + // GET /api/ratings -// TODO: implement per README.md section 2. export async function getAllRatings(req, res, next) { try { - // TODO + const ratings = await Rating.find().sort({ createdAt: -1 }).lean(); + res.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 + 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); } } // POST /api/ratings -// TODO: implement per README.md section 2. export async function createRating(req, res, next) { try { - // TODO - } catch (err) { next(err); } + const { value, error } = createSchema.validate(req.body); + if (error) return res.status(400).json({ message: error.message }); + + const rating = await Rating.create(value); + res.status(201).json({ rating }); + } catch (err) { + // Handle MongoDB duplicate key error for the compound index + if (err.code === 11000) { + return res.status(409).json({ message: 'User has already rated this movie' }); + } + next(err); + } } -// GET /api/ratings/summary?movieCode=MV101 -// TODO: implement per README.md section 3. +// GET /api/ratings/summary?movieCode=... 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 stats = await Rating.aggregate([ + { $match: { movieCode: movieCode } }, + { + $group: { + _id: '$movieCode', + averageRating: { $avg: '$rating' }, + ratingCount: { $sum: 1 } + } + } + ]); + + if (stats.length === 0) { + return res.json({ + movieCode, + averageRating: 0, + ratingCount: 0 + }); + } + + const { averageRating, ratingCount } = stats[0]; + res.json({ + movieCode, + averageRating, + ratingCount + }); } catch (err) { next(err); } } diff --git a/server/src/models/Rating.js b/server/src/models/Rating.js index c1e6a23..df3b9f0 100644 --- a/server/src/models/Rating.js +++ b/server/src/models/Rating.js @@ -1,14 +1,16 @@ 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. +// Compound unique index on movieCode and ratedBy +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..bad14a9 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 come before /:id to avoid being caught by the id handler +router.get('/summary', getRatingSummary); +router.get('/', getAllRatings); +router.get('/:id', getRating); +router.post('/', createRating); export default router;