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
79 changes: 62 additions & 17 deletions server/src/controllers/ratingController.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,78 @@
import mongoose from 'mongoose';
import { Rating } from '../models/Rating.js';

// GET /api/ratings
// TODO: implement per README.md section 2.
export async function getAllRatings(req, res, next) {
export const getAllRatings = async (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) {
export const getRating = async (req, res, next) => {
try {
// TODO
} catch (err) { next(err); }
}
if (!mongoose.isValidObjectId(req.params.id)) {
return res.status(400).json({ message: 'Invalid id' });
}
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) {
export const createRating = async (req, res, next) => {
try {
// TODO
} catch (err) { next(err); }
}
const { bookCode, rating, note, ratedBy } = req.body;
const created = await Rating.create({ bookCode, rating, note, ratedBy });
res.status(201).json({ rating: created });
} catch (err) {
if (err.name === 'ValidationError') {
return res.status(400).json({ message: err.message });
}
if (err.code === 11000) {
return res.status(409).json({ message: 'This user already rated this book' });
}
next(err);
}
};

// GET /api/ratings/summary?bookCode=BK101
// 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 { bookCode } = req.query;
if (!bookCode || typeof bookCode !== 'string') {
return res.status(400).json({ message: 'bookCode is required' });
}

const result = await Rating.aggregate([
{ $match: { bookCode } },
{
$group: {
_id: '$bookCode',
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);
}
};
10 changes: 8 additions & 2 deletions server/src/models/Rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ 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, partialFilterExpression: { ratedBy: { $type: 'objectId' } } }
);

export const Rating = mongoose.model('Rating', ratingSchema);
5 changes: 4 additions & 1 deletion server/src/routes/ratings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('/summary', getRatingSummary); // must come before '/:id'
router.post('/', createRating);
router.get('/', getAllRatings);
router.get('/:id', getRating);

export default router;
Loading