コード例 #1
0
        public async Task Handle(ApplicationStatusChangedToAwaitingValidationIntegrationEvent @event)
        {
            using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}"))
            {
                _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event);

                var confirmedApplicationSlotItems = new List <ConfirmedApplicationSlotItem>();

                foreach (var applicationSlotItem in @event.ApplicationSlotItems)
                {
                    var scholarshipItem = _scholarshipContext.ScholarshipItems.Find(applicationSlotItem.ScholarshipItemId);
                    var hasSlots        = scholarshipItem.AvailableSlots >= applicationSlotItem.Slots;
                    var confirmedApplicationSlotItem = new ConfirmedApplicationSlotItem(scholarshipItem.Id, hasSlots);

                    confirmedApplicationSlotItems.Add(confirmedApplicationSlotItem);
                }

                var confirmedIntegrationEvent = confirmedApplicationSlotItems.Any(c => !c.HasSlots)
                    ? (IntegrationEvent) new ApplicationSlotRejectedIntegrationEvent(@event.ApplicationId, confirmedApplicationSlotItems)
                    : new ApplicationSlotConfirmedIntegrationEvent(@event.ApplicationId);

                await _scholarshipIntegrationEventService.SaveEventAndScholarshipContextChangesAsync(confirmedIntegrationEvent);

                await _scholarshipIntegrationEventService.PublishThroughEventBusAsync(confirmedIntegrationEvent);
            }
        }
コード例 #2
0
        public async Task<ActionResult> UpdateScholarshipItemAsync([FromBody] ScholarshipItem scholarshipItemToUpdate)
        {
            var scholarshipItem = await _scholarshipContext.ScholarshipItems.SingleOrDefaultAsync(i => i.Id == scholarshipItemToUpdate.Id);

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

            var oldAmount = scholarshipItem.Amount;
            var raiseScholarshipItemAmountChangedEvent = oldAmount != scholarshipItemToUpdate.Amount;

            // Update current scholarshipItem
            scholarshipItem = scholarshipItemToUpdate;
            _scholarshipContext.ScholarshipItems.Update(scholarshipItem);

            if (raiseScholarshipItemAmountChangedEvent) // Save scholarshipItem's data and publish integration event through the Event Bus if Amount has changed
            {
                //Create Integration Event to be published through the Event Bus
                var amountChangedEvent = new ScholarshipItemAmountChangedIntegrationEvent(scholarshipItem.Id, scholarshipItemToUpdate.Amount, oldAmount);

                // Achieving atomicity between original Scholarship database operation and the IntegrationEventLog thanks to a local transaction
                await _scholarshipIntegrationEventService.SaveEventAndScholarshipContextChangesAsync(amountChangedEvent);

                // Publish through the Event Bus and mark the saved event as published
                await _scholarshipIntegrationEventService.PublishThroughEventBusAsync(amountChangedEvent);
            }
            else // Just save the updated scholarshipItem because the ScholarshipItem's Amount hasn't changed.
            {
                await _scholarshipContext.SaveChangesAsync();
            }

            return CreatedAtAction(nameof(ItemByIdAsync), new { id = scholarshipItemToUpdate.Id }, null);
        }