AdminCoursePage added..... admin routes protection added

This commit is contained in:
Harshitha Shetty
2025-03-29 15:40:41 +05:30
parent 19b94b1a9f
commit 9fda8ba883
10 changed files with 599 additions and 18 deletions

View File

@@ -121,4 +121,18 @@ router.post("/reset-password", async (req, res) => {
}
});
// router.get("/me", (req, res) => {
// try {
// const token = req.cookies.token;
// console.log("token recieved",token);
// if (!token) return res.status(401).json({ message: "Unauthorized" });
// const user = jwt.verify(token, process.env.JWT_SECRET);
// res.json(user);
// } catch (error) {
// res.status(401).json({ message: "Invalid token" });
// }
// });
module.exports = router;

View File

@@ -1,25 +1,26 @@
const express = require("express");
const Course = require("../models/Course");
const verifyAdmin = require("../../client/src/components/verifyAdmin");
const router = express.Router();
router.get("/", async (req, res) => {
try {
const filter = {};
if (req.query.department) filter.department = req.query.department;
if (req.query.program) filter.program = req.query.program;
if (req.query.semester) filter.semester = Number(req.query.semester); // Convert to number if needed
if (req.query.scheme) filter.scheme = req.query.scheme;
const courses = await Course.find(filter);
res.json(courses);
} catch (error) {
console.error("Error fetching courses:", error);
res.status(500).json({ error: "Failed to fetch courses" });
}
});
try {
const filter = {};
if (req.query.department) filter.department = req.query.department;
if (req.query.program) filter.program = req.query.program;
if (req.query.semester) filter.semester = Number(req.query.semester); // Convert to number if needed
if (req.query.scheme) filter.scheme = req.query.scheme;
const courses = await Course.find(filter);
res.json(courses);
} catch (error) {
console.error("Error fetching courses:", error);
res.status(500).json({ error: "Failed to fetch courses" });
}
});
// Get course by ID
router.get("/:id", async (req, res) => {
@@ -36,7 +37,7 @@ router.get("/:id", async (req, res) => {
});
// Update course status route
router.patch("/api/courses/:courseId", async (req, res) => {
router.patch(":courseId", async (req, res) => {
try {
const { courseId } = req.params; // Extract courseId from params
const { status } = req.body; // Extract status from body
@@ -69,5 +70,67 @@ router.patch("/api/courses/:courseId", async (req, res) => {
}
});
//update a course
router.put("/:courseId", verifyAdmin, async (req, res) => {
try {
const updatedCourse = await Course.findOneAndUpdate(
{ courseId: req.params.courseId },
req.body,
{ new: true }
);
if (!updatedCourse) {
return res.status(404).json({ error: "Course not found" });
}
res.json(updatedCourse);
} catch (error) {
console.error("Error updating course:", error);
res.status(500).json({ error: "Failed to update course" });
}
});
//delete
router.delete("/:courseId", verifyAdmin, async (req, res) => {
try {
const deletedCourse = await Course.findOneAndDelete({ courseId: req.params.courseId });
if (!deletedCourse) {
return res.status(404).json({ error: "Course not found" });
}
res.json({ message: "Course deleted successfully" });
} catch (error) {
console.error("Error deleting course:", error);
res.status(500).json({ error: "Failed to delete course" });
}
});
// add a new course
router.post("/", verifyAdmin, async (req, res) => {
try {
const { courseId, name, department, program, scheme, semester, status } = req.body;
// Check if a course with the same courseId already exists
const existingCourse = await Course.findOne({ courseId });
if (existingCourse) {
return res.status(400).json({ error: "Course ID already exists" });
}
const newCourse = new Course({
courseId,
name,
department,
program,
scheme,
semester,
status
});
await newCourse.save();
res.status(201).json({ message: "Course added successfully", course: newCourse });
} catch (error) {
console.error("Error adding course:", error);
res.status(500).json({ error: "Failed to add course" });
}
});
module.exports = router;