Skip to content

Instantly share code, notes, and snippets.

@kislayverma
Created May 29, 2021 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kislayverma/daac16e77b4542455c82335accde9bfa to your computer and use it in GitHub Desktop.
Save kislayverma/daac16e77b4542455c82335accde9bfa to your computer and use it in GitHub Desktop.
Original method broken down to workflow style
public Slot makeAppointment(Doctor d, Hospital h, Patient p, Date startTime, Date endTime) throws Exception {
if (!isHospitalOpen(h, startTime, endTime)) {
throw new Exception("Hospital isn't open in this time period");
}
Slot s = getFreeSlotForDoctor(d, startTime, endTime);
if (s == null) {
throw new Exception("Doctor is not free in this time period");
}
Slot bookedSlot = createAppointment(s, d, p);
notifyDoctor(bookedSlot, d, p);
notifyPatient(bookedSlot, d, p);
return bookedSlot;
}
private boolean isHospitalOpen(Hospital h, Date startTime, Date endTime) {
String hospitalServiceBaseUrl = ConfigReader.readConfigName("hospitalServiceBaseUrl");
HospitalService hospitalService = new HospitalService(hospitalServiceBaseUrl);
DateRange range = hospitalService.getWorkingHours();
return range.contains(startTime) && range.contains(endTime);
}
private boolean getFreeSlotForDoctor(Doctor d, Date startTime, Date endTime) {
String scheduleServiceBaseUrl = ConfigReader.readConfigName("scheduleServiceBaseUrl");
ScheduleSerivce scheduleService = new ScheduleService(scheduleServiceBaseUrl);
return scheduleService.getAppointmentSlots(d).stream().filter(slot -> !slot.isOccupied) // Only consider free slots
.filter(slot -> slot.contains(startTime) && slot.contains(endTime)) // Only consider slots which contain this time period
.findFirst();
}
private Slot createAppointment(Slot s, Doctor d, Patient p) {
String appointmentServiceBaseUrl = ConfigReader.readConfigName("appointmentServiceBaseUrl");
AppointmentService appointmentService = new AppointmentService(appointmentServiceBaseUrl);
s.setIsOccupied(true);
return appointmentService.bookSlot(d, p, s);
}
private void notifyDoctor(Slot s, Doctor d, Patient p) {
DoctorEmailTemplate doctorEmailTemplate = new DoctorEmailTemplate(d, p, bookedSlot);
notificationService.notify(d, doctorEmailTemplate);
}
private void notifyPatient(Slot s, Doctor d, Patient p) {
PatientEmailTemplate patientEmailTemplate = new PatientEmailTemplate(d, p, bookedSlot);
notificationService.notify(p, patientEmailTemplate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment