From 7fbdf54803f5467dd85e5d80dbb07e9666bca048 Mon Sep 17 00:00:00 2001 From: Salma Mahran Date: Thu, 1 Oct 2026 15:46:09 +0300 Subject: [PATCH] postlab 61-1186 t16 --- .../src/controllers/evaluationController.js | 39 +++++++++++++++++-- server/src/models/Evaluation.js | 10 ++++- server/src/routes/evaluations.js | 8 +++- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/server/src/controllers/evaluationController.js b/server/src/controllers/evaluationController.js index 3d43598..38a681f 100644 --- a/server/src/controllers/evaluationController.js +++ b/server/src/controllers/evaluationController.js @@ -1,10 +1,18 @@ import { Evaluation } from '../models/Evaluation.js'; +import Joi from 'joi'; +const createSchema = Joi.object({ + seminarCode: Joi.string().trim().required(), + score: Joi.number().min(1).max(5).required(), + comment: Joi.string().allow('').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().sort({ createdAt: -1 }); + res.status(200).json({ evaluations }); } catch (err) { next(err); } } @@ -12,7 +20,12 @@ export async function getAllEvaluations(req, res, next) { // 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); } } @@ -20,7 +33,11 @@ export async function getEvaluation(req, res, next) { // TODO: implement per README.md section 2. export async function createEvaluation(req, res, next) { try { - // TODO + 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) { next(err); } } @@ -28,6 +45,20 @@ export async function createEvaluation(req, res, next) { // 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); } } diff --git a/server/src/models/Evaluation.js b/server/src/models/Evaluation.js index b7a7d5a..49071ce 100644 --- a/server/src/models/Evaluation.js +++ b/server/src/models/Evaluation.js @@ -4,11 +4,17 @@ import mongoose from 'mongoose'; const evaluationSchema = new mongoose.Schema( { - // TODO + seminarCode: { type: String, required: true, trim: 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, partialFilterExpression: { evaluatedBy: { $type: 'objectId' } } } +); export const Evaluation = mongoose.model('Evaluation', evaluationSchema); diff --git a/server/src/routes/evaluations.js b/server/src/routes/evaluations.js index 00ec53e..bedd7fc 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.get('/', getAllEvaluations); +router.post('/', createEvaluation); +// Must be registered 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