DEV Community

Toluwani Omosuyi
Toluwani Omosuyi

Posted on

How I Tackled a Challenging Backend Problem

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

Top comments (1)

Collapse
 
sloan profile image
Sloan the DEV Moderator

Hey there!

While we appreciate you sharing posts here, please see DEV's Content Policy available in our Terms and be careful not to share content that is too promotional. The terms state:

Users must make a good-faith effort to share content that is on-topic, of high-quality, and is not designed primarily for the purposes of promotion or creating backlinks.

Going forward, be careful to avoid sharing articles that are too promotional in nature. While it's generally okay to promote your product, business, personal brand, etc., just make sure that the posts you share have value beyond promotion.

If promotion is your primary goal, then consider signing up for Pro Tools which gives you access to:

  • Billboards for advertising,
  • Analytics to see advanced stats on your posts
  • And more!

Hope you understand our reason for limiting promotional content and that you continue to enjoy DEV!