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-9810

## 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.
97 changes: 72 additions & 25 deletions server/src/controllers/feedbackController.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,80 @@
import { Feedback } from '../models/Feedback.js';
import Feedback from '../models/Feedback.js';

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

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

export const getAllFeedbacks = async (req, res, next) => {
try {
// TODO
} catch (err) { next(err); }
}
const feedbacks = await Feedback.find();

res.status(200).json({ feedbacks });
} catch (error) {
next(error);
}
};

// POST /api/feedback
// TODO: implement per README.md section 2.
export async function createFeedback(req, res, next) {
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',
});
}

res.status(200).json({ feedback });
} catch (error) {
next(error);
}
};

// GET /api/feedback/summary?workshopCode=WS101
// TODO: implement per README.md section 3.
export async function getFeedbackSummary(req, res, next) {
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: '$workshopCode',
averageScore: { $avg: '$score' },
feedbackCount: { $sum: 1 },
},
},
]);

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

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

// TODO: define the Feedback schema per README.md section 1.
import mongoose from 'mongoose';

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;
16 changes: 10 additions & 6 deletions server/src/routes/feedback.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
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();

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

export default router;
export default router;
Loading