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
30 changes: 24 additions & 6 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
"scripts": {
"dev": "nodemon src/index.js",
"start": "node src/index.js",
"test": "NODE_OPTIONS=--experimental-vm-modules jest --runInBand",
"grade": "NODE_OPTIONS=--experimental-vm-modules jest --runInBand --verbose"
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --runInBand",
"grade": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --runInBand --verbose"
},
"dependencies": {
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"cross-env": "^10.1.0",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"joi": "^17.12.3",
"mongoose": "^8.6.0",
"morgan": "^1.10.0",
"joi": "^17.12.3"
"morgan": "^1.10.0"
},
"devDependencies": {
"jest": "^29.7.0",
Expand Down
82 changes: 64 additions & 18 deletions server/src/controllers/evaluationController.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,79 @@
import Joi from 'joi';
import { Evaluation } from '../models/Evaluation.js';

const createSchema = Joi.object({
sessionCode: Joi.string().required(),
score: Joi.number().integer().min(1).max(5).required(),
comment: Joi.string().optional(),
evaluatedBy: Joi.string().hex().length(24).optional(),
});

// POST /api/evaluations
export async function createEvaluation(req, res, next) {
try {
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) {
next(err);
}
}

// 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).lean();
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); }
}

// GET /api/evaluations/summary?sessionCode=SS101
// TODO: implement per README.md section 3.
// GET /api/evaluations/summary?sessionCode=XXX
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 stats = await Evaluation.aggregate([
{ $match: { sessionCode } },
{
$group: {
_id: '$sessionCode',
averageScore: { $avg: '$score' },
evaluationCount: { $sum: 1 },
},
},
]);

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

const result = stats[0];
res.json({
sessionCode: result._id,
averageScore: result.averageScore,
evaluationCount: result.evaluationCount,
});
} catch (err) {
next(err);
}
}
28 changes: 20 additions & 8 deletions server/src/models/Evaluation.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
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({
sessionCode: {
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.
// Compound unique index to prevent duplicate evaluations by the same user for the same session
evaluationSchema.index({ sessionCode: 1, evaluatedBy: 1 }, { unique: true });

export const Evaluation = mongoose.model('Evaluation', evaluationSchema);
17 changes: 12 additions & 5 deletions server/src/routes/evaluations.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
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();

// summary must be before :id
router.get('/summary', getEvaluationSummary);

router.route('/')
.get(getAllEvaluations)
.post(createEvaluation);

// TODO: wire up the three routes in README.md section 2 and the summary route in section 3.
router.get('/:id', getEvaluation);

export default router;
Loading