Skip to content

Commit bf6360a

Browse files
committed
Add Order Payment Integration events
1 parent 38f86f2 commit bf6360a

10 files changed

Lines changed: 73 additions & 32 deletions

src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/BuyerAndPaymentMethodVerified/UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,9 @@ public async Task Handle(BuyerAndPaymentMethodVerifiedDomainEvent buyerPaymentMe
3636

3737
var orderStartedIntegrationEvent = new OrderStartedIntegrationEvent(buyerPaymentMethodVerifiedEvent.Buyer.IdentityGuid);
3838

39-
// Using a local transaction to achieve atomicity between original Ordering database operation and
40-
// the IntegrationEventLog. Only saving event if order has been successfully persisted to db
4139
await _orderingIntegrationEventService
4240
.SaveEventAndOrderingContextChangesAsync(orderStartedIntegrationEvent);
4341

44-
// Publish ordering integration event and mark it as published
4542
await _orderingIntegrationEventService
4643
.PublishThroughEventBusAsync(orderStartedIntegrationEvent);
4744

src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,25 @@
66
using Domain.Events;
77
using System;
88
using System.Threading.Tasks;
9+
using Ordering.API.Application.IntegrationCommands.Commands;
10+
using Ordering.API.Application.IntegrationEvents;
911

1012
public class UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler
1113
: IAsyncNotificationHandler<OrderStockMethodVerifiedDomainEvent>
1214
{
1315
private readonly IOrderRepository _orderRepository;
14-
private readonly ILoggerFactory _logger;
16+
private readonly ILoggerFactory _logger;
17+
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
1518

1619
public UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler(
17-
IOrderRepository orderRepository, ILoggerFactory logger)
20+
IOrderRepository orderRepository, ILoggerFactory logger,
21+
IOrderingIntegrationEventService orderingIntegrationEventService)
1822
{
1923
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
2024
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
25+
_orderingIntegrationEventService = orderingIntegrationEventService;
2126
}
2227

23-
// Domain Logic comment:
24-
// When the Order Stock items method have been validate and confirmed,
25-
// then we can update the original Order with the new order status
2628
public async Task Handle(OrderStockMethodVerifiedDomainEvent orderStockMethodVerifiedDomainEvent)
2729
{
2830
var orderToUpdate = await _orderRepository.GetAsync(orderStockMethodVerifiedDomainEvent.OrderId);
@@ -37,14 +39,9 @@ await _orderRepository.UnitOfWork
3739
.LogTrace($"Order with Id: {orderStockMethodVerifiedDomainEvent.OrderId} has been successfully updated with " +
3840
$"a status order id: { orderStockMethodVerifiedDomainEvent.OrderStatus.Id }");
3941

40-
41-
//var payOrderCommandMsg = new PayOrderCommandMsg(order.Id);
42-
43-
//// Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
44-
//await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg);
45-
46-
//// Publish through the Event Bus and mark the saved event as published
47-
//await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg);
42+
var payOrderCommandMsg = new PayOrderCommandMsg(orderToUpdate.Id);
43+
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg);
44+
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg);
4845
}
4946
}
5047
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Ordering.API.Application.IntegrationEvents.EventHandling
2+
{
3+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
4+
using Ordering.API.Application.IntegrationEvents.Events;
5+
using System.Threading.Tasks;
6+
7+
public class OrderPaymentFailedIntegrationEventHandler :
8+
IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>
9+
{
10+
public async Task Handle(OrderPaymentFailedIntegrationEvent @event)
11+
{
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Ordering.API.Application.IntegrationEvents.EventHandling
2+
{
3+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
4+
using Ordering.API.Application.IntegrationEvents.Events;
5+
using System.Threading.Tasks;
6+
7+
public class OrderPaymentSuccededIntegrationEventHandler :
8+
IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>
9+
{
10+
public async Task Handle(OrderPaymentSuccededIntegrationEvent @event)
11+
{
12+
}
13+
}
14+
}

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
using Ordering.API.Application.IntegrationCommands.Commands;
88
using Ordering.Domain.Exceptions;
99

10-
public class OrderStockConfirmedIntegrationEventHandler : IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>
10+
public class OrderStockConfirmedIntegrationEventHandler :
11+
IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>
1112
{
1213
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
1314
private readonly IOrderRepository _orderRepository;
@@ -25,7 +26,7 @@ public async Task Handle(OrderStockConfirmedIntegrationEvent @event)
2526
var order = await _orderRepository.GetAsync(@event.OrderId);
2627
CheckValidSagaId(order);
2728

28-
order.SetOrderStockConfirmed(true);
29+
order.SetOrderStockConfirmed();
2930

3031
//Create Integration Event to be published through the Event Bus
3132
var payOrderCommandMsg = new PayOrderCommandMsg(order.Id);

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Ordering.API.Application.IntegrationEvents.EventHandling
1+
using System.Linq;
2+
3+
namespace Ordering.API.Application.IntegrationEvents.EventHandling
24
{
35
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
46
using System;
@@ -27,14 +29,11 @@ public async Task Handle(OrderStockNotConfirmedIntegrationEvent @event)
2729
var order = await _orderRepository.GetAsync(@event.OrderId);
2830
CheckValidSagaId(order);
2931

30-
order.SetOrderStockConfirmed(false);
31-
32-
var orderStockNotConfirmedItems = @event.OrderStockItems.FindAll(c => !c.Confirmed);
32+
var orderStockNotConfirmedItems = @event.OrderStockItems
33+
.FindAll(c => !c.Confirmed)
34+
.Select(c => c.ProductId);
3335

34-
foreach (var orderStockNotConfirmedItem in orderStockNotConfirmedItems)
35-
{
36-
//TODO: Add messages
37-
}
36+
order.SetOrderStockConfirmed(orderStockNotConfirmedItems);
3837
}
3938

4039
private void CheckValidSagaId(Order orderSaga)

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
52
using System.Threading.Tasks;
63

74
namespace Ordering.API.Application.IntegrationEvents.EventHandling
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
2+
3+
namespace Ordering.API.Application.IntegrationEvents.Events
4+
{
5+
public class OrderPaymentFailedIntegrationEvent : IntegrationEvent
6+
{
7+
public int OrderId { get; }
8+
9+
public OrderPaymentFailedIntegrationEvent(int orderId) => OrderId = orderId;
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
2+
3+
namespace Ordering.API.Application.IntegrationEvents.Events
4+
{
5+
public class OrderPaymentSuccededIntegrationEvent : IntegrationEvent
6+
{
7+
public int OrderId { get; }
8+
9+
public OrderPaymentSuccededIntegrationEvent(int orderId) => OrderId = orderId;
10+
}
11+
}

src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public async Task Handle(SubmitOrderCommandMsg command)
7272
/// has been completed and order has not been cancelled.
7373
/// If so, the process continues for validation.
7474
/// </summary>
75-
/// <param name="command">
75+
/// <param name="event">
7676
/// Integration command message which is sent by a saga
7777
/// scheduler which provides the sagas that its grace
7878
/// period has completed.
@@ -91,10 +91,10 @@ public async Task Handle(ConfirmGracePeriodCommandMsg @event)
9191
var orderStockList = orderSaga.OrderItems
9292
.Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits()));
9393

94-
//Create Integration Event to be published through the Event Bus
9594
var confirmOrderStockEvent = new ConfirmOrderStockCommandMsg(orderSaga.Id, orderStockList);
9695

97-
// Publish through the Event Bus and mark the saved event as published
96+
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(confirmOrderStockEvent);
97+
9898
await _orderingIntegrationEventService.PublishThroughEventBusAsync(confirmOrderStockEvent);
9999
}
100100
}

0 commit comments

Comments
 (0)