How I Tackled a Challenging Backend Problem
Published: 2024-06-29 12:14:44

My name is Toluwani, and I'll share a recent difficult backend problem I solved 2 months ago. As a junior backend developer, I was given a task to build an API for a Medical Appointment Application. 

The application aims to facilitate appointment bookings between Patients and Doctors. A doctor can only have one appointment scheduled with a patient at a time, so if a patient needs to book an appointment and no doctor is available, my API should respond with the proper response and status code. 

I used Python programming language alongside with FastAPI framework and an in-memory database because I have yet to learn how to use a database. 

As the name implies my application would contain three entities which are _patients_, _doctors_, and _appointments_.

To start with I'll explain the folders I created within my actual project folder for me to be able to have this project done. I started with the **router folder** which entails all endpoints (i.e. CRUD operation) relating to each entity and how the application handles incoming API requests. Then I created a **service folder** for myself, I'd say that is the engine behind my project because that is where each file relating to each entity functions are being executed and also interacts with the database in which I used and in memory, In summary, it contains the logic of the whole operation of the project, and lastly, I created a **schema folder** which determine the structure of the database and how my patient, doctor, and appointment data will look like by enforcing strict typing using Pydantic. 

The next thing I did was implement CRUD (create, read, update, and delete) operations for patients and doctors. Doing this I've been able to fix two entities from the three entities I need to work on in my project. I could create a patient profile containing ({id}, name, age, sex, weight, height, phone) and a doctor profile containing ({id}, name, specialization, phone, is_available (defaults to True)) making sure the patient or doctor wouldn't have to add an ID when creating its profile because a unique ID is being generated for each user. The reading is in two categories either reading all patients/doctors or reading just a patient/doctor using the ID that was generated when creating a profile. 

The update is being achieved by using the same ID generated for each user to make changes to the profile created and there's an HTTPException that raises an error if the ID isn't in the database to avoid the application from crashing lastly the delete is being executed using the ID generated for each user and also raises an HTTPException if the User's ID is in the database. For the is_available (defaults to True) in the doctor's schema it is to show that at default all doctors are available until an appointment is booked then the is_available changes to False until the doctor is done with its appointment before it is changed back to true. I'll be explaining the appointment entity to give a better grasp of this paragraph. 

Furthermore, in the appointment router, Only patients can create an appointment. When a patient tries to create an appointment, the first available doctor is assigned to the Appointment, and if there's an issue with the patient meeting up with the scheduled appointment it can be canceled before it is completed, making the doctor free again. If no doctors are available, it raises an HTTPException to the user and when the appointment is complete this will make the Doctor available again and other patients can book the doctor. So back to the Set availability status. This is for the Doctors, allowing them to set their status to unavailable to prevent them from being booked during an appointment with a patient.


In conclusion, this was how I was able to tackle the project little by little and make sure I used the appropriate status code where necessary. The project taught me valuable lessons in managing backend logic, error handling, and API structuring. As I look forward to the HNG Internship, I’m eager to further refine my skills, learn from experienced mentors, and tackle even more complex problems. 

Join me as I embark on a new journey of learning at [HNG Internship](https://hng.tech/internship)