From 1ff7ce922876528f2610d1adf9b175fe7cdd9378 Mon Sep 17 00:00:00 2001 From: aly Date: Thu, 1 Oct 2026 15:44:21 +0300 Subject: [PATCH 1/2] "implemented task 2" --- server/package-lock.json | 2 + .../src/controllers/evaluationController.js | 92 ++++++++++++++++--- server/src/models/Evaluation.js | 25 ++++- server/src/routes/evaluations.js | 11 ++- 4 files changed, 109 insertions(+), 21 deletions(-) diff --git a/server/package-lock.json b/server/package-lock.json index b819a71..0c230a0 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -53,6 +53,7 @@ "integrity": "sha512-RgHBCvtjbOK2gXSNBNIkNoEc9qoVEtau3hj8gEqKQuL3HZAibKarWFEI3Lfm6EYKkLalOh8eSrj9b+ch9H/VBA==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@babel/code-frame": "^7.29.7", "@babel/generator": "^7.29.7", @@ -1503,6 +1504,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "baseline-browser-mapping": "^2.11.25", "caniuse-lite": "^1.0.30001810", diff --git a/server/src/controllers/evaluationController.js b/server/src/controllers/evaluationController.js index 3d43598..e176ceb 100644 --- a/server/src/controllers/evaluationController.js +++ b/server/src/controllers/evaluationController.js @@ -1,33 +1,99 @@ +import Joi from 'joi'; +import mongoose from 'mongoose'; import { Evaluation } from '../models/Evaluation.js'; +const evaluationSchema = Joi.object({ + seminarCode: Joi.string().required(), + score: Joi.number().min(1).max(5).required(), + comment: Joi.string().optional().allow('', null), + evaluatedBy: Joi.string().hex().length(24).optional().allow(null), +}); + // 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(); + res.status(200).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 { id } = req.params; + + // Handle invalid ObjectId strings gracefully with a 404 + if (!mongoose.Types.ObjectId.isValid(id)) { + return res.status(404).json({ message: 'Evaluation not found' }); + } + + const evaluation = await Evaluation.findById(id); + + if (!evaluation) { + return res.status(404).json({ message: 'Evaluation not found' }); + } + + res.status(200).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 } = evaluationSchema.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 duplicate key error for compound unique index + if (err.code === 11000) { + return res.status(409).json({ message: 'Evaluation already exists for this seminar' }); + } + next(err); + } } // GET /api/evaluations/summary?seminarCode=SM101 -// TODO: implement per README.md section 3. 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 } }, + { + $group: { + _id: '$seminarCode', + averageScore: { \(avg: '\)score' }, + evaluationCount: { $sum: 1 }, + }, + }, + ]); + + if (!result || result.length === 0) { + return res.status(200).json({ + seminarCode, + averageScore: 0, + evaluationCount: 0, + }); + } + + res.status(200).json({ + seminarCode: result[0]._id, + averageScore: result[0].averageScore, + evaluationCount: result[0].evaluationCount, + }); + } catch (err) { + next(err); + } +} \ No newline at end of file diff --git a/server/src/models/Evaluation.js b/server/src/models/Evaluation.js index b7a7d5a..828cebb 100644 --- a/server/src/models/Evaluation.js +++ b/server/src/models/Evaluation.js @@ -1,14 +1,29 @@ import mongoose from 'mongoose'; -// TODO: define the Evaluation schema per README.md section 1. - 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 unique index on { seminarCode: 1, evaluatedBy: 1 } +evaluationSchema.index({ seminarCode: 1, evaluatedBy: 1 }, { unique: true }); -export const Evaluation = mongoose.model('Evaluation', evaluationSchema); +export const Evaluation = mongoose.model('Evaluation', evaluationSchema); \ No newline at end of file diff --git a/server/src/routes/evaluations.js b/server/src/routes/evaluations.js index 00ec53e..202e2f3 100644 --- a/server/src/routes/evaluations.js +++ b/server/src/routes/evaluations.js @@ -3,11 +3,16 @@ import { getAllEvaluations, getEvaluation, createEvaluation, - getEvaluationSummary + getEvaluationSummary, } from '../controllers/evaluationController.js'; const router = Router(); -// TODO: wire up the three routes in README.md section 2 and the summary route in section 3. +// /summary MUST precede /:id to prevent Express from treating 'summary' as an ID parameter +router.get('/summary', getEvaluationSummary); -export default router; +router.get('/', getAllEvaluations); +router.get('/:id', getEvaluation); +router.post('/', createEvaluation); + +export default router; \ No newline at end of file From 089348e5e908d3ae7f5e45afb67f8f83878797b6 Mon Sep 17 00:00:00 2001 From: aly Date: Thu, 1 Oct 2026 15:56:37 +0300 Subject: [PATCH 2/2] "fixed errors " --- .../src/controllers/evaluationController.js | 76 ++++++------------- server/src/models/Evaluation.js | 22 +----- server/src/routes/evaluations.js | 9 +-- 3 files changed, 30 insertions(+), 77 deletions(-) diff --git a/server/src/controllers/evaluationController.js b/server/src/controllers/evaluationController.js index e176ceb..e2e6095 100644 --- a/server/src/controllers/evaluationController.js +++ b/server/src/controllers/evaluationController.js @@ -2,59 +2,44 @@ import Joi from 'joi'; import mongoose from 'mongoose'; import { Evaluation } from '../models/Evaluation.js'; -const evaluationSchema = Joi.object({ - seminarCode: Joi.string().required(), +const createSchema = Joi.object({ + seminarCode: Joi.string().trim().required(), score: Joi.number().min(1).max(5).required(), - comment: Joi.string().optional().allow('', null), - evaluatedBy: Joi.string().hex().length(24).optional().allow(null), + comment: Joi.string().allow(''), + evaluatedBy: Joi.string().hex().length(24) }); // GET /api/evaluations export async function getAllEvaluations(req, res, next) { try { - const evaluations = await Evaluation.find(); + const evaluations = await Evaluation.find().sort({ createdAt: -1 }); res.status(200).json({ evaluations }); - } catch (err) { - next(err); - } + } catch (err) { next(err); } } // GET /api/evaluations/:id export async function getEvaluation(req, res, next) { try { - const { id } = req.params; - - // Handle invalid ObjectId strings gracefully with a 404 - if (!mongoose.Types.ObjectId.isValid(id)) { - return res.status(404).json({ message: 'Evaluation not found' }); + if (!mongoose.isValidObjectId(req.params.id)) { + return res.status(400).json({ message: 'Invalid evaluation id' }); } - - const evaluation = await Evaluation.findById(id); - - if (!evaluation) { - return res.status(404).json({ message: 'Evaluation not found' }); - } - + 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); - } + } catch (err) { next(err); } } // POST /api/evaluations export async function createEvaluation(req, res, next) { try { - const { value, error } = evaluationSchema.validate(req.body); - if (error) { - return res.status(400).json({ message: error.message }); - } + 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) { - // Handle duplicate key error for compound unique index if (err.code === 11000) { - return res.status(409).json({ message: 'Evaluation already exists for this seminar' }); + return res.status(409).json({ message: 'This user has already evaluated this seminar' }); } next(err); } @@ -64,36 +49,19 @@ export async function createEvaluation(req, res, next) { export async function getEvaluationSummary(req, res, next) { try { const { seminarCode } = req.query; - - if (!seminarCode) { + if (typeof seminarCode !== 'string' || !seminarCode.trim()) { return res.status(400).json({ message: 'seminarCode is required' }); } - const result = await Evaluation.aggregate([ + const [result] = await Evaluation.aggregate([ { $match: { seminarCode } }, - { - $group: { - _id: '$seminarCode', - averageScore: { \(avg: '\)score' }, - evaluationCount: { $sum: 1 }, - }, - }, + { $group: { _id: null, averageScore: { $avg: '$score' }, evaluationCount: { $sum: 1 } } } ]); - if (!result || result.length === 0) { - return res.status(200).json({ - seminarCode, - averageScore: 0, - evaluationCount: 0, - }); - } - res.status(200).json({ - seminarCode: result[0]._id, - averageScore: result[0].averageScore, - evaluationCount: result[0].evaluationCount, + seminarCode, + averageScore: result ? result.averageScore : 0, + evaluationCount: result ? result.evaluationCount : 0 }); - } catch (err) { - next(err); - } -} \ No newline at end of file + } catch (err) { next(err); } +} \ No newline at end of file diff --git a/server/src/models/Evaluation.js b/server/src/models/Evaluation.js index 828cebb..e05d4f2 100644 --- a/server/src/models/Evaluation.js +++ b/server/src/models/Evaluation.js @@ -2,28 +2,14 @@ import mongoose from 'mongoose'; const evaluationSchema = new mongoose.Schema( { - 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', - }, + 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 } ); -// Compound unique index on { seminarCode: 1, evaluatedBy: 1 } evaluationSchema.index({ seminarCode: 1, evaluatedBy: 1 }, { unique: true }); export const Evaluation = mongoose.model('Evaluation', evaluationSchema); \ No newline at end of file diff --git a/server/src/routes/evaluations.js b/server/src/routes/evaluations.js index 202e2f3..0d1c68e 100644 --- a/server/src/routes/evaluations.js +++ b/server/src/routes/evaluations.js @@ -3,16 +3,15 @@ import { getAllEvaluations, getEvaluation, createEvaluation, - getEvaluationSummary, + getEvaluationSummary } from '../controllers/evaluationController.js'; const router = Router(); -// /summary MUST precede /:id to prevent Express from treating 'summary' as an ID parameter -router.get('/summary', getEvaluationSummary); - router.get('/', getAllEvaluations); +// Must come before '/:id' so "summary" is not treated as an id. +router.get('/summary', getEvaluationSummary); router.get('/:id', getEvaluation); router.post('/', createEvaluation); -export default router; \ No newline at end of file +export default router \ No newline at end of file