Ejemplo n.º 1
0
        public async Task <ActionResult> UpdateReasonCodeAsync([FromBody] ReasonCode reasonCodeToUpdate)
        {
            var reasonCode = await _appointmentContext.ReasonCodes.SingleOrDefaultAsync(i => i.Id == reasonCodeToUpdate.Id);

            if (reasonCode == null)
            {
                return(NotFound(new { Message = $"Item with id {reasonCodeToUpdate.Id} not found." }));
            }

            var oldName = reasonCode.Code;
            var raiseCodeChangedEvent = oldName != reasonCodeToUpdate.Code;

            // Update current reasonCode
            reasonCode = reasonCodeToUpdate;
            _appointmentContext.ReasonCodes.Update(reasonCode);

            if (raiseCodeChangedEvent) // Save reasonCode's data and publish integration event through the Event Bus if price has changed
            {
                //Create Integration Event to be published through the Event Bus
                var reasonCodeChangedEvent = new AppointmentReasonCodeChangedIntegrationEvent(reasonCode.Id, reasonCodeToUpdate.Code, oldName);

                // Achieving atomicity between original ReasonCode database operation and the IntegrationEventLog thanks to a local transaction
                await _appointmentIntegrationEventService.SaveEventAndReasonCodeContextChangesAsync(reasonCodeChangedEvent);

                // Publish through the Event Bus and mark the saved event as published
                await _appointmentIntegrationEventService.PublishThroughEventBusAsync(reasonCodeChangedEvent);
            }
            else // Just save the updated reason code because the ReasonCode cod hasn't changed.
            {
                await _appointmentContext.SaveChangesAsync();
            }

            return(CreatedAtAction(nameof(ItemByIdAsync), new { id = reasonCodeToUpdate.Id }, null));
        }
Ejemplo n.º 2
0
        public async Task Handle(AppointmentReasonCodeChangedIntegrationEvent @event)
        {
            using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}"))
            {
                _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event);

                var confirmedIntegrationEvent = [email protected]("microservice")
                    ? (IntegrationEvent) new ReasonCodeRejectedIntegrationEvent(@event.BrandId)
                    : new ReasonCodeConfirmedIntegrationEvent(@event.BrandId);

                await _appointmentIntegrationEventService.SaveEventAndReasonCodeContextChangesAsync(confirmedIntegrationEvent);

                await _appointmentIntegrationEventService.PublishThroughEventBusAsync(confirmedIntegrationEvent);
            }
        }