Files
appointment_to_examiner/server/routes/appointmentRoutes.js

52 lines
1.4 KiB
JavaScript

const express = require("express");
const Appointment = require("../models/Appointment");
const router = express.Router();
// Get all appointments
router.get("/", async (req, res) => {
try {
const appointments = await Appointment.find()
.populate("courseId", "name department")
.populate("facultyId", "name email");
res.json(appointments);
} catch (error) {
res.status(500).json({ error: "Failed to fetch appointments" });
}
});
// Create a new appointment
router.post("/", async (req, res) => {
try {
const { courseId, facultyId, appointmentType } = req.body;
const newAppointment = new Appointment({
courseId,
facultyId,
appointmentType,
});
await newAppointment.save();
res.status(201).json(newAppointment);
} catch (error) {
res.status(400).json({ error: "Failed to create appointment" });
}
});
// Get appointment by ID
router.get("/:id", async (req, res) => {
try {
const appointment = await Appointment.findOne({ appointmentId: req.params.id })
.populate("courseId", "name department")
.populate("facultyId", "name email");
if (!appointment) {
return res.status(404).json({ error: "Appointment not found" });
}
res.json(appointment);
} catch (error) {
res.status(500).json({ error: "Failed to fetch appointment" });
}
});
module.exports = router;