diff --git a/server/src/controllers/evaluationController.js b/server/src/controllers/evaluationController.js index 3d43598..281207c 100644 --- a/server/src/controllers/evaluationController.js +++ b/server/src/controllers/evaluationController.js @@ -1,33 +1,56 @@ +import Joi from 'joi'; import { Evaluation } from '../models/Evaluation.js'; +const createSchema = Joi.object({ + seminarCode: Joi.string().required(), + score: Joi.number().min(1).max(5).required(), + comment: Joi.string(), + 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.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 + const evaluation = await Evaluation.findById(req.params.id); + if (!evaluation) return res.status(404).json({ message: 'Evaluation not found' }); + res.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 + const { value, error } = createSchema.validate(req.body); + if (error) return res.status(400).json({ message: error.message }); + + const evaluation = await Evaluation.create(value); + res.status(201).json({ evaluation }); } catch (err) { 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 (!seminarCode) return res.status(400).json({ message: 'seminarCode is required' }); + + const [summary] = await Evaluation.aggregate([ + { $match: { seminarCode } }, + { $group: { _id: null, averageScore: { $avg: '$score' }, evaluationCount: { $sum: 1 } } } + ]); + + res.json({ + seminarCode, + averageScore: summary?.averageScore ?? 0, + evaluationCount: summary?.evaluationCount ?? 0 + }); } catch (err) { next(err); } } diff --git a/server/src/models/Evaluation.js b/server/src/models/Evaluation.js index b7a7d5a..5580772 100644 --- a/server/src/models/Evaluation.js +++ b/server/src/models/Evaluation.js @@ -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); diff --git a/server/src/routes/evaluations.js b/server/src/routes/evaluations.js index 00ec53e..f1ec568 100644 --- a/server/src/routes/evaluations.js +++ b/server/src/routes/evaluations.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.post('/', createEvaluation); +router.get('/', getAllEvaluations); +router.get('/summary', getEvaluationSummary); +router.get('/:id', getEvaluation); export default router;