Fixed backend issue, update course status working

This commit is contained in:
Harshitha Shetty
2025-01-04 23:50:59 +05:30
parent b4adca13c7
commit 7079591f84
7 changed files with 123 additions and 454 deletions

View File

@@ -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;