Add Order Payment Integration events

This commit is contained in:
Christian Arenas 2017-05-11 16:05:27 +02:00
parent 9ae4d93a54
commit 3d05230a2b
10 changed files with 73 additions and 32 deletions

View File

@ -36,12 +36,9 @@ namespace Ordering.API.Application.DomainEventHandlers.BuyerAndPaymentMethodVeri
var orderStartedIntegrationEvent = new OrderStartedIntegrationEvent(buyerPaymentMethodVerifiedEvent.Buyer.IdentityGuid); var orderStartedIntegrationEvent = new OrderStartedIntegrationEvent(buyerPaymentMethodVerifiedEvent.Buyer.IdentityGuid);
// Using a local transaction to achieve atomicity between original Ordering database operation and
// the IntegrationEventLog. Only saving event if order has been successfully persisted to db
await _orderingIntegrationEventService await _orderingIntegrationEventService
.SaveEventAndOrderingContextChangesAsync(orderStartedIntegrationEvent); .SaveEventAndOrderingContextChangesAsync(orderStartedIntegrationEvent);
// Publish ordering integration event and mark it as published
await _orderingIntegrationEventService await _orderingIntegrationEventService
.PublishThroughEventBusAsync(orderStartedIntegrationEvent); .PublishThroughEventBusAsync(orderStartedIntegrationEvent);

View File

@ -6,23 +6,25 @@
using Domain.Events; using Domain.Events;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Ordering.API.Application.IntegrationCommands.Commands;
using Ordering.API.Application.IntegrationEvents;
public class UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler public class UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler
: IAsyncNotificationHandler<OrderStockMethodVerifiedDomainEvent> : IAsyncNotificationHandler<OrderStockMethodVerifiedDomainEvent>
{ {
private readonly IOrderRepository _orderRepository; private readonly IOrderRepository _orderRepository;
private readonly ILoggerFactory _logger; private readonly ILoggerFactory _logger;
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
public UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler( public UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler(
IOrderRepository orderRepository, ILoggerFactory logger) IOrderRepository orderRepository, ILoggerFactory logger,
IOrderingIntegrationEventService orderingIntegrationEventService)
{ {
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));
_orderingIntegrationEventService = orderingIntegrationEventService;
} }
// Domain Logic comment:
// When the Order Stock items method have been validate and confirmed,
// then we can update the original Order with the new order status
public async Task Handle(OrderStockMethodVerifiedDomainEvent orderStockMethodVerifiedDomainEvent) public async Task Handle(OrderStockMethodVerifiedDomainEvent orderStockMethodVerifiedDomainEvent)
{ {
var orderToUpdate = await _orderRepository.GetAsync(orderStockMethodVerifiedDomainEvent.OrderId); var orderToUpdate = await _orderRepository.GetAsync(orderStockMethodVerifiedDomainEvent.OrderId);
@ -37,14 +39,9 @@
.LogTrace($"Order with Id: {orderStockMethodVerifiedDomainEvent.OrderId} has been successfully updated with " + .LogTrace($"Order with Id: {orderStockMethodVerifiedDomainEvent.OrderId} has been successfully updated with " +
$"a status order id: { orderStockMethodVerifiedDomainEvent.OrderStatus.Id }"); $"a status order id: { orderStockMethodVerifiedDomainEvent.OrderStatus.Id }");
var payOrderCommandMsg = new PayOrderCommandMsg(orderToUpdate.Id);
//var payOrderCommandMsg = new PayOrderCommandMsg(order.Id); await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg);
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg);
//// Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
//await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg);
//// Publish through the Event Bus and mark the saved event as published
//await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg);
} }
} }
} }

View File

@ -0,0 +1,14 @@
namespace Ordering.API.Application.IntegrationEvents.EventHandling
{
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Ordering.API.Application.IntegrationEvents.Events;
using System.Threading.Tasks;
public class OrderPaymentFailedIntegrationEventHandler :
IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>
{
public async Task Handle(OrderPaymentFailedIntegrationEvent @event)
{
}
}
}

View File

@ -0,0 +1,14 @@
namespace Ordering.API.Application.IntegrationEvents.EventHandling
{
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Ordering.API.Application.IntegrationEvents.Events;
using System.Threading.Tasks;
public class OrderPaymentSuccededIntegrationEventHandler :
IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>
{
public async Task Handle(OrderPaymentSuccededIntegrationEvent @event)
{
}
}
}

View File

@ -7,7 +7,8 @@
using Ordering.API.Application.IntegrationCommands.Commands; using Ordering.API.Application.IntegrationCommands.Commands;
using Ordering.Domain.Exceptions; using Ordering.Domain.Exceptions;
public class OrderStockConfirmedIntegrationEventHandler : IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent> public class OrderStockConfirmedIntegrationEventHandler :
IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>
{ {
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
private readonly IOrderRepository _orderRepository; private readonly IOrderRepository _orderRepository;
@ -25,7 +26,7 @@
var order = await _orderRepository.GetAsync(@event.OrderId); var order = await _orderRepository.GetAsync(@event.OrderId);
CheckValidSagaId(order); CheckValidSagaId(order);
order.SetOrderStockConfirmed(true); order.SetOrderStockConfirmed();
//Create Integration Event to be published through the Event Bus //Create Integration Event to be published through the Event Bus
var payOrderCommandMsg = new PayOrderCommandMsg(order.Id); var payOrderCommandMsg = new PayOrderCommandMsg(order.Id);

View File

@ -1,4 +1,6 @@
namespace Ordering.API.Application.IntegrationEvents.EventHandling using System.Linq;
namespace Ordering.API.Application.IntegrationEvents.EventHandling
{ {
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using System; using System;
@ -27,14 +29,11 @@
var order = await _orderRepository.GetAsync(@event.OrderId); var order = await _orderRepository.GetAsync(@event.OrderId);
CheckValidSagaId(order); CheckValidSagaId(order);
order.SetOrderStockConfirmed(false); var orderStockNotConfirmedItems = @event.OrderStockItems
.FindAll(c => !c.Confirmed)
.Select(c => c.ProductId);
var orderStockNotConfirmedItems = @event.OrderStockItems.FindAll(c => !c.Confirmed); order.SetOrderStockConfirmed(orderStockNotConfirmedItems);
foreach (var orderStockNotConfirmedItem in orderStockNotConfirmedItems)
{
//TODO: Add messages
}
} }
private void CheckValidSagaId(Order orderSaga) private void CheckValidSagaId(Order orderSaga)

View File

@ -1,7 +1,4 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Ordering.API.Application.IntegrationEvents.EventHandling namespace Ordering.API.Application.IntegrationEvents.EventHandling

View File

@ -0,0 +1,11 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
namespace Ordering.API.Application.IntegrationEvents.Events
{
public class OrderPaymentFailedIntegrationEvent : IntegrationEvent
{
public int OrderId { get; }
public OrderPaymentFailedIntegrationEvent(int orderId) => OrderId = orderId;
}
}

View File

@ -0,0 +1,11 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
namespace Ordering.API.Application.IntegrationEvents.Events
{
public class OrderPaymentSuccededIntegrationEvent : IntegrationEvent
{
public int OrderId { get; }
public OrderPaymentSuccededIntegrationEvent(int orderId) => OrderId = orderId;
}
}

View File

@ -72,7 +72,7 @@ namespace Ordering.API.Application.Sagas
/// has been completed and order has not been cancelled. /// has been completed and order has not been cancelled.
/// If so, the process continues for validation. /// If so, the process continues for validation.
/// </summary> /// </summary>
/// <param name="command"> /// <param name="event">
/// Integration command message which is sent by a saga /// Integration command message which is sent by a saga
/// scheduler which provides the sagas that its grace /// scheduler which provides the sagas that its grace
/// period has completed. /// period has completed.
@ -91,10 +91,10 @@ namespace Ordering.API.Application.Sagas
var orderStockList = orderSaga.OrderItems var orderStockList = orderSaga.OrderItems
.Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits())); .Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits()));
//Create Integration Event to be published through the Event Bus
var confirmOrderStockEvent = new ConfirmOrderStockCommandMsg(orderSaga.Id, orderStockList); var confirmOrderStockEvent = new ConfirmOrderStockCommandMsg(orderSaga.Id, orderStockList);
// Publish through the Event Bus and mark the saved event as published await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(confirmOrderStockEvent);
await _orderingIntegrationEventService.PublishThroughEventBusAsync(confirmOrderStockEvent); await _orderingIntegrationEventService.PublishThroughEventBusAsync(confirmOrderStockEvent);
} }
} }