HHey there! ๐ Welcome back to my series on DDD. If you’re just joining me and haven’t checked out Part 1 and Part 2, I’d recommend starting there. We’re using a cool approach called Domain-Driven Design (DDD) to tackle this.
Ready to dive in? Let’s go! ๐
๐ What Happens When a Patient Makes an Appointment?
When a patient makes an appointment, a few things happen:
1. The patient books an appointment: The patient chooses a time and a doctor for their appointment.
2. The system says “OK, got it!”: The system makes a note of the appointment.
3. The doctor finishes the appointment: When the appointment is done, the doctor tells the system it’s finished.
Now, let’s see how we can make this happen with some code!
Building Blocks (Entities)
The first thing we need is an Appointment building block. Think of it as a little machine that knows how to handle appointments.
public class Appointment { private String id; private String patientId; private String doctorId; private LocalDateTime appointmentTime; private AppointmentStatus status; public Appointment(String id, String patientId, String doctorId, LocalDateTime appointmentTime) { this.id = id; this.patientId = patientId; this.doctorId = doctorId; this.appointmentTime = appointmentTime; this.status = AppointmentStatus.SCHEDULED; } public void confirmAppointment() { this.status = AppointmentStatus.CONFIRMED; } public void completeAppointment() { this.status = AppointmentStatus.COMPLETED; } }
Libraries (Repositories)
Next, we need a library to keep all our Appointment building blocks.
public interface AppointmentRepository extends JpaRepository<Appointment, String> {
// We can add custom queries here...
}
Managers (Services)
We also need a manager who knows how to use these building blocks to do useful things.
public class AppointmentService {
private final AppointmentRepository appointmentRepository;
public AppointmentService(AppointmentRepository appointmentRepository) {
this.appointmentRepository = appointmentRepository;
}
public Appointment scheduleAppointment(String patientId, String doctorId, LocalDateTime appointmentTime) {
Appointment appointment = new Appointment(UUID.randomUUID().toString(), patientId, doctorId, appointmentTime);
return appointmentRepository.save(appointment);
}
public Appointment confirmAppointment(String appointmentId) {
Appointment appointment = appointmentRepository.findById(appointmentId).orElseThrow(() -> new IllegalArgumentException("Invalid appointment ID"));
appointment.confirmAppointment();
return appointmentRepository.save(appointment);
}
public Appointment completeAppointment(String appointmentId) {
Appointment appointment = appointmentRepository.findById(appointmentId).orElseThrow(() -> new IllegalArgumentException("Invalid appointment ID"));
appointment.completeAppointment();
return appointmentRepository.save(appointment);
}
}
Receptionists (Controllers)
Finally, we need a receptionist who listens to requests and tells the manager what to do.
public class AppointmentController {
private final AppointmentService appointmentService;
public AppointmentController(AppointmentService appointmentService) {
this.appointmentService = appointmentService;
}
public Appointment scheduleAppointment(AppointmentDTO request) {
return appointmentService.scheduleAppointment(request.getPatientId(), request.getDoctorId(), request.getAppointmentTime());
}
public Appointment confirmAppointment(String appointmentId) {
return appointmentService.confirmAppointment(appointmentId);
}
public Appointment completeAppointment(String appointmentId) {
return appointmentService.completeAppointment(appointmentId);
}
}
And that’s it! We’ve built the basic parts of our hospital computer system. Keep following along, and we’ll dive deeper into each of these parts in the next articles. Keep coding and having fun! ๐๐๐จ๐ป๐ฉ๐ป
I will update the code in GitHub soon.