From 4db786bfbf1805f7d4fbd5bce421738a263bf472 Mon Sep 17 00:00:00 2001 From: mariam Date: Tue, 29 Sep 2026 12:39:51 +0300 Subject: [PATCH] mariam pre task --- server/src/controllers/ratingController.js | 97 +++++++++++++++++++--- server/src/models/Rating.js | 31 +++++-- server/src/routes/ratings.js | 11 ++- 3 files changed, 119 insertions(+), 20 deletions(-) diff --git a/server/src/controllers/ratingController.js b/server/src/controllers/ratingController.js index 1df9ca0..d040c22 100644 --- a/server/src/controllers/ratingController.js +++ b/server/src/controllers/ratingController.js @@ -1,33 +1,104 @@ import { Rating } from '../models/Rating.js'; // 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(); + + 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); + + 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 rating = await Rating.create(req.body); + + res.status(201).json({ rating }); + + } catch (err) { + next(err); + } + } // GET /api/ratings/summary?bookCode=BK101 -// TODO: implement per README.md section 3. + export async function getRatingSummary(req, res, next) { + try { - // TODO - } catch (err) { next(err); } -} + + const bookCode = req.query.bookCode; + + if (!bookCode) { + return res.status(400).json({ + message: 'bookCode is required' + }); + } + + const result = await Rating.aggregate([ + { + $match: { + bookCode: bookCode + } + }, + { + $group: { + _id: null, + 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); + } + +} \ No newline at end of file diff --git a/server/src/models/Rating.js b/server/src/models/Rating.js index c1e6a23..89aed26 100644 --- a/server/src/models/Rating.js +++ b/server/src/models/Rating.js @@ -1,14 +1,35 @@ import mongoose from 'mongoose'; -// TODO: define the Rating schema per README.md section 1. - 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); +export const Rating = mongoose.model('Rating', ratingSchema); \ No newline at end of file diff --git a/server/src/routes/ratings.js b/server/src/routes/ratings.js index 8183a49..2e4a189 100644 --- a/server/src/routes/ratings.js +++ b/server/src/routes/ratings.js @@ -1,4 +1,5 @@ import { Router } from 'express'; + import { getAllRatings, getRating, @@ -8,6 +9,12 @@ 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; +export default router; \ No newline at end of file