public async Task Handle(AppointmentScheduledEvent appointmentScheduledEvent, CancellationToken cancellationToken) { // we are translating from a domain event to an application event here var newMessage = new CreateConfirmationEmailMessage(); var appt = appointmentScheduledEvent.AppointmentScheduled; // if this is slow these can be parallelized or cached. MEASURE before optimizing. var doctor = await _repository.GetByIdAsync <Doctor, int>(appt.DoctorId.Value); var clientWithPatientsSpec = new ClientByIdIncludePatientsSpecification(appt.ClientId); var client = (await _repository.ListAsync <Client, int>(clientWithPatientsSpec)) .FirstOrDefault(); var patient = client.Patients.First(p => p.Id == appt.PatientId); var apptType = await _repository.GetByIdAsync <AppointmentType, int>(appt.AppointmentTypeId); newMessage.AppointmentDateTime = appointmentScheduledEvent.AppointmentScheduled.TimeRange.Start; newMessage.ClientName = client.FullName; newMessage.ClientEmailAddress = client.EmailAddress; newMessage.DoctorName = doctor.Name; newMessage.PatientName = patient.Name; newMessage.ProcedureName = apptType.Name; _messagePublisher.Publish(newMessage); }
public async Task Handle(AppointmentScheduledEvent appointmentScheduledEvent, CancellationToken cancellationToken) { // we are translating from a domain event to an application event here var newMessage = new CreateConfirmationEmailMessage(); var appt = appointmentScheduledEvent.AppointmentScheduled; // if this is slow these can be parallelized or cached. MEASURE before optimizing. var doctor = await _doctorRepository.GetByIdAsync(appt.DoctorId); if (doctor == null) { throw new DoctorNotFoundException(appt.DoctorId); } var clientWithPatientsSpec = new ClientByIdIncludePatientsSpecification(appt.ClientId); var client = await _clientRepository.GetBySpecAsync(clientWithPatientsSpec); if (client == null) { throw new ClientNotFoundException(appt.ClientId); } var patient = client.Patients.FirstOrDefault(p => p.Id == appt.PatientId); if (patient == null) { throw new PatientNotFoundException(appt.PatientId); } var apptType = await _appointmentTypeRepository.GetByIdAsync(appt.AppointmentTypeId); if (apptType == null) { throw new AppointmentTypeNotFoundException(appt.AppointmentTypeId); } newMessage.AppointmentDateTime = appointmentScheduledEvent.AppointmentScheduled.TimeRange.Start; newMessage.ClientName = client.FullName; newMessage.ClientEmailAddress = client.EmailAddress; newMessage.DoctorName = doctor.Name; newMessage.PatientName = patient.Name; newMessage.ProcedureName = apptType.Name; // TODO: uncomment this after finish ServiceBrokerMessagePublisher // messagePublisher.Publish(newMessage); }