Skip to content
Open

POST #124

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 73 additions & 24 deletions server/src/controllers/evaluationController.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,82 @@
import Joi from 'joi';
import { Evaluation } from '../models/Evaluation.js';

// GET /api/evaluations
// TODO: implement per README.md section 2.
export async function getAllEvaluations(req, res, next) {
const evaluationSchema = Joi.object({
seminarCode: Joi.string().required(),
score: Joi.number().integer().min(1).max(5).required(),
comment: Joi.string().optional(),
evaluatedBy: Joi.string().hex().length(24).optional(),
});

export const createEvaluation = async (req, res, next) => {
try {
// TODO
} catch (err) { next(err); }
}
const { error, value } = evaluationSchema.validate(req.body);
if (error) {
return res.status(400).json({ message: error.details[0].message });
}

const evaluation = await Evaluation.create(value);
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) {
export const getAllEvaluations = async (req, res, next) => {
try {
// TODO
} catch (err) { next(err); }
}
const evaluations = await Evaluation.find().lean();
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) {
export const getEvaluation = async (req, res, next) => {
try {
// TODO
} catch (err) { next(err); }
}
const evaluation = await Evaluation.findById(req.params.id).lean();
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) {
export const 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 } },
{
$group: {
_id: null,
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,
averageScore: result.averageScore,
evaluationCount: result.evaluationCount,
});
} catch (err) {
next(err);
}
};
23 changes: 19 additions & 4 deletions server/src/models/Evaluation.js
Original file line number Diff line number Diff line change
@@ -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 to prevent duplicate evaluations by the same user for the same seminar
evaluationSchema.index({ seminarCode: 1, evaluatedBy: 1 }, { unique: true });

export const Evaluation = mongoose.model('Evaluation', evaluationSchema);
14 changes: 9 additions & 5 deletions server/src/routes/evaluations.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Router } from 'express';
import express from 'express';
import {
createEvaluation,
getAllEvaluations,
getEvaluation,
createEvaluation,
getEvaluationSummary
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.
// Summary must be defined before /:id to avoid collision
router.get('/summary', getEvaluationSummary);
router.get('/', getAllEvaluations);
router.get('/:id', getEvaluation);
router.post('/', createEvaluation);

export default router;
Loading