From 47ac7ebdbee1618a53c90e976e13715d94c9624f Mon Sep 17 00:00:00 2001 From: KhaledAbdel8 Date: Thu, 1 Oct 2026 15:41:25 +0300 Subject: [PATCH] used claude code to do this task --- .../src/controllers/evaluationController.js | 83 ++++++++++++++++--- server/src/models/Evaluation.js | 30 +++++-- server/src/routes/evaluations.js | 17 ++-- 3 files changed, 100 insertions(+), 30 deletions(-) diff --git a/server/src/controllers/evaluationController.js b/server/src/controllers/evaluationController.js index 3d43598..dd2204a 100644 --- a/server/src/controllers/evaluationController.js +++ b/server/src/controllers/evaluationController.js @@ -1,33 +1,90 @@ +import Joi from 'joi'; import { Evaluation } from '../models/Evaluation.js'; +const createSchema = Joi.object({ + seminarCode: 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 - } catch (err) { next(err); } + 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 - } catch (err) { next(err); } + const evaluation = await Evaluation.findById(req.params.id); + 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 - } 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) { + // Handle MongoDB duplicate key error for compound index + if (err.code === 11000) { + return res.status(409).json({ message: 'User has already evaluated this seminar' }); + } + next(err); + } } -// GET /api/evaluations/summary?seminarCode=SM101 -// TODO: implement per README.md section 3. +// GET /api/evaluations/summary?seminarCode=... 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: seminarCode } }, + { + $group: { + _id: '$seminarCode', + averageScore: { $avg: '$score' }, + evaluationCount: { $sum: 1 } + } + } + ]); + + if (result.length === 0) { + return res.json({ + seminarCode: seminarCode, + averageScore: 0, + evaluationCount: 0 + }); + } + + const summary = result[0]; + res.json({ + seminarCode: summary._id, + averageScore: summary.averageScore, + evaluationCount: summary.evaluationCount + }); + } catch (err) { + next(err); + } } diff --git a/server/src/models/Evaluation.js b/server/src/models/Evaluation.js index b7a7d5a..c534bb8 100644 --- a/server/src/models/Evaluation.js +++ b/server/src/models/Evaluation.js @@ -1,14 +1,28 @@ import mongoose from 'mongoose'; -// TODO: define the Evaluation schema per README.md section 1. - -const evaluationSchema = new mongoose.Schema( - { - // TODO +const evaluationSchema = new mongoose.Schema({ + seminarCode: { + type: String, + required: true + }, + score: { + type: Number, + required: true, + min: 1, + max: 5 + }, + comment: { + type: String, + optional: true }, - { timestamps: true } -); + evaluatedBy: { + type: mongoose.Schema.Types.ObjectId, + ref: 'User', + optional: true + } +}, { timestamps: true }); -// TODO: add the compound uniqueness constraint described in README.md section 1. +// Compound unique index to prevent a user from evaluating the same seminar twice +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..fde5a90 100644 --- a/server/src/routes/evaluations.js +++ b/server/src/routes/evaluations.js @@ -1,13 +1,12 @@ -import { Router } from 'express'; -import { - getAllEvaluations, - getEvaluation, - createEvaluation, - getEvaluationSummary -} from '../controllers/evaluationController.js'; +import express from 'express'; +import * as evaluationController from '../controllers/evaluationController.js'; -const router = Router(); +const router = express.Router(); -// TODO: wire up the three routes in README.md section 2 and the summary route in section 3. +// Summary must be defined before :id to avoid conflict +router.get('/summary', evaluationController.getEvaluationSummary); +router.get('/', evaluationController.getAllEvaluations); +router.post('/', evaluationController.createEvaluation); +router.get('/:id', evaluationController.getEvaluation); export default router;