Skip to content

Added controllers and routes to create and fetch judge's feedback #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2025
Merged
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
3 changes: 2 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const practiceSlot = require("./routes/practiceSlot");
const notifications = require("./routes/notifications");
const settings = require("./routes/settings");
const slotting = require("./routes/slotting");
const feedback = require("./routes/feedback");

app.use("/colleges", collegesRouter);
app.use("/events", eventsRouter);
Expand All @@ -88,7 +89,7 @@ app.use("/practiceSlots", practiceSlot);
app.use("/notifications", notifications);
app.use("/settings", settings);
app.use("/slotting", slotting);

app.use("/feedback", feedback)

// Error handlers
app.use(handle404);
Expand Down
60 changes: 60 additions & 0 deletions src/controllers/feedback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const Feedback = require("../models/Feedback");

exports.create = async(req, res) => {
try{
const {rating, comment, signature, judge, event} = req.body;
if(!rating || !comment, !signature || !judge || !event){
return res.status(400).json({
status: 400,
message: "Rating, comment, signature, judge id and event id are required ⁉️",
});
}
const feedback = await Feedback({
rating,
comment,
signature,
judge,
event
})
const result = await feedback.save();
return res.status(200).json({
status: 200,
message: "Success",
data: result
});
}catch(e){
return res.status(501).json({
status: 501,
message: "Internal Server Error ❌",
});
}
}

exports.getFeedbackForAnEvent = async(req, res) => {
try{
const {event} = req.body;
if(!event){
return res.status(400).json({
status: 400,
message: "Event id is required ⁉️",
});
}
const feedback = await Feedback.find({ event }).populate("judge", "name");
if(!feedback){
return res.status(404).json({
status: 404,
message: "No feedback found for the event 🚫",
});
}
return res.status(200).json({
status: 200,
message: "Success",
data: feedback
});
}catch(e){
return res.status(501).json({
status: 501,
message: "Internal Server Error ❌",
});
}
}
14 changes: 8 additions & 6 deletions src/controllers/judges.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use strict";

const ObjectId = require("mongodb").ObjectId;
const JudgeModel = require("../models/Judge");
const JudgeScoreModel = require("../models/JudgeScore");

const FeedbackModel = require("../models/Feedback");
const getAll = async (req, res) => {
try {
let judges = await JudgeModel.find();
Expand Down Expand Up @@ -89,16 +89,17 @@ const create = async (req, res) => {
}
};
/**
* Deletes a judge by ID and removes all associated judge score records.
* Deletes a judge by ID and removes all associated judge score records and feedback.
*
* This function:
* 1. Extracts the judge ID from the request parameters.
* 2. Checks if the judge exists in the database.
* 3. If the judge does not exist, returns a 404 error response.
* 4. Deletes all `JudgeScore` records linked to the specified judge.
* 5. Deletes the judge record from the database.
* 6. Returns a success response if the operation is completed successfully.
* 7. Handles any errors by returning a 500 Internal Server Error response.
* 5. Deletes all `Feedback` records linked to the specified judge.
* 6. Deletes the judge record from the database.
* 7. Returns a success response if the operation is completed successfully.
* 8. Handles any errors by returning a 500 Internal Server Error response.
*
* @param {Object} req - Express request object containing the judge ID in `req.params.id`.
* @param {Object} res - Express response object for sending JSON responses.
Expand All @@ -122,6 +123,7 @@ const deleteJudgeById = async (req, res) => {

// Delete the judge
await JudgeModel.findByIdAndDelete(judge_id);
await FeedbackModel.deleteOne({judge: ObjectId((judge_id))});

return res.json({
status: 200,
Expand Down
33 changes: 33 additions & 0 deletions src/models/Feedback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const mongoose = require("mongoose");

const schema = {
rating:{
type: Number,
required: true
},
comment: {
type: String
},
signature: {
type: String,
required: true
},
judge: {
type: mongoose.Schema.Types.ObjectId,
ref: "Judge",
required: true
},
event: {
type: mongoose.Schema.Types.ObjectId,
ref: "Event",
required: true
}
};

const options = {
autoCreate: true,
};

const feedbackSchema = new mongoose.Schema(schema, options);

module.exports = mongoose.model("Feedback", feedbackSchema);
7 changes: 7 additions & 0 deletions src/routes/feedback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const router = require("express").Router();
const { create, getFeedbackForAnEvent } = require("../controllers/feedback");

router.post("/", create);
router.post("/getFeedback", getFeedbackForAnEvent);

module.exports = router;