Add Order Payment Integration events
This commit is contained in:
parent
9ae4d93a54
commit
3d05230a2b
@ -36,12 +36,9 @@ namespace Ordering.API.Application.DomainEventHandlers.BuyerAndPaymentMethodVeri
|
||||
|
||||
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
|
||||
.SaveEventAndOrderingContextChangesAsync(orderStartedIntegrationEvent);
|
||||
|
||||
// Publish ordering integration event and mark it as published
|
||||
await _orderingIntegrationEventService
|
||||
.PublishThroughEventBusAsync(orderStartedIntegrationEvent);
|
||||
|
||||
|
@ -6,23 +6,25 @@
|
||||
using Domain.Events;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Ordering.API.Application.IntegrationCommands.Commands;
|
||||
using Ordering.API.Application.IntegrationEvents;
|
||||
|
||||
public class UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler
|
||||
: IAsyncNotificationHandler<OrderStockMethodVerifiedDomainEvent>
|
||||
{
|
||||
private readonly IOrderRepository _orderRepository;
|
||||
private readonly ILoggerFactory _logger;
|
||||
private readonly ILoggerFactory _logger;
|
||||
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
|
||||
|
||||
public UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler(
|
||||
IOrderRepository orderRepository, ILoggerFactory logger)
|
||||
IOrderRepository orderRepository, ILoggerFactory logger,
|
||||
IOrderingIntegrationEventService orderingIntegrationEventService)
|
||||
{
|
||||
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
|
||||
_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)
|
||||
{
|
||||
var orderToUpdate = await _orderRepository.GetAsync(orderStockMethodVerifiedDomainEvent.OrderId);
|
||||
@ -37,14 +39,9 @@
|
||||
.LogTrace($"Order with Id: {orderStockMethodVerifiedDomainEvent.OrderId} has been successfully updated with " +
|
||||
$"a status order id: { orderStockMethodVerifiedDomainEvent.OrderStatus.Id }");
|
||||
|
||||
|
||||
//var payOrderCommandMsg = new PayOrderCommandMsg(order.Id);
|
||||
|
||||
//// 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);
|
||||
var payOrderCommandMsg = new PayOrderCommandMsg(orderToUpdate.Id);
|
||||
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg);
|
||||
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg);
|
||||
}
|
||||
}
|
||||
}
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -7,7 +7,8 @@
|
||||
using Ordering.API.Application.IntegrationCommands.Commands;
|
||||
using Ordering.Domain.Exceptions;
|
||||
|
||||
public class OrderStockConfirmedIntegrationEventHandler : IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>
|
||||
public class OrderStockConfirmedIntegrationEventHandler :
|
||||
IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>
|
||||
{
|
||||
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
|
||||
private readonly IOrderRepository _orderRepository;
|
||||
@ -25,7 +26,7 @@
|
||||
var order = await _orderRepository.GetAsync(@event.OrderId);
|
||||
CheckValidSagaId(order);
|
||||
|
||||
order.SetOrderStockConfirmed(true);
|
||||
order.SetOrderStockConfirmed();
|
||||
|
||||
//Create Integration Event to be published through the Event Bus
|
||||
var payOrderCommandMsg = new PayOrderCommandMsg(order.Id);
|
||||
|
@ -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 System;
|
||||
@ -27,14 +29,11 @@
|
||||
var order = await _orderRepository.GetAsync(@event.OrderId);
|
||||
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);
|
||||
|
||||
foreach (var orderStockNotConfirmedItem in orderStockNotConfirmedItems)
|
||||
{
|
||||
//TODO: Add messages
|
||||
}
|
||||
order.SetOrderStockConfirmed(orderStockNotConfirmedItems);
|
||||
}
|
||||
|
||||
private void CheckValidSagaId(Order orderSaga)
|
||||
|
@ -1,7 +1,4 @@
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -72,7 +72,7 @@ namespace Ordering.API.Application.Sagas
|
||||
/// has been completed and order has not been cancelled.
|
||||
/// If so, the process continues for validation.
|
||||
/// </summary>
|
||||
/// <param name="command">
|
||||
/// <param name="event">
|
||||
/// Integration command message which is sent by a saga
|
||||
/// scheduler which provides the sagas that its grace
|
||||
/// period has completed.
|
||||
@ -91,10 +91,10 @@ namespace Ordering.API.Application.Sagas
|
||||
var orderStockList = orderSaga.OrderItems
|
||||
.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);
|
||||
|
||||
// Publish through the Event Bus and mark the saved event as published
|
||||
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(confirmOrderStockEvent);
|
||||
|
||||
await _orderingIntegrationEventService.PublishThroughEventBusAsync(confirmOrderStockEvent);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user