public void Publish(CreateConfirmationEmailMessage eventToPublish) { Guard.Against.Null(eventToPublish, nameof(eventToPublish)); var channel = _objectPool.Get(); object message = (object)eventToPublish; try { string exchangeName = MessagingConstants.Exchanges.FRONTDESK_VETCLINICPUBLIC_EXCHANGE; channel.ExchangeDeclare(exchangeName, "direct", true, false, null); var messageString = JsonSerializer.Serialize(message); var sendBytes = Encoding.UTF8.GetBytes(messageString); var properties = channel.CreateBasicProperties(); properties.Persistent = true; channel.BasicPublish( exchange: exchangeName, routingKey: "appointment-scheduled", basicProperties: properties, body: sendBytes); _logger.LogInformation($"Sending appt scheduled event: {messageString}"); } finally { _objectPool.Return(channel); } }
public async Task Handle(AppointmentScheduledEvent appointmentScheduledEvent, CancellationToken cancellationToken) { _logger.LogInformation("Handling appointmentScheduledEvent"); // 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.AppointmentId = appt.Id; newMessage.AppointmentStartDateTime = appointmentScheduledEvent.AppointmentScheduled.TimeRange.Start; newMessage.ClientName = client.FullName; newMessage.ClientEmailAddress = client.EmailAddress; newMessage.DoctorName = doctor.Name; newMessage.PatientName = patient.Name; newMessage.AppointmentType = apptType.Name; _messagePublisher.Publish(newMessage); _logger.LogInformation($"Message published. {newMessage.PatientName}"); }