From 7bf2ad67d0293cfce7de1a3585bf98f3335e16d9 Mon Sep 17 00:00:00 2001 From: Zeiad Dahi Date: Thu, 1 Oct 2026 14:37:49 +0300 Subject: [PATCH] Initial Pre --- .../src/controllers/evaluationController.js | 109 ++++++++++++++---- server/src/index.js | 1 + server/src/models/Evaluation.js | 27 ++++- server/src/routes/evaluations.js | 11 +- 4 files changed, 121 insertions(+), 27 deletions(-) diff --git a/server/src/controllers/evaluationController.js b/server/src/controllers/evaluationController.js index e9b7ec8..69387e4 100644 --- a/server/src/controllers/evaluationController.js +++ b/server/src/controllers/evaluationController.js @@ -1,33 +1,94 @@ import { Evaluation } from '../models/Evaluation.js'; -// GET /api/evaluations -// TODO: implement per README.md section 2. -export async function getAllEvaluations(req, res, next) { +// POST /api/evaluations +export const createEvaluation = async (req, res, next) => { try { - // TODO - } catch (err) { next(err); } -} + const evaluation = await Evaluation.create(req.body); -// GET /api/evaluations/:id -// TODO: implement per README.md section 2. -export async function getEvaluation(req, res, next) { - try { - // TODO - } catch (err) { next(err); } -} + return res.status(201).json({ + evaluation + }); + } catch (err) { + next(err); + } +}; -// POST /api/evaluations -// TODO: implement per README.md section 2. -export async function createEvaluation(req, res, next) { +// GET /api/evaluations +export const getAllEvaluations = async (req, res, next) => { try { - // TODO - } catch (err) { next(err); } -} + const evaluations = await Evaluation.find(); + + return res.status(200).json({ + evaluations + }); + } catch (err) { + next(err); + } +}; // GET /api/evaluations/summary?sessionCode=SS101 -// TODO: implement per README.md section 3. -export async function getEvaluationSummary(req, res, next) { +export const getEvaluationSummary = async (req, res, next) => { + try { + const { sessionCode } = req.query; + + if (!sessionCode) { + return res.status(400).json({ + message: 'sessionCode is required' + }); + } + + const result = await Evaluation.aggregate([ + { + $match: { + sessionCode + } + }, + { + $group: { + _id: '$sessionCode', + averageScore: { + $avg: '$score' + }, + evaluationCount: { + $sum: 1 + } + } + } + ]); + + if (result.length === 0) { + return res.status(200).json({ + sessionCode, + averageScore: 0, + evaluationCount: 0 + }); + } + + return res.status(200).json({ + sessionCode, + averageScore: result[0].averageScore, + evaluationCount: result[0].evaluationCount + }); + } catch (err) { + next(err); + } +}; + +// GET /api/evaluations/:id +export const 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' + }); + } + + return res.status(200).json({ + evaluation + }); + } catch (err) { + next(err); + } +}; \ No newline at end of file diff --git a/server/src/index.js b/server/src/index.js index 320417a..18c79e8 100644 --- a/server/src/index.js +++ b/server/src/index.js @@ -7,6 +7,7 @@ const PORT = process.env.PORT || 4000; async function bootstrap() { await connectDB(); + app.listen(PORT, () => { console.log(`API listening on http://localhost:${PORT}`); }); diff --git a/server/src/models/Evaluation.js b/server/src/models/Evaluation.js index b7a7d5a..07a1199 100644 --- a/server/src/models/Evaluation.js +++ b/server/src/models/Evaluation.js @@ -1,13 +1,38 @@ + 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 } ); +evaluationSchema.index( + { sessionCode: 1, evaluatedBy: 1 }, + { unique: true } +); + // TODO: add the compound uniqueness constraint described in README.md section 1. diff --git a/server/src/routes/evaluations.js b/server/src/routes/evaluations.js index 00ec53e..e43a2d6 100644 --- a/server/src/routes/evaluations.js +++ b/server/src/routes/evaluations.js @@ -8,6 +8,13 @@ import { const router = Router(); -// TODO: wire up the three routes in README.md section 2 and the summary route in section 3. +router.post('/', createEvaluation); -export default router; +router.get('/', getAllEvaluations); + +// IMPORTANT: put /summary BEFORE /:id +router.get('/summary', getEvaluationSummary); + +router.get('/:id', getEvaluation); + +export default router; \ No newline at end of file