diff --git a/server/package-lock.json b/server/package-lock.json index b819a71..0c230a0 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -53,6 +53,7 @@ "integrity": "sha512-RgHBCvtjbOK2gXSNBNIkNoEc9qoVEtau3hj8gEqKQuL3HZAibKarWFEI3Lfm6EYKkLalOh8eSrj9b+ch9H/VBA==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@babel/code-frame": "^7.29.7", "@babel/generator": "^7.29.7", @@ -1503,6 +1504,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "baseline-browser-mapping": "^2.11.25", "caniuse-lite": "^1.0.30001810", diff --git a/server/src/controllers/evaluationController.js b/server/src/controllers/evaluationController.js index e9b7ec8..9c5be2d 100644 --- a/server/src/controllers/evaluationController.js +++ b/server/src/controllers/evaluationController.js @@ -1,33 +1,81 @@ +import mongoose from 'mongoose'; +import Joi from 'joi'; import { Evaluation } from '../models/Evaluation.js'; +const createSchema = Joi.object({ + sessionCode: Joi.string().required(), + score: Joi.number().min(1).max(5).required(), + comment: Joi.string().optional(), + evaluatedBy: Joi.string().hex().length(24).optional() +}); + // GET /api/evaluations -// TODO: implement per README.md section 2. export async function getAllEvaluations(req, res, next) { try { - // TODO + const evaluations = await Evaluation.find().lean(); + 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 + const { id } = req.params; + if (!mongoose.isValidObjectId(id)) { + return res.status(400).json({ message: 'Invalid id' }); + } + + const evaluation = await Evaluation.findById(id).lean(); + 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); + 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 session' }); + } + next(err); + } } // GET /api/evaluations/summary?sessionCode=SS101 -// TODO: implement per README.md section 3. export async function getEvaluationSummary(req, res, next) { try { - // TODO + const { sessionCode } = req.query; + if (!sessionCode || typeof sessionCode !== 'string') { + return res.status(400).json({ message: 'sessionCode is required' }); + } + + const result = await Evaluation.aggregate([ + { $match: { sessionCode } }, + { + $group: { + _id: '$sessionCode', + averageScore: { $avg: '$score' }, + evaluationCount: { $sum: 1 }, + }, + }, + ]); + + if (result.length === 0) { + return res.status(200).json({ sessionCode, averageScore: 0, evaluationCount: 0 }); + } + + res.status(200).json({ + sessionCode, + averageScore: result[0].averageScore, + evaluationCount: result[0].evaluationCount, + }); } catch (err) { next(err); } } diff --git a/server/src/models/Evaluation.js b/server/src/models/Evaluation.js index b7a7d5a..6a1dbc9 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 + sessionCode: { 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({ sessionCode: 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..a2ceee4 100644 --- a/server/src/routes/evaluations.js +++ b/server/src/routes/evaluations.js @@ -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.post('/', createEvaluation); +router.get('/', getAllEvaluations); +// Must come before '/:id', otherwise "summary" is treated as an id. +router.get('/summary', getEvaluationSummary); +router.get('/:id', getEvaluation); -export default router; +export default router; \ No newline at end of file