diff --git a/server/src/controllers/evaluationController.js b/server/src/controllers/evaluationController.js index 3d43598..78fde8f 100644 --- a/server/src/controllers/evaluationController.js +++ b/server/src/controllers/evaluationController.js @@ -4,30 +4,89 @@ import { Evaluation } from '../models/Evaluation.js'; // TODO: implement per README.md section 2. export async function getAllEvaluations(req, res, next) { try { - // TODO - } catch (err) { next(err); } + const evaluations = await Evaluation.find(); + + 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 - } catch (err) { next(err); } + 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 evaluation = await Evaluation.create(req.body); + + 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 - } catch (err) { next(err); } -} + const { seminarCode } = req.query; + + if (!seminarCode) { + 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 } + } + } + ]); + + if (result.length === 0) { + return res.status(200).json({ + seminarCode, + averageScore: 0, + evaluationCount: 0 + }); + } + + res.status(200).json({ + seminarCode, + averageScore: result[0].averageScore, + evaluationCount: result[0].evaluationCount + }); + } catch (err) { + next(err); + } +} \ No newline at end of file diff --git a/server/src/models/Evaluation.js b/server/src/models/Evaluation.js index b7a7d5a..274933d 100644 --- a/server/src/models/Evaluation.js +++ b/server/src/models/Evaluation.js @@ -4,11 +4,34 @@ import mongoose from 'mongoose'; 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); \ No newline at end of file diff --git a/server/src/routes/evaluations.js b/server/src/routes/evaluations.js index 00ec53e..7f1df2c 100644 --- a/server/src/routes/evaluations.js +++ b/server/src/routes/evaluations.js @@ -7,6 +7,13 @@ import { } from '../controllers/evaluationController.js'; const router = Router(); +router.post('/', createEvaluation); + +router.get('/', getAllEvaluations); + +router.get('/summary', getEvaluationSummary); + +router.get('/:id', getEvaluation); // TODO: wire up the three routes in README.md section 2 and the summary route in section 3.