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
2 changes: 2 additions & 0 deletions server/package-lock.json

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

54 changes: 44 additions & 10 deletions server/src/controllers/evaluationController.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,67 @@
import Joi from 'joi';
import mongoose from 'mongoose';
import { Evaluation } from '../models/Evaluation.js';

const createSchema = Joi.object({
seminarCode: Joi.string().trim().required(),
score: Joi.number().min(1).max(5).required(),
comment: Joi.string().allow(''),
evaluatedBy: Joi.string().hex().length(24)
});

// GET /api/evaluations
// TODO: implement per README.md section 2.
export async function getAllEvaluations(req, res, next) {
try {
// TODO
const evaluations = await Evaluation.find().sort({ createdAt: -1 });
res.status(200).json({ evaluations });
} catch (err) { next(err); }
}

// GET /api/evaluations/:id
// TODO: implement per README.md section 2.
export async function getEvaluation(req, res, next) {
try {
// TODO
if (!mongoose.isValidObjectId(req.params.id)) {
return res.status(400).json({ message: 'Invalid evaluation id' });
}
const evaluation = await Evaluation.findById(req.params.id);
if (!evaluation) return res.status(404).json({ message: 'Evaluation not found' });
res.status(200).json({ evaluation });
} catch (err) { next(err); }
}

// POST /api/evaluations
// TODO: implement per README.md section 2.
export async function createEvaluation(req, res, next) {
try {
// TODO
} catch (err) { next(err); }
const { value, error } = createSchema.validate(req.body, { stripUnknown: true });
if (error) return res.status(400).json({ message: error.message });

const evaluation = await Evaluation.create(value);
res.status(201).json({ evaluation });
} catch (err) {
if (err.code === 11000) {
return res.status(409).json({ message: 'This user has already evaluated this seminar' });
}
next(err);
}
}

// GET /api/evaluations/summary?seminarCode=SM101
// TODO: implement per README.md section 3.
export async function getEvaluationSummary(req, res, next) {
try {
// TODO
const { seminarCode } = req.query;
if (typeof seminarCode !== 'string' || !seminarCode.trim()) {
return res.status(400).json({ message: 'seminarCode is required' });
}

const [result] = await Evaluation.aggregate([
{ $match: { seminarCode } },
{ $group: { _id: null, averageScore: { $avg: '$score' }, evaluationCount: { $sum: 1 } } }
]);

res.status(200).json({
seminarCode,
averageScore: result ? result.averageScore : 0,
evaluationCount: result ? result.evaluationCount : 0
});
} catch (err) { next(err); }
}
}
11 changes: 6 additions & 5 deletions server/src/models/Evaluation.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import mongoose from 'mongoose';

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

const evaluationSchema = new mongoose.Schema(
{
// TODO
seminarCode: { type: String, required: true },
score: { type: Number, required: true, min: 1, max: 5 },
comment: { type: String },
evaluatedBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
},
{ timestamps: true }
);

// TODO: add the compound uniqueness constraint described in README.md section 1.
evaluationSchema.index({ seminarCode: 1, evaluatedBy: 1 }, { unique: true });

export const Evaluation = mongoose.model('Evaluation', evaluationSchema);
export const Evaluation = mongoose.model('Evaluation', evaluationSchema);
8 changes: 6 additions & 2 deletions server/src/routes/evaluations.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {

const router = Router();

// TODO: wire up the three routes in README.md section 2 and the summary route in section 3.
router.get('/', getAllEvaluations);
// Must come before '/:id' so "summary" is not treated as an id.
router.get('/summary', getEvaluationSummary);
router.get('/:id', getEvaluation);
router.post('/', createEvaluation);

export default router;
export default router
Loading