Exemple #1
0
        public async Task <PatientDTO> Handle(CreatePatientCommand request, CancellationToken cancellationToken)
        {
            bool tableInsertStatus;

            try
            {
                _patient = await _patientRepository.AddAsync(request.PatientDTO);

                using (EventProducer eventProducer = _eventStore.CreateEventProducer())
                {
                    EventData eventData = eventProducer.CreateEventData(_patient, "Patient-Creation-Pending");
                    eventProducer.SendEvent($"Patient-{_patient.ID}", eventData);
                }
                tableInsertStatus = true;
            }
            catch (Exception)
            {
                tableInsertStatus = false;
            }
            if (tableInsertStatus == true)
            {
                try
                {
                    PatientMessage patientMessage = new PatientMessage("PatientCreated", _patient);
                    var            message        = await _client.SendObjectMessageAsync("EmailQueue", patientMessage);

                    using (EventProducer eventProducer = _eventStore.CreateEventProducer())
                    {
                        EventData eventData = eventProducer.CreateEventData(message, "PatientCreatedConfirmation-Sent");
                        eventProducer.SendEvent($"AMQMessages-Patient-{_patient.ID}", eventData);
                    }
                }
                catch (Exception)
                {
                    await _patientRepository.DeleteAsync(_patient.ID);

                    using (EventProducer eventProducer = _eventStore.CreateEventProducer())
                    {
                        EventData eventData = eventProducer.CreateEventData(_patient, "Patient-Creation-Rollback");
                        eventProducer.SendEvent($"Patient-{_patient.ID}", eventData);
                    }
                    tableInsertStatus = false;
                }
            }
            using (EventConsumer eventConsumer = _eventStore.CreateEventConsumer($"AMQMessages-Patient-{_patient.ID}", "EmailCheck"))
            {
                bool eventFound = false;
                while (eventFound == false)
                {
                    var events = await eventConsumer.ReadStreamEventsForwardAsync();

                    if (eventConsumer.FindEventType(events, "EmailConfirmation-Sent"))
                    {
                        eventFound       = true;
                        _workerSucceeded = true;
                    }
                    else if (eventConsumer.FindEventType(events, "EmailConfirmation-Failed"))
                    {
                        eventFound       = true;
                        _workerSucceeded = false;
                    }
                }
            }
            if (_workerSucceeded == false)
            {
                await _patientRepository.DeleteAsync(_patient.ID);

                using (EventProducer eventProducer = _eventStore.CreateEventProducer())
                {
                    EventData eventData = eventProducer.CreateEventData(_patient, "Patient-Creation-Rollback");
                    eventProducer.SendEvent($"Patient-{_patient.ID}", eventData);
                }
                tableInsertStatus = false;
            }
            if (tableInsertStatus == false)
            {
                return(null);
            }
            using (EventProducer eventProducer = _eventStore.CreateEventProducer())
            {
                EventData eventData = eventProducer.CreateEventData(_patient, "Patient-Creation-Committed");
                eventProducer.SendEvent($"Patient-{_patient.ID}", eventData);
            }
            return(_patient);
        }