diff --git a/server/.gitignore b/server/.gitignore new file mode 100644 index 0000000..fcc5400 --- /dev/null +++ b/server/.gitignore @@ -0,0 +1,3 @@ +.env +node_modules/ +.DS_Store 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..a52a6bd 100644 --- a/server/src/controllers/ratingController.js +++ b/server/src/controllers/ratingController.js @@ -1,33 +1,76 @@ +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) { + 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 summary = await Rating.aggregate([ + { $match: { movieCode } }, + { + $group: { + _id: '$movieCode', + averageRating: { $avg: '$rating' }, + ratingCount: { $sum: 1 } + } + } + ]); + + if (summary.length === 0) { + return res.json({ + movieCode, + averageRating: 0, + ratingCount: 0 + }); + } + + const result = summary[0]; + res.json({ + movieCode: result._id, + averageRating: result.averageRating, + ratingCount: result.ratingCount + }); } catch (err) { next(err); } } diff --git a/server/src/models/Rating.js b/server/src/models/Rating.js index c1e6a23..145e8fd 100644 --- a/server/src/models/Rating.js +++ b/server/src/models/Rating.js @@ -1,14 +1,26 @@ import mongoose from 'mongoose'; -// TODO: define the Rating schema per README.md section 1. - -const ratingSchema = new mongoose.Schema( - { - // TODO +const ratingSchema = new mongoose.Schema({ + movieCode: { + type: String, + required: true + }, + rating: { + type: Number, + required: true, + min: 1, + max: 5 + }, + note: { + type: String }, - { timestamps: true } -); + 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: a user can only rate a movie once +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..8152c63 100644 --- a/server/src/routes/ratings.js +++ b/server/src/routes/ratings.js @@ -1,13 +1,16 @@ -import { Router } from 'express'; +import express from 'express'; import { + createRating, getAllRatings, getRating, - createRating, getRatingSummary } from '../controllers/ratingController.js'; -const router = Router(); +const router = express.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;