From 6c7b37ff8ae02c3e7cf0ad1b71cbdc7c29806b37 Mon Sep 17 00:00:00 2001 From: Farida Date: Thu, 1 Oct 2026 15:39:50 +0300 Subject: [PATCH] task 2 --- .../src/controllers/evaluationController.js | 44 ++++++++++++++++--- server/src/models/Evaluation.js | 8 +++- server/src/routes/evaluations.js | 5 ++- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/server/src/controllers/evaluationController.js b/server/src/controllers/evaluationController.js index 3d43598..18f51e0 100644 --- a/server/src/controllers/evaluationController.js +++ b/server/src/controllers/evaluationController.js @@ -1,26 +1,27 @@ 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); } } @@ -28,6 +29,35 @@ 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 (!seminarCode) { + return res.status(400).json({ message: 'seminarCode is required' }); + } + + const stats = await Evaluation.aggregate([ + { $match: { seminarCode } }, + { + $group: { + _id: '$seminarCode', + averageScore: { $avg: '$score' }, + evaluationCount: { $sum: 1 } + } + } + ]); + + if (stats.length === 0) { + return res.json({ + seminarCode, + averageScore: 0, + evaluationCount: 0 + }); + } + + const { averageScore, evaluationCount } = stats[0]; + res.json({ + seminarCode, + averageScore, + evaluationCount + }); } catch (err) { next(err); } } diff --git a/server/src/models/Evaluation.js b/server/src/models/Evaluation.js index b7a7d5a..09a5754 100644 --- a/server/src/models/Evaluation.js +++ b/server/src/models/Evaluation.js @@ -4,11 +4,15 @@ 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. +// Compound uniqueness constraint: a user can evaluate a seminar only once. +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..2abdac3 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.get('/', getAllEvaluations); +router.get('/:id', getEvaluation); +router.post('/', createEvaluation); +router.get('/summary', getEvaluationSummary); export default router;