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
2 changes: 2 additions & 0 deletions server/package-lock.json

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

114 changes: 89 additions & 25 deletions server/src/controllers/evaluationController.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,97 @@
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) {
const createEvaluation = async (req, res, next) => {
try {
// TODO
} catch (err) { next(err); }
}
const evaluation = await Evaluation.create(req.body);

// GET /api/evaluations/:id
// TODO: implement per README.md section 2.
export async function getEvaluation(req, res, next) {
res.status(201).json({
evaluation,
});
} catch (err) {
next(err);
}
};

const getAllEvaluations = async (req, res, next) => {
try {
// TODO
} catch (err) { next(err); }
}
const evaluations = await Evaluation.find();

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) {
const getEvaluation = async (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',
});
}

// GET /api/evaluations/summary?sessionCode=SS101
// TODO: implement per README.md section 3.
export async function getEvaluationSummary(req, res, next) {
res.status(200).json({
evaluation,
});
} catch (err) {
next(err);
}
};

const getEvaluationSummary = async (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,
},
},
},
]);

let averageScore = 0;
let evaluationCount = 0;

if (result.length > 0) {
averageScore = result[0].averageScore;
evaluationCount = result[0].evaluationCount;
}

res.status(200).json({
sessionCode,
averageScore,
evaluationCount,
});
} catch (err) {
next(err);
}
};

export {
createEvaluation,
getAllEvaluations,
getEvaluation,
getEvaluationSummary,
};
36 changes: 30 additions & 6 deletions server/src/models/Evaluation.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
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,
},

evaluatedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
},
{ timestamps: true }
{
timestamps: true,
}
);

evaluationSchema.index(
{ sessionCode: 1, evaluatedBy: 1 },
{ unique: true }
);

// TODO: add the compound uniqueness constraint described in README.md section 1.
const Evaluation = mongoose.model('Evaluation', evaluationSchema);

export const Evaluation = mongoose.model('Evaluation', evaluationSchema);
export default Evaluation;
19 changes: 13 additions & 6 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();

router.post('/', createEvaluation);

router.get('/', getAllEvaluations);

router.get('/summary', getEvaluationSummary);

// 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;
export default router;
Loading