From a7a2d63abd24e1c12a5139827493df036e77f700 Mon Sep 17 00:00:00 2001 From: Adham Date: Thu, 1 Oct 2026 16:02:32 +0300 Subject: [PATCH] Complete Task 4.1: Evaluation model, controller, and routes - Evaluation.js: schema with sessionCode (required), score (required, min:1 max:5), comment (optional), evaluatedBy (optional ObjectId ref User), timestamps, and compound unique index on {sessionCode, evaluatedBy} - evaluationController.js: getAllEvaluations, getEvaluation (404 on missing), createEvaluation (201), getEvaluationSummary (aggregate pipeline) - evaluations.js: /summary route declared before /:id to prevent param collision --- .../src/controllers/evaluationController.js | 37 +++++++++++++++---- server/src/models/Evaluation.js | 10 +++-- server/src/routes/evaluations.js | 7 +++- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/server/src/controllers/evaluationController.js b/server/src/controllers/evaluationController.js index e9b7ec8..0809e6d 100644 --- a/server/src/controllers/evaluationController.js +++ b/server/src/controllers/evaluationController.js @@ -1,33 +1,54 @@ import { Evaluation } from '../models/Evaluation.js'; // 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 }).lean(); + 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).lean(); + 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 evaluation = await Evaluation.create(req.body); + res.status(201).json({ evaluation }); } catch (err) { 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) { + return res.status(400).json({ message: 'sessionCode is required' }); + } + + const results = await Evaluation.aggregate([ + { $match: { sessionCode } }, + { + $group: { + _id: '$sessionCode', + averageScore: { $avg: '$score' }, + evaluationCount: { $sum: 1 } + } + } + ]); + + if (results.length === 0) { + return res.json({ sessionCode, averageScore: 0, evaluationCount: 0 }); + } + + const { averageScore, evaluationCount } = results[0]; + res.json({ sessionCode, averageScore, evaluationCount }); } catch (err) { next(err); } } diff --git a/server/src/models/Evaluation.js b/server/src/models/Evaluation.js index b7a7d5a..3e41d1e 100644 --- a/server/src/models/Evaluation.js +++ b/server/src/models/Evaluation.js @@ -1,14 +1,16 @@ 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. +// Compound unique index: one evaluation per (sessionCode, evaluatedBy) +evaluationSchema.index({ sessionCode: 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..eee50b1 100644 --- a/server/src/routes/evaluations.js +++ b/server/src/routes/evaluations.js @@ -8,6 +8,11 @@ import { const router = Router(); -// TODO: wire up the three routes in README.md section 2 and the summary route in section 3. +// Summary must come before /:id so "summary" is not treated as an id +router.get('/summary', getEvaluationSummary); + +router.get('/', getAllEvaluations); +router.get('/:id', getEvaluation); +router.post('/', createEvaluation); export default router;