forked from CSI-KJSCE/appointment_to_examiner
Commit
This commit is contained in:
@@ -1,51 +1,65 @@
|
||||
const express = require("express");
|
||||
const Appointment = require("../models/Appointment");
|
||||
|
||||
const router = express.Router();
|
||||
const Appointment = require("../models/Appointment");
|
||||
const Faculty = require("../models/Faculty");
|
||||
const Course = require("../models/Course");
|
||||
|
||||
// Save multiple appointments
|
||||
router.post("/", async (req, res) => {
|
||||
try {
|
||||
const { appointments } = req.body; // Expecting an array of appointments
|
||||
if (!appointments || !Array.isArray(appointments)) {
|
||||
return res.status(400).json({ error: "Invalid or missing data" });
|
||||
}
|
||||
|
||||
const savedAppointments = [];
|
||||
for (const appointment of appointments) {
|
||||
const { facultyId, courseId, tasks } = appointment;
|
||||
|
||||
// Validate input data
|
||||
if (!facultyId || !courseId || !Array.isArray(tasks) || tasks.length === 0) {
|
||||
return res.status(400).json({ error: "Invalid appointment data" });
|
||||
}
|
||||
|
||||
const faculty = await Faculty.findOne({ facultyId });
|
||||
const course = await Course.findOne({ courseId });
|
||||
|
||||
if (!faculty || !course) {
|
||||
return res.status(404).json({
|
||||
error: `Faculty or Course not found for facultyId: ${facultyId}, courseId: ${courseId}`,
|
||||
});
|
||||
}
|
||||
|
||||
// Save each task as a separate appointment
|
||||
for (const task of tasks) {
|
||||
const newAppointment = new Appointment({
|
||||
facultyId,
|
||||
facultyName: faculty.name,
|
||||
courseId,
|
||||
courseName: course.name,
|
||||
task,
|
||||
});
|
||||
const savedAppointment = await newAppointment.save();
|
||||
savedAppointments.push(savedAppointment);
|
||||
}
|
||||
}
|
||||
|
||||
res.status(201).json(savedAppointments);
|
||||
} catch (error) {
|
||||
console.error("Error saving appointments:", error);
|
||||
res.status(500).json({ error: "Failed to save appointments" });
|
||||
}
|
||||
});
|
||||
|
||||
// Get all appointments
|
||||
router.get("/", async (req, res) => {
|
||||
try {
|
||||
const appointments = await Appointment.find()
|
||||
.populate("courseId", "name department")
|
||||
.populate("facultyId", "name email");
|
||||
const appointments = await Appointment.find();
|
||||
res.json(appointments);
|
||||
} catch (error) {
|
||||
console.error("Error fetching appointments:", 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;
|
||||
|
||||
Reference in New Issue
Block a user