diff --git a/server/src/controllers/evaluationController.js b/server/src/controllers/evaluationController.js index e9b7ec8..2fa6db6 100644 --- a/server/src/controllers/evaluationController.js +++ b/server/src/controllers/evaluationController.js @@ -1,11 +1,15 @@ -import { Evaluation } from '../models/Evaluation.js'; +import { Evaluation } from "../models/Evaluation.js"; // 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().lean(); + res.json({ evaluations }); + } catch (err) { + next(err); + } } // GET /api/evaluations/:id @@ -13,7 +17,13 @@ export async function getAllEvaluations(req, res, next) { 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 @@ -21,7 +31,11 @@ export async function getEvaluation(req, res, next) { export async function createEvaluation(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/summary?sessionCode=SS101 @@ -29,5 +43,19 @@ export async function createEvaluation(req, res, next) { export async function getEvaluationSummary(req, res, next) { try { // TODO - } catch (err) { next(err); } + const { sessionCode } = req.query; +if (!sessionCode) return res.status(400).json({ message: 'sessionCode is required' }); + +const result = await Evaluation.aggregate([ + { $match: { sessionCode } }, + { $group: { _id: null, averageScore: { $avg: '$score' }, evaluationCount: { $sum: 1 } } } +]); + +if (result.length === 0) { + return res.json({ sessionCode, averageScore: 0, evaluationCount: 0 }); +} +res.json({ sessionCode, averageScore: result[0].averageScore, evaluationCount: result[0].evaluationCount }); + } catch (err) { + next(err); + } } diff --git a/server/src/models/Evaluation.js b/server/src/models/Evaluation.js index b7a7d5a..975f77a 100644 --- a/server/src/models/Evaluation.js +++ b/server/src/models/Evaluation.js @@ -1,14 +1,19 @@ 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,required:false}, + 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. export const Evaluation = mongoose.model('Evaluation', evaluationSchema); diff --git a/server/src/routes/evaluations.js b/server/src/routes/evaluations.js index 00ec53e..82fa571 100644 --- a/server/src/routes/evaluations.js +++ b/server/src/routes/evaluations.js @@ -1,13 +1,16 @@ -import { Router } from 'express'; +import { Router } from "express"; import { getAllEvaluations, getEvaluation, createEvaluation, - getEvaluationSummary -} from '../controllers/evaluationController.js'; + 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. +router.get("/summary", getEvaluationSummary); +router.get("/", getAllEvaluations); +router.get("/:id", getEvaluation); +router.post("/", createEvaluation); export default router;