Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
"scripts": {
"dev": "nodemon src/index.js",
"start": "node src/index.js",
"test": "NODE_OPTIONS=--experimental-vm-modules jest --runInBand",
"grade": "NODE_OPTIONS=--experimental-vm-modules jest --runInBand --verbose"
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --runInBand",
"grade": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --runInBand --verbose"
},
"dependencies": {
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"joi": "^17.12.3",
"mongoose": "^8.6.0",
"morgan": "^1.10.0",
"joi": "^17.12.3"
"morgan": "^1.10.0"
},
"devDependencies": {
"cross-env": "^10.1.0",
"jest": "^29.7.0",
"nodemon": "^3.1.0",
"supertest": "^7.1.1"
Expand Down
99 changes: 75 additions & 24 deletions server/src/controllers/ratingController.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,84 @@
import Joi from 'joi';
import { Rating } from '../models/Rating.js';

// GET /api/ratings
// TODO: implement per README.md section 2.
export async function getAllRatings(req, res, next) {
const createRatingSchema = Joi.object({
movieCode: Joi.string().required(),
rating: Joi.number().integer().min(1).max(5).required(),
note: Joi.string().optional(),
ratedBy: Joi.string().hex().length(24).optional(),
});

export const createRating = async (req, res, next) => {
try {
// TODO
} catch (err) { next(err); }
}
const { error } = createRatingSchema.validate(req.body);
if (error) {
return res.status(400).json({ message: error.details[0].message });
}

const rating = await Rating.create(req.body);
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/:id
// TODO: implement per README.md section 2.
export async function getRating(req, res, next) {
export const getAllRatings = async (req, res, next) => {
try {
// TODO
} catch (err) { next(err); }
}
const ratings = await Rating.find().lean();
res.status(200).json({ ratings });
} catch (err) {
next(err);
}
};

// POST /api/ratings
// TODO: implement per README.md section 2.
export async function createRating(req, res, next) {
export const getRating = async (req, res, next) => {
try {
// TODO
} catch (err) { next(err); }
}
const rating = await Rating.findById(req.params.id).lean();
if (!rating) {
return res.status(404).json({ message: 'Rating not found' });
}
res.status(200).json({ rating });
} catch (err) {
next(err);
}
};

// GET /api/ratings/summary?movieCode=MV101
// TODO: implement per README.md section 3.
export async function getRatingSummary(req, res, next) {
export const getRatingSummary = async (req, res, next) => {
try {
// TODO
} catch (err) { next(err); }
}
const { movieCode } = req.query;
if (!movieCode) {
return res.status(400).json({ message: 'movieCode is required' });
}

const summary = await Rating.aggregate([
{ $match: { movieCode: movieCode } },
{
$group: {
_id: '$movieCode',
averageRating: { $avg: '$rating' },
ratingCount: { $sum: 1 },
},
},
]);

if (summary.length === 0) {
return res.status(200).json({
movieCode,
averageRating: 0,
ratingCount: 0,
});
}

const { _id, averageRating, ratingCount } = summary[0];
res.status(200).json({
movieCode: _id,
averageRating,
ratingCount,
});
} catch (err) {
next(err);
}
};
23 changes: 19 additions & 4 deletions server/src/models/Rating.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import mongoose from 'mongoose';

// TODO: define the Rating schema per README.md section 1.

const ratingSchema = new mongoose.Schema(
{
// TODO
movieCode: {
type: String,
required: [true, 'Movie code is required'],
},
rating: {
type: Number,
required: [true, 'Rating is required'],
min: [1, 'Rating must be at least 1'],
max: [5, 'Rating cannot exceed 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.
// Compound unique index to prevent a user from rating the same movie twice
ratingSchema.index({ movieCode: 1, ratedBy: 1 }, { unique: true });

export const Rating = mongoose.model('Rating', ratingSchema);
14 changes: 9 additions & 5 deletions server/src/routes/ratings.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Router } from 'express';
import express from 'express';
import {
createRating,
getAllRatings,
getRating,
createRating,
getRatingSummary
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.
// Order is important: /summary must come before /:id
router.get('/summary', getRatingSummary);
router.get('/', getAllRatings);
router.post('/', createRating);
router.get('/:id', getRating);

export default router;
Loading