From 2e8bd5ae901258d7af6cd93e5c67de8e150b8020 Mon Sep 17 00:00:00 2001 From: mernahossamsabra-spec Date: Thu, 1 Oct 2026 16:09:48 +0300 Subject: [PATCH] Implement Seminar Evaluation API: model, controller, and routes --- .../src/controllers/evaluationController.js | 86 +++++++++++++------ server/src/models/Evaluation.js | 34 +++++--- server/src/routes/evaluations.js | 23 ++--- 3 files changed, 98 insertions(+), 45 deletions(-) diff --git a/server/src/controllers/evaluationController.js b/server/src/controllers/evaluationController.js index 3d43598..6cd3ae1 100644 --- a/server/src/controllers/evaluationController.js +++ b/server/src/controllers/evaluationController.js @@ -1,33 +1,69 @@ -import { Evaluation } from '../models/Evaluation.js'; +const Evaluation = require('../models/Evaluation'); -// GET /api/evaluations -// TODO: implement per README.md section 2. -export async function getAllEvaluations(req, res, next) { +exports.createEvaluation = async (req, res, next) => { try { - // TODO - } catch (err) { next(err); } -} + const evaluation = await Evaluation.create(req.body); + res.status(201).json({ evaluation }); + } catch (err) { + next(err); + } +}; -// GET /api/evaluations/:id -// TODO: implement per README.md section 2. -export async function getEvaluation(req, res, next) { +exports.getAllEvaluations = async (req, res, next) => { try { - // TODO - } catch (err) { next(err); } -} + const evaluations = await Evaluation.find(); + res.status(200).json({ evaluations }); + } catch (err) { + next(err); + } +}; -// POST /api/evaluations -// TODO: implement per README.md section 2. -export async function createEvaluation(req, res, next) { +exports.getEvaluation = async (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.status(200).json({ evaluation }); + } catch (err) { + next(err); + } +}; -// GET /api/evaluations/summary?seminarCode=SM101 -// TODO: implement per README.md section 3. -export async function getEvaluationSummary(req, res, next) { +exports.getEvaluationSummary = async (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 summary = await Evaluation.aggregate([ + { $match: { seminarCode: seminarCode } }, + { + $group: { + _id: '$seminarCode', + averageScore: { $avg: '$score' }, + evaluationCount: { $sum: 1 }, + }, + }, + ]); + + if (summary.length === 0) { + return res.status(200).json({ + seminarCode, + averageScore: 0, + evaluationCount: 0, + }); + } + + const result = summary[0]; + res.status(200).json({ + seminarCode: result._id, + averageScore: result.averageScore, + evaluationCount: result.evaluationCount, + }); + } catch (err) { + next(err); + } +}; diff --git a/server/src/models/Evaluation.js b/server/src/models/Evaluation.js index b7a7d5a..82e6afd 100644 --- a/server/src/models/Evaluation.js +++ b/server/src/models/Evaluation.js @@ -1,14 +1,28 @@ -import mongoose from 'mongoose'; +const mongoose = require('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, 'Seminar code is required'], + }, + score: { + type: Number, + required: [true, 'Score is required'], + min: [1, 'Score must be at least 1'], + max: [5, 'Score cannot be more than 5'], + }, + comment: { + type: String, + }, + evaluatedBy: { + type: mongoose.Schema.Types.ObjectId, + ref: 'User', }, - { timestamps: true } -); +}, { + timestamps: true +}); -// TODO: add the compound uniqueness constraint described in README.md section 1. +// Compound unique index: one evaluation per user per seminar +evaluationSchema.index({ seminarCode: 1, evaluatedBy: 1 }, { unique: true }); -export const Evaluation = mongoose.model('Evaluation', evaluationSchema); +module.exports = mongoose.model('Evaluation', evaluationSchema); diff --git a/server/src/routes/evaluations.js b/server/src/routes/evaluations.js index 00ec53e..207645e 100644 --- a/server/src/routes/evaluations.js +++ b/server/src/routes/evaluations.js @@ -1,13 +1,16 @@ -import { Router } from 'express'; -import { - getAllEvaluations, - getEvaluation, - createEvaluation, - getEvaluationSummary -} from '../controllers/evaluationController.js'; +const express = require('express'); +const router = express.Router(); +const evaluationController = require('../controllers/evaluationController'); -const router = Router(); +// Summary endpoint (MUST be before /:id) +router.get('/summary', evaluationController.getEvaluationSummary); -// TODO: wire up the three routes in README.md section 2 and the summary route in section 3. +// General routes +router.route('/') + .get(evaluationController.getAllEvaluations) + .post(evaluationController.createEvaluation); -export default router; +router.route('/:id') + .get(evaluationController.getEvaluation); + +module.exports = router;