feat : add appointment apis
This commit is contained in:
224
backend/src/controllers/appointment.controller.js
Normal file
224
backend/src/controllers/appointment.controller.js
Normal file
@@ -0,0 +1,224 @@
|
||||
import prisma from "../prisma/client.js";
|
||||
//CREATE APPOINTMENT
|
||||
|
||||
export const createAppointment = async (req, res) => {
|
||||
try {
|
||||
const {name, mobileNumber, email, message, date, doctorId, departmentId} =
|
||||
req.body;
|
||||
|
||||
if (!name || !mobileNumber || !doctorId || !departmentId || !date) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "Required fields missing",
|
||||
});
|
||||
}
|
||||
|
||||
const appointment = await prisma.appointment.create({
|
||||
data: {
|
||||
name,
|
||||
mobileNumber,
|
||||
email,
|
||||
message,
|
||||
date: new Date(date),
|
||||
doctorId,
|
||||
departmentId,
|
||||
},
|
||||
include: {
|
||||
doctor: true,
|
||||
department: true,
|
||||
},
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
message: "Appointment booked successfully",
|
||||
data: appointment,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Failed to create appointment",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// GET ALL APPOINTMENTS
|
||||
|
||||
export const getAppointments = async (req, res) => {
|
||||
try {
|
||||
const appointments = await prisma.appointment.findMany({
|
||||
include: {
|
||||
doctor: true,
|
||||
department: true,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: "desc",
|
||||
},
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: appointments,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Failed to fetch appointments",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// GET SINGLE APPOINTMENT
|
||||
|
||||
export const getAppointment = async (req, res) => {
|
||||
try {
|
||||
const {id} = req.params;
|
||||
|
||||
const appointment = await prisma.appointment.findUnique({
|
||||
where: {
|
||||
id: Number(id),
|
||||
},
|
||||
include: {
|
||||
doctor: true,
|
||||
department: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!appointment) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: "Appointment not found",
|
||||
});
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: appointment,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Failed to fetch appointment",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// GET APPOINTMENTS BY DOCTOR
|
||||
|
||||
export const getAppointmentsByDoctor = async (req, res) => {
|
||||
try {
|
||||
const {doctorId} = req.params;
|
||||
|
||||
const appointments = await prisma.appointment.findMany({
|
||||
where: {
|
||||
doctorId,
|
||||
},
|
||||
include: {
|
||||
doctor: true,
|
||||
department: true,
|
||||
},
|
||||
orderBy: {
|
||||
date: "asc",
|
||||
},
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: appointments,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Failed to fetch doctor appointments",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// GET APPOINTMENTS BY DEPARTMENT
|
||||
|
||||
export const getAppointmentsByDepartment = async (req, res) => {
|
||||
try {
|
||||
const {departmentId} = req.params;
|
||||
|
||||
const appointments = await prisma.appointment.findMany({
|
||||
where: {
|
||||
departmentId,
|
||||
},
|
||||
include: {
|
||||
doctor: true,
|
||||
department: true,
|
||||
},
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: appointments,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Failed to fetch department appointments",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// UPDATE APPOINTMENT
|
||||
|
||||
export const updateAppointment = async (req, res) => {
|
||||
try {
|
||||
const {id} = req.params;
|
||||
|
||||
const appointment = await prisma.appointment.update({
|
||||
where: {
|
||||
id: Number(id),
|
||||
},
|
||||
data: req.body,
|
||||
include: {
|
||||
doctor: true,
|
||||
department: true,
|
||||
},
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: "Appointment updated successfully",
|
||||
data: appointment,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Failed to update appointment",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
//DELETE APPOINTMENT
|
||||
|
||||
export const deleteAppointment = async (req, res) => {
|
||||
try {
|
||||
const {id} = req.params;
|
||||
|
||||
await prisma.appointment.delete({
|
||||
where: {
|
||||
id: Number(id),
|
||||
},
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: "Appointment deleted successfully",
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Failed to delete appointment",
|
||||
});
|
||||
}
|
||||
};
|
||||
23
backend/src/routes/appointment.routes.js
Normal file
23
backend/src/routes/appointment.routes.js
Normal file
@@ -0,0 +1,23 @@
|
||||
import express from "express";
|
||||
import {
|
||||
createAppointment,
|
||||
getAppointments,
|
||||
getAppointment,
|
||||
updateAppointment,
|
||||
deleteAppointment,
|
||||
} from "../controllers/appointment.controller.js";
|
||||
|
||||
import jwtAuthMiddleware from "../middleware/auth.js";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
/* PUBLIC */
|
||||
|
||||
router.get("/getall", getAppointments);
|
||||
router.post("/", createAppointment);
|
||||
|
||||
router.get("/:id", getAppointment);
|
||||
router.patch("/:id", updateAppointment);
|
||||
router.delete("/:id", jwtAuthMiddleware, deleteAppointment);
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user