diff --git a/Task2-ai-assisted-coding-lab4.2 b/Task2-ai-assisted-coding-lab4.2 new file mode 160000 index 0000000..fe8efa3 --- /dev/null +++ b/Task2-ai-assisted-coding-lab4.2 @@ -0,0 +1 @@ +Subproject commit fe8efa33df8783d5278af2f867b88a1740292363 diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..b5c5c10 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "Task2-ai-assisted-coding-lab4.2", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} 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..c51401b 100644 --- a/server/src/controllers/evaluationController.js +++ b/server/src/controllers/evaluationController.js @@ -1,33 +1,76 @@ +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 + 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); + 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) { + 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 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: null, + 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..7a38734 100644 --- a/server/src/models/Evaluation.js +++ b/server/src/models/Evaluation.js @@ -1,14 +1,25 @@ 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, + }, + evaluatedBy: { + type: mongoose.Schema.Types.ObjectId, + ref: 'User', }, - { timestamps: true } -); +}, { timestamps: true }); -// TODO: add the compound uniqueness constraint described in README.md section 1. +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..ff1a03c 100644 --- a/server/src/routes/evaluations.js +++ b/server/src/routes/evaluations.js @@ -1,4 +1,4 @@ -import { Router } from 'express'; +import express from 'express'; import { getAllEvaluations, getEvaluation, @@ -6,8 +6,13 @@ import { getEvaluationSummary } 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. +router.get('/summary', getEvaluationSummary); +router.route('/') + .get(getAllEvaluations) + .post(createEvaluation); + +router.get('/:id', getEvaluation); export default router;