Skip to content
Open
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
83 changes: 70 additions & 13 deletions server/src/controllers/evaluationController.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,90 @@
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
} catch (err) { next(err); }
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
} 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
// 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) {
// Handle MongoDB duplicate key error for compound index
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?seminarCode=...
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: seminarCode } },
{
$group: {
_id: '$seminarCode',
averageScore: { $avg: '$score' },
evaluationCount: { $sum: 1 }
}
}
]);

if (result.length === 0) {
return res.json({
seminarCode: seminarCode,
averageScore: 0,
evaluationCount: 0
});
}

const summary = result[0];
res.json({
seminarCode: summary._id,
averageScore: summary.averageScore,
evaluationCount: summary.evaluationCount
});
} catch (err) {
next(err);
}
}
30 changes: 22 additions & 8 deletions server/src/models/Evaluation.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
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,
optional: true
},
{ timestamps: true }
);
evaluatedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
optional: true
}
}, { timestamps: true });

// TODO: add the compound uniqueness constraint described in README.md section 1.
// Compound unique index to prevent a user from evaluating the same seminar twice
evaluationSchema.index({ seminarCode: 1, evaluatedBy: 1 }, { unique: true });

export const Evaluation = mongoose.model('Evaluation', evaluationSchema);
17 changes: 8 additions & 9 deletions server/src/routes/evaluations.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Router } from 'express';
import {
getAllEvaluations,
getEvaluation,
createEvaluation,
getEvaluationSummary
} from '../controllers/evaluationController.js';
import express from 'express';
import * as evaluationController 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 conflict
router.get('/summary', evaluationController.getEvaluationSummary);
router.get('/', evaluationController.getAllEvaluations);
router.post('/', evaluationController.createEvaluation);
router.get('/:id', evaluationController.getEvaluation);

export default router;
Loading