diff --git a/server/src/controllers/ratingController.js b/server/src/controllers/ratingController.js index 1df9ca0..56717bc 100644 --- a/server/src/controllers/ratingController.js +++ b/server/src/controllers/ratingController.js @@ -1,33 +1,74 @@ +import Joi from 'joi'; import { Rating } from '../models/Rating.js'; +const createSchema = Joi.object({ + bookCode: Joi.string().required(), + rating: Joi.number().min(1).max(5).required(), + note: Joi.string().allow('').optional(), + ratedBy: Joi.alternatives() + .try( + Joi.string().hex().length(24), + Joi.valid(null) + ) + .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 + const { value, error } = createSchema.validate(req.body, { abortEarly: false, stripUnknown: true }); + if (error) return res.status(400).json({ message: error.message }); + + const rating = await Rating.create(value); + 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 + const { bookCode } = req.query; + + 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, + averageRating: Number(summary.averageRating), + ratingCount: Number(summary.ratingCount) + }); } catch (err) { next(err); } } diff --git a/server/src/models/Rating.js b/server/src/models/Rating.js index c1e6a23..2d20ff2 100644 --- a/server/src/models/Rating.js +++ b/server/src/models/Rating.js @@ -1,14 +1,15 @@ 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); diff --git a/server/src/routes/ratings.js b/server/src/routes/ratings.js index 8183a49..c051619 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.get('/', getAllRatings); +router.get('/summary', getRatingSummary); +router.get('/:id', getRating); +router.post('/', createRating); export default router;