Skip to content

Commit

Permalink
Merge pull request #129 from Varad0014/newapi
Browse files Browse the repository at this point in the history
[New API]: Added Hospital API
  • Loading branch information
dishamodi0910 authored May 23, 2024
2 parents ff6b518 + 599c323 commit e4e1007
Show file tree
Hide file tree
Showing 18 changed files with 1,774 additions and 0 deletions.
127 changes: 127 additions & 0 deletions New_APIs/HospitalAPI/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Description
This API facilitates Hospital Management, offering functionality to manage patients, doctors, appointments, and their associated bills.

# API Endpoints
## Patient Endpoints
### GET /api/patients
Retrieve all patients
### POST /api/patients
Add a new patient
### GET /api/patients/:patientId
Retrieve a specific patient
### PATCH /api/patients/:patientId
Update a specific patient
### DELETE /api/patients/:patientId
Delete a specific patient

## Doctor Endpoints
### GET /api/doctors
Retrieve all doctors
### POST /api/doctors
Add a new doctor
### GET /api/doctors/:doctorId
Retrieve a specific doctor
### PATCH /api/doctors/:doctorId
Update a specific doctor
### DELETE /api/doctors/:doctorId
Delete a specific doctor


## Appointment Endpoints
### GET /api/appointments
Retrieve all appointments
### POST /api/appointments
Create a new appointment
### GET /api/appointments/:appointmentId
Retrieve a specific appointment
### PATCH /api/appointments/:appointmentId
Update a specific appointment
### DELETE /api/appointments/:appointmentId
Delete a specific appointment
### GET /api/appointments/:appointmentId/bill
Retrieve bill of a specific appointment
### POST /api/appointments/:appointmentId/bill
Create bill of a specific appointment
### PATCH /api/appointments/:appointmentId/bill
Update bill of a specific appointment
### DELETE /api/appointments/:appointmentId/bill
Delete bill of a specific appointment

## Speciality Endpoints
### GET /api/specialities
Retrieve all specialities
### POST /api/specialities
Add a new speciality
### GET /api/specialities/:specialityId
Retrieve a specific speciality
### PATCH /api/specialities/:specialityId
Update a specific speciality
### DELETE /api/specialities/:specialityId
Delete a specific speciality

# Getting Started
## Pre-Requisites
Make sure you have:
- Node
- MongoDB

## Steps to Run
- Clone the repository and navigate to the main directory **HospitalAPI**

```bash
cd HospitalAPI
```
- Create a **.env** file in the main directory
```bash
touch .env
```

- Add the following neccessary details in the **.env** file
```bash
PORT=4000
DBURL=mongodb://localhost:27017/<database name>
```
- Install dependencies
```bash
npm install
```
- Add initial data to the database
```bash
node seed.js
```
- Run the server code
```bash
node index.js
```

## Request Body Example
### Patient request body
```bash
{
"name": "John Doe",
"gender": "Male",
"phone": "9999999999",
"dateOfBirth": "1990-05-15",
"address": "Delhi"
}
```
### Doctor request body
```bash
#replace with ids present in your database
{
"name": "John Doe",
"phone": "9999999999",
"gender": "Male",
"specialityId": "664b580728e29bccc9eb30e2"
}
```
### Appointment request body
```bash
#replace with ids present in your database
{
"doctorId": "664b35fc2d0604dd4cbdca91",
"patientId": "664b35fc2d0604dd4cbdca96",
"appointmentDate": "2024-05-15"
}
```

150 changes: 150 additions & 0 deletions New_APIs/HospitalAPI/controllers/appointments.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import Appointment from "../models/appointment.js";
import Bill from "../models/bill.js"
import Doctor from "../models/doctor.js";
import Patient from "../models/patient.js";

export default class AppointmentController {
static async addAppointment(req, res, next) {
const { doctorId, patientId, appointmentDate } = req.body;
const patient = await Patient.findById(patientId);
const doctor = await Doctor.findById(doctorId);
console.log(patient);
console.log(doctor);
if(!patient || !doctor){
return res.status(404).send({ error: "Patient or Doctor not found" })
}
const appointment = new Appointment({
doctor: doctorId,
patient: patientId,
appointmentDate: appointmentDate
});
try {
const response = await appointment.save();
res.status(201).json(response);
} catch (err) {
res.status(400).json({ message: err });
}
}

static async getAllAppointments(req, res, next) {
try {
const response = await Appointment.find().populate('doctor').populate('patient');
if (response.length === 0) {
return res.status(404).send({ error: "No appointments found" })
}
res.status(200).json(response);
} catch (err) {
console.log(err);
res.status(500).json({ message: err });
}
}
static async getAppointmentById(req, res, next) {
const { appointmentId } = req.params;
try {
const response = await Appointment.findById(appointmentId).populate('doctor').populate('patient');
if (!response) {
return res.status(404).send({ error: "Appointment not found!" })
}
res.status(200).json(response);
} catch (err) {
res.status(500).json({ message: err });
}
}
static async updateAppointmentById(req, res, next) {
const { appointmentId } = req.params;
const { appointmentDate, status } = req.body;
const updatedInfo = {};
if (appointmentDate) updatedInfo.appointmentDate = appointmentDate;
if (status) updatedInfo.status = status;

try {
const response = await Appointment.findByIdAndUpdate(appointmentId, updatedInfo, { runValidators: true, new: true });
if (!response) {
return res.status(404).send({ error: "Appointment not found!" })
}
res.status(200).json(response);
} catch (err) {
res.status(500).json({ message: err });
}
}

static async deleteAppointmentById(req, res, next) {
const { appointmentId } = req.params;
try {
const response = await Appointment.findByIdAndDelete(appointmentId);
if (!response) {
return res.status(404).send({ error: "Appointment not found!" })
}
res.status(200).json(response);
} catch (err) {
res.status(500).json({ message: err });
}
}

static async addAppointmentBill(req, res, next) {
const { appointmentId } = req.params;
const { amount } = req.body;
const appointment = await Appointment.findById(appointmentId);
if (appointment !== null && appointment.status == 'Completed') {
const bill = new Bill({
appointment: appointmentId,
amount: amount
})
try {
const response = await bill.save();
res.status(201).json(response);
} catch (err) {
res.status(400).json({ message: err });
}
}
else {
res.status(400).json({ message: "Appointment not completed" });
}
}

static async getAppointmentBill(req, res, next) {
const { appointmentId } = req.params;
try {
const response = await Bill.findOne({ appointment: appointmentId }).populate('appointment');
if (!response) {
return res.status(404).send({ error: "Bill not found!" })
}
res.status(200).json(response);
} catch (err) {
res.status(500).json({ message: err });
}
}

static async updateAppointmentBill(req, res, next) {
const { appointmentId } = req.params;
const { amount } = req.body;
const { status } = req.body;
const updatedInfo = {};
if (amount) updatedInfo.amount = amount
if (status) updatedInfo.status = status
try {
const response = await Bill.findOneAndUpdate({ appointment: appointmentId }, updatedInfo, { runValidators: true, new: true });
if (!response) {
return res.status(404).send({ error: "Bill not found!" })
}
res.status(200).json(response);
} catch (err) {
res.status(500).json({ message: err });
}
}

static async deleteAppointmentBill(req, res, next) {
const { appointmentId } = req.params;
try {
const response = await Bill.findOneAndDelete({ appointment: appointmentId });
if (!response) {
return res.status(404).send({ error: "Bill not found!" })
}
res.status(200).json({ message: "Bill Deleted Sucessfully" });
} catch (err) {
console.log(err);
res.status(500).json({ message: err });
}
}

}
87 changes: 87 additions & 0 deletions New_APIs/HospitalAPI/controllers/doctors.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import Doctor from "../models/doctor.js";
import Speciality from "../models/speciality.js";

export default class DoctorController {
static async addDoctor(req, res, next) {
const { name, phone, gender, specialityId } = req.body;
const spec = await Speciality.findById(specialityId);
if (!spec) {
return res.status(404).send({ error: "Invalid Speciality" })
}
const doctor = new Doctor({
name: name,
speciality: specialityId,
gender: gender,
phone: phone,
})
try {
const response = await doctor.save();
res.status(201).json(response);
} catch (err) {
res.status(400).json({ message: err.message });
}
}
static async getAllDoctors(req, res, next) {
try {
const response = await Doctor.find().populate('speciality');
if (response.length === 0) {
return res.status(404).send({ error: "No doctors found" })
}
res.status(200).json(response);
} catch (err) {
console.log(err);
res.status(500).json({ message: err });
}
}

static async getDoctorById(req, res, next) {
const { doctorId } = req.params;
try {
const response = await Doctor.findById(doctorId).populate('speciality');
if (!response) {
return res.status(404).send({ error: "Doctor not found!" })
}
res.status(200).json(response);
} catch (err) {
console.log(err);
res.status(500).json({ message: err });
}
}
static async updateDoctorById(req, res, next) {
const { doctorId } = req.params;
const { name, phone, gender, specialityId } = req.body;
const updatedInfo = {};
if (name) updatedInfo.name = name;
if (phone) updatedInfo.phone = phone;
if (gender) updatedInfo.gender = gender;
if (specialityId) updatedInfo.speciality = specialityId;
const spec = await Speciality.findById(specialityId);
if (!spec) {
return res.status(404).send({ error: "Invalid Speciality" })
}
try {
const response = await Doctor.findByIdAndUpdate(doctorId, updatedInfo, { runValidators: true, new: true });
if (!response) {
return res.status(404).send({ error: "Doctor not found!" })
}
res.status(200).json(response);
} catch (err) {
console.log(err);
res.status(500).json({ message: err });
}
}

static async deleteDoctorById(req, res, next) {
const { doctorId } = req.params;
try {
const response = await Doctor.findByIdAndDelete(doctorId);
if (!response) {
return res.status(404).send({ error: "Doctor not found!" })
}
res.status(200).json({ message: "Doctor Deleted Sucessfully" });
} catch (err) {
console.log(err);
res.status(500).json({ message: err });
}
}
}
Loading

0 comments on commit e4e1007

Please sign in to comment.