From 362fcd7ed5379c4fc721f91b3fee533d3633cc29 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 29 Sep 2026 12:40:34 +0300 Subject: [PATCH] Ziad_gharib --- server/src/controllers/ratingController.js | 28 ++++++++++++++++++---- server/src/models/Rating.js | 6 ++++- server/src/routes/ratings.js | 4 ++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/server/src/controllers/ratingController.js b/server/src/controllers/ratingController.js index 1df9ca0..62dcdb8 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 }).lean(); + 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,22 @@ export async function createRating(req, res, next) { // TODO: implement per README.md section 3. export async function getRatingSummary(req, res, next) { try { - // TODO + const bookCode = req.query.bookCode; + if (!bookCode) return res.status(400).json({ message: 'bookCode is required' }); + + const [summary] = await Rating.aggregate([ + { $match: { bookCode } }, + { $group: { _id: '$bookCode', averageRating: { $avg: '$rating' }, ratingCount: { $sum: 1 } } } + ]); + + if (!summary) { + return res.json({ bookCode, averageRating: 0, ratingCount: 0 }); + } + + res.json({ + bookCode: summary._id, + averageRating: summary.averageRating, + ratingCount: summary.ratingCount + }); } catch (err) { next(err); } } diff --git a/server/src/models/Rating.js b/server/src/models/Rating.js index c1e6a23..76a6db1 100644 --- a/server/src/models/Rating.js +++ b/server/src/models/Rating.js @@ -4,11 +4,15 @@ 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..166de38 100644 --- a/server/src/routes/ratings.js +++ b/server/src/routes/ratings.js @@ -9,5 +9,9 @@ import { const router = Router(); // TODO: wire up the three routes in README.md section 2 and the summary route in section 3. +router.get('/summary', getRatingSummary); +router.get('/', getAllRatings); +router.get('/:id', getRating); +router.post('/', createRating); export default router;