Skip to content

Instantly share code, notes, and snippets.

@kislayverma
Created May 29, 2021 12:25
Show Gist options
  • Save kislayverma/2a6c66a0602576545d74cb0a8e392ae4 to your computer and use it in GitHub Desktop.
Save kislayverma/2a6c66a0602576545d74cb0a8e392ae4 to your computer and use it in GitHub Desktop.
Method is now too granular
public Slot makeAppointment(Doctor d, Hospital h, Patient p, Date startTime, Date endTime) throws Exception {
if (!isHospitalOpen(h, startTime, endTime)) {
throwException("Hospital isn't open in this time period");
}
Slot s = getFreeSlotForDoctor(d, startTime, endTime);
if (s == null) {
throwException("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 void throwException(String message) throws Exception{
throw new Exception(message);
}
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) {
return buildScheduleService.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 ScheduleService buildScheduleService() {
String scheduleServiceBaseUrl = ConfigReader.readConfigName("scheduleServiceBaseUrl");
return new ScheduleService(scheduleServiceBaseUrl);
}
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