Skip to content

Instantly share code, notes, and snippets.

@kislayverma
Last active May 29, 2021 12:26
Show Gist options
  • Save kislayverma/f57d9f00efe913f99d369cabb49a18d8 to your computer and use it in GitHub Desktop.
Save kislayverma/f57d9f00efe913f99d369cabb49a18d8 to your computer and use it in GitHub Desktop.
Method is still kind of ok but getting worse
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) {
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