Fixed backend issue, update course status working
This commit is contained in:
@@ -35,4 +35,39 @@ router.get("/:id", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Update course status route
|
||||
router.patch("/api/courses/:courseId", async (req, res) => {
|
||||
try {
|
||||
const { courseId } = req.params; // Extract courseId from params
|
||||
const { status } = req.body; // Extract status from body
|
||||
|
||||
console.log("Request params:", req.params);
|
||||
console.log("Request body:", req.body);
|
||||
|
||||
// Validate status
|
||||
if (!["submitted", "not submitted"].includes(status.toLowerCase())) {
|
||||
return res.status(400).json({ message: "Invalid status value" });
|
||||
}
|
||||
|
||||
// Find and update the course by courseId
|
||||
const updatedCourse = await Course.findOneAndUpdate(
|
||||
{ courseId }, // Query by courseId field
|
||||
{ status: status.toLowerCase() }, // Set status (convert to lowercase)
|
||||
{ new: true } // Return the updated document
|
||||
);
|
||||
|
||||
if (!updatedCourse) {
|
||||
console.error("Course not found:", courseId);
|
||||
return res.status(404).json({ message: "Course not found" });
|
||||
}
|
||||
|
||||
console.log("Updated course:", updatedCourse);
|
||||
res.status(200).json(updatedCourse);
|
||||
} catch (error) {
|
||||
console.error("Error updating course status:", error.message);
|
||||
res.status(500).json({ message: "Internal server error" });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user