public async Task Consume(ConsumeContext <OrderRegistered> context) { var id = context.CorrelationId.GetValueOrDefault(); var address = context.Message.Address; logger.LogInformation($"Received {nameof(OrderRegistered)} message with Id = '{id}' and Address = '{address}'."); dbContext.Add(new Delivery { Id = id, Address = address }); await dbContext.SaveChangesAsync(); logger.LogInformation($"Delivery '{id}' created. Waiting for Payment, Stock and Billing services."); await context.Publish <DeliveryPrepared>(new { context.CorrelationId }); }
public async Task Consume(ConsumeContext <PaymentRefused> context) { var id = context.CorrelationId.GetValueOrDefault(); var delivery = await dbContext.Deliveries.FindAsync(id); if (delivery is null) { return; } logger.LogInformation($"'{id}' exists in this context. Deleting because of {nameof(PaymentRefused)} message."); dbContext.Deliveries.Remove(delivery); await dbContext.SaveChangesAsync(); await context.Publish <DeliveryCancelled>(new { context.CorrelationId }); }
public async Task Consume(ConsumeContext <BillingCompleted> context) { var id = context.CorrelationId.GetValueOrDefault(); logger.LogInformation($"Received {nameof(BillingCompleted)} message with Id = '{id}'"); var delivery = await dbContext.Deliveries.FindAsync(id); if (delivery is null) { logger.LogInformation($"'{id}' does not exists in this context. Rejecting, will retry in a few."); throw new DeliveryNotFoundException(); } logger.LogInformation($"'{id}' exists in this context."); delivery.IsBillingCompleted = true; var shouldSendIsShippedMessage = false; if (delivery.IsBillingCompleted && delivery.IsPaymentAccepted && delivery.IsStockBooked) { logger.LogInformation($"'{id}' can be shipped !"); delivery.IsShipped = true; shouldSendIsShippedMessage = true; } await dbContext.SaveChangesAsync(); logger.LogInformation($"'{id}' updated."); if (shouldSendIsShippedMessage) { await context.Publish <DeliveryStarted>(new { context.CorrelationId }); } }