public async Task <ActionResult> UpdateserviceListItemAsync([FromBody] ServiceListItem serviceListItemToUpdate) { var ServiceListItem = await _serviceListContext.ServiceListItems.SingleOrDefaultAsync(i => i.Id == serviceListItemToUpdate.Id); if (ServiceListItem == null) { return(NotFound(new { Message = $"Item with id {serviceListItemToUpdate.Id} not found." })); } var oldPrice = ServiceListItem.Price; var raiseserviceListItemPriceChangedEvent = oldPrice != serviceListItemToUpdate.Price; // Update current serviceListItem ServiceListItem = serviceListItemToUpdate; _serviceListContext.ServiceListItems.Update(ServiceListItem); if (raiseserviceListItemPriceChangedEvent) // Save serviceListItem'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 priceChangedEvent = new ServiceListPriceChangedIntegrationEvent(ServiceListItem.Id, serviceListItemToUpdate.Price, oldPrice); // Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction await _catalogIntegrationEventService.SaveEventAndServiceListContextChangesAsync(priceChangedEvent); // Publish through the Event Bus and mark the saved event as published await _catalogIntegrationEventService.PublishThroughEventBusAsync(priceChangedEvent); } else // Just save the updated serviceListItem because the serviceListItem's Price hasn't changed. { await _serviceListContext.SaveChangesAsync(); } return(CreatedAtAction(nameof(ItemByIdAsync), new { id = serviceListItemToUpdate.Id }, null)); }
public async Task Handle(BonusActivatedIntegrationEvent @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 = new BonusActivatedIntegrationEvent(@event.BonusId, @event.PromoId); await _catalogIntegrationEventService.SaveEventAndServiceListContextChangesAsync(confirmedIntegrationEvent); await _catalogIntegrationEventService.PublishThroughEventBusAsync(confirmedIntegrationEvent); } }