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

@@ -14,6 +14,7 @@ const courseRoutes = require("./routes/courseRoutes");
const facultyRoutes = require("./routes/facultyRoutes");
const appointmentRoutes = require("./routes/appointmentRoutes");
const optionsRoutes = require("./routes/optionsRoutes");
const Course = require("./models/Course");
// MongoDB Connection
mongoose
@@ -152,6 +153,39 @@ app.get("/api/user/profile", async (req, res) => {
}
});
app.patch("/api/courses/:courseId", async (req, res) => {
const { courseId } = req.params;
const { status } = req.body;
console.log('Request params:', req.params);
console.log('Request body:', req.body);
if (!status) {
console.error("Status is missing in the request body.");
return res.status(400).json({ message: "Status is required" });
}
try {
const updatedCourse = await Course.findOneAndUpdate(
{ courseId: courseId }, // Use courseId field for finding the course
{ status }, // Update the status field
{ new: true }// Return the updated document
);
if (!updatedCourse) {
console.error("Course not found:", courseId);
return res.status(404).json({ message: "Course not found" });
}
res.json(updatedCourse);
} catch (error) {
console.error("Error updating course status:", error.message);
res.status(500).json({ message: "Internal server error" });
}
});
// Serve React Build Files
app.use(express.static(path.join(__dirname, "../client/build")));
app.get("*", (req, res) =>