From fdf11e2b03e0636c164977f4f984953c5677476e Mon Sep 17 00:00:00 2001 From: jjanahassan Date: Wed, 30 Sep 2026 12:02:56 +0300 Subject: [PATCH 1/2] Update ratings controller, model, and routes --- server/package-lock.json | 2 + server/src/controllers/ratingController.js | 55 ++++++++++++++++++++-- server/src/models/Rating.js | 11 ++++- server/src/routes/ratings.js | 7 ++- 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/server/package-lock.json b/server/package-lock.json index b819a71..0c230a0 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -53,6 +53,7 @@ "integrity": "sha512-RgHBCvtjbOK2gXSNBNIkNoEc9qoVEtau3hj8gEqKQuL3HZAibKarWFEI3Lfm6EYKkLalOh8eSrj9b+ch9H/VBA==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@babel/code-frame": "^7.29.7", "@babel/generator": "^7.29.7", @@ -1503,6 +1504,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "baseline-browser-mapping": "^2.11.25", "caniuse-lite": "^1.0.30001810", diff --git a/server/src/controllers/ratingController.js b/server/src/controllers/ratingController.js index 4e12a06..cf2dca7 100644 --- a/server/src/controllers/ratingController.js +++ b/server/src/controllers/ratingController.js @@ -4,7 +4,9 @@ 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(); + res.status(200).json({ ratings }); + } catch (err) { next(err); } } @@ -12,7 +14,15 @@ 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.status(200).json({ rating }); } catch (err) { next(err); } } @@ -20,14 +30,49 @@ 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); } } -// GET /api/ratings/summary?movieCode=MV101 +// GET /api/ratings/summary?bookCode=BK101 // TODO: implement per README.md section 3. export async function getRatingSummary(req, res, next) { try { - // TODO + const { bookCode } = req.query; + + if (!bookCode) { + return res.status(400).json({ + message: 'bookCode is required' + }); + } + + const result = await Rating.aggregate([ + { + $match: { bookCode } + }, + { + $group: { + _id: '$bookCode', + averageRating: { $avg: '$rating' }, + ratingCount: { $sum: 1 } + } + } + ]); + + if (result.length === 0) { + return res.status(200).json({ + bookCode, + averageRating: 0, + ratingCount: 0 + }); + } + + res.status(200).json({ + bookCode, + averageRating: result[0].averageRating, + ratingCount: result[0].ratingCount + }); } catch (err) { next(err); } } diff --git a/server/src/models/Rating.js b/server/src/models/Rating.js index c1e6a23..03d0d81 100644 --- a/server/src/models/Rating.js +++ b/server/src/models/Rating.js @@ -4,11 +4,18 @@ import mongoose from 'mongoose'; const ratingSchema = new mongoose.Schema( { - // TODO + bookCode: { 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( + { bookCode: 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..b0d346a 100644 --- a/server/src/routes/ratings.js +++ b/server/src/routes/ratings.js @@ -8,6 +8,11 @@ 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; From 00cbd353863eead850c50a2615dd7aac1f5446e6 Mon Sep 17 00:00:00 2001 From: jjanahassan Date: Wed, 30 Sep 2026 12:15:51 +0300 Subject: [PATCH 2/2] Fix: rename bookCode to movieCode in Rating model and controller --- server/src/controllers/ratingController.js | 16 ++++++++-------- server/src/models/Rating.js | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/server/src/controllers/ratingController.js b/server/src/controllers/ratingController.js index cf2dca7..ae431b6 100644 --- a/server/src/controllers/ratingController.js +++ b/server/src/controllers/ratingController.js @@ -36,25 +36,25 @@ export async function createRating(req, res, next) { } catch (err) { next(err); } } -// GET /api/ratings/summary?bookCode=BK101 +// GET /api/ratings/summary?movieCode=MV101 // TODO: implement per README.md section 3. export async function getRatingSummary(req, res, next) { try { - const { bookCode } = req.query; + const { movieCode } = req.query; - if (!bookCode) { + if (!movieCode) { return res.status(400).json({ - message: 'bookCode is required' + message: 'movieCode is required' }); } const result = await Rating.aggregate([ { - $match: { bookCode } + $match: { movieCode } }, { $group: { - _id: '$bookCode', + _id: '$movieCode', averageRating: { $avg: '$rating' }, ratingCount: { $sum: 1 } } @@ -63,14 +63,14 @@ export async function getRatingSummary(req, res, next) { if (result.length === 0) { return res.status(200).json({ - bookCode, + movieCode, averageRating: 0, ratingCount: 0 }); } res.status(200).json({ - bookCode, + movieCode, averageRating: result[0].averageRating, ratingCount: result[0].ratingCount }); diff --git a/server/src/models/Rating.js b/server/src/models/Rating.js index 03d0d81..b20c744 100644 --- a/server/src/models/Rating.js +++ b/server/src/models/Rating.js @@ -4,7 +4,7 @@ import mongoose from 'mongoose'; const ratingSchema = new mongoose.Schema( { - bookCode: { type: String, required: true }, + 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'} @@ -14,7 +14,7 @@ const ratingSchema = new mongoose.Schema( ); ratingSchema.index( - { bookCode: 1, ratedBy: 1 }, + { movieCode: 1, ratedBy: 1 }, { unique: true } );