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
10 changes: 5 additions & 5 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
## Submission
<!-- Replace the line below with your required identifier token. -->
XX-XXXXX TXX
61-5349 T17

## Checklist
- [ ] I worked in my fork and am submitting to the original repository.
- [ ] I ran the automated tests.
- [ ] I did not commit credentials, .env, or node_modules.
- [ ] I understand that AI tools are allowed and that I am responsible for the submitted code.
- [X ] I worked in my fork and am submitting to the original repository.
- [X ] I ran the automated tests.
- [ X] I did not commit credentials, .env, or node_modules.
- [X ] I understand that AI tools are allowed and that I am responsible for the submitted code.
139 changes: 114 additions & 25 deletions server/src/controllers/feedbackController.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,122 @@
import { Feedback } from '../models/Feedback.js';
import Joi from 'joi';
import mongoose from 'mongoose';
import Feedback from '../models/Feedback.js';

// GET /api/feedback
// TODO: implement per README.md section 2.
export async function getAllFeedbacks(req, res, next) {
const feedbackSchema = Joi.object({
workshopCode: Joi.string().required(),

score: Joi.number()
.integer()
.min(1)
.max(5)
.required(),

comment: Joi.string().allow('').optional(),

submittedBy: Joi.string()
.custom((value, helpers) => {
if (!mongoose.Types.ObjectId.isValid(value)) {
return helpers.error('any.invalid');
}

return value;
})
.optional(),
});

export const createFeedback = async (req, res, next) => {
try {
// TODO
} catch (err) { next(err); }
}
const { error, value } = feedbackSchema.validate(req.body);

if (error) {
return res.status(400).json({
message: error.details[0].message,
});
}

const feedback = await Feedback.create(value);

return res.status(201).json({
feedback,
});
} catch (err) {
return next(err);
}
};

// GET /api/feedback/:id
// TODO: implement per README.md section 2.
export async function getFeedback(req, res, next) {
export const getAllFeedbacks = async (req, res, next) => {
try {
// TODO
} catch (err) { next(err); }
}
const feedbacks = await Feedback.find();

// POST /api/feedback
// TODO: implement per README.md section 2.
export async function createFeedback(req, res, next) {
return res.status(200).json({
feedbacks,
});
} catch (err) {
return next(err);
}
};

export const getFeedback = async (req, res, next) => {
try {
// TODO
} catch (err) { next(err); }
}
const feedback = await Feedback.findById(req.params.id);

if (!feedback) {
return res.status(404).json({
message: 'Feedback not found',
});
}

// GET /api/feedback/summary?workshopCode=WS101
// TODO: implement per README.md section 3.
export async function getFeedbackSummary(req, res, next) {
return res.status(200).json({
feedback,
});
} catch (err) {
return next(err);
}
};

export const getFeedbackSummary = async (req, res, next) => {
try {
// TODO
} catch (err) { next(err); }
}
const { workshopCode } = req.query;

if (!workshopCode) {
return res.status(400).json({
message: 'workshopCode is required',
});
}

const result = await Feedback.aggregate([
{
$match: {
workshopCode,
},
},
{
$group: {
_id: null,
averageScore: {
$avg: '$score',
},
feedbackCount: {
$sum: 1,
},
},
},
]);

if (result.length === 0) {
return res.status(200).json({
workshopCode,
averageScore: 0,
feedbackCount: 0,
});
}

return res.status(200).json({
workshopCode,
averageScore: result[0].averageScore,
feedbackCount: result[0].feedbackCount,
});
} catch (err) {
return next(err);
}
};
37 changes: 31 additions & 6 deletions server/src/models/Feedback.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
import mongoose from 'mongoose';

// TODO: define the Feedback schema per README.md section 1.

const feedbackSchema = new mongoose.Schema(
{
// TODO
workshopCode: {
type: String,
required: true,
},

score: {
type: Number,
required: true,
min: 1,
max: 5,
},

comment: {
type: String,
},

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

feedbackSchema.index(
{ workshopCode: 1, submittedBy: 1 },
{ unique: true }
);

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

export const Feedback = mongoose.model('Feedback', feedbackSchema);
export { Feedback };
export default Feedback;
18 changes: 12 additions & 6 deletions server/src/routes/feedback.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { Router } from 'express';
import express from 'express';
import {
createFeedback,
getAllFeedbacks,
getFeedback,
createFeedback,
getFeedbackSummary
getFeedbackSummary,
} from '../controllers/feedbackController.js';

const router = Router();
const router = express.Router();

router.post('/', createFeedback);

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

router.get('/', getAllFeedbacks);

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

export default router;
export default router;
Loading