Skip to content

Commit d3d15f1

Browse files
committed
OrderStockConfirmedDomainEvent implemented
1 parent 2f41736 commit d3d15f1

5 files changed

Lines changed: 34 additions & 31 deletions

File tree

src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler.cs renamed to src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
1+
using System.Linq;
2+
3+
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
24
{
35
using MediatR;
46
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
@@ -9,14 +11,14 @@
911
using Ordering.API.Application.IntegrationCommands.Commands;
1012
using Ordering.API.Application.IntegrationEvents;
1113

12-
public class UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler
13-
: IAsyncNotificationHandler<OrderStockMethodVerifiedDomainEvent>
14+
public class OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler
15+
: IAsyncNotificationHandler<OrderStockConfirmedDomainEvent>
1416
{
1517
private readonly IOrderRepository _orderRepository;
1618
private readonly ILoggerFactory _logger;
1719
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
1820

19-
public UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler(
21+
public OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler(
2022
IOrderRepository orderRepository, ILoggerFactory logger,
2123
IOrderingIntegrationEventService orderingIntegrationEventService)
2224
{
@@ -25,23 +27,18 @@ public UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler(
2527
_orderingIntegrationEventService = orderingIntegrationEventService;
2628
}
2729

28-
public async Task Handle(OrderStockMethodVerifiedDomainEvent orderStockMethodVerifiedDomainEvent)
30+
public async Task Handle(OrderStockConfirmedDomainEvent orderStockMethodVerifiedDomainEvent)
2931
{
30-
var orderToUpdate = await _orderRepository.GetAsync(orderStockMethodVerifiedDomainEvent.OrderId);
31-
orderToUpdate.SetOrderStatusId(orderStockMethodVerifiedDomainEvent.OrderStatus.Id);
32-
33-
_orderRepository.Update(orderToUpdate);
34-
35-
await _orderRepository.UnitOfWork
36-
.SaveEntitiesAsync();
37-
38-
_logger.CreateLogger(nameof(UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler))
32+
_logger.CreateLogger(nameof(OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler))
3933
.LogTrace($"Order with Id: {orderStockMethodVerifiedDomainEvent.OrderId} has been successfully updated with " +
4034
$"a status order id: { orderStockMethodVerifiedDomainEvent.OrderStatus.Id }");
4135

42-
var payOrderCommandMsg = new PayOrderCommandMsg(orderToUpdate.Id);
43-
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg);
44-
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg);
36+
if (orderStockMethodVerifiedDomainEvent.OrderStatus == OrderStatus.StockValidated)
37+
{
38+
var payOrderCommandMsg = new PayOrderCommandMsg(orderStockMethodVerifiedDomainEvent.OrderId);
39+
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg);
40+
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg);
41+
}
4542
}
4643
}
4744
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Linq;
2+
using Ordering.API.Application.IntegrationCommands.Commands;
23

34
namespace Ordering.API.Application.IntegrationEvents.EventHandling
45
{
@@ -25,15 +26,18 @@ public OrderStockNotConfirmedIntegrationEventHandler(IOrderRepository orderRepos
2526

2627
public async Task Handle(OrderStockNotConfirmedIntegrationEvent @event)
2728
{
28-
//TODO: must update the order state to cancelled and the CurrentOrderStateContextDescription with the reasons of no-stock confirm
29-
var order = await _orderRepository.GetAsync(@event.OrderId);
30-
CheckValidSagaId(order);
29+
var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
30+
CheckValidSagaId(orderToUpdate);
3131

3232
var orderStockNotConfirmedItems = @event.OrderStockItems
3333
.FindAll(c => !c.Confirmed)
3434
.Select(c => c.ProductId);
3535

36-
order.SetOrderStockConfirmed(orderStockNotConfirmedItems);
36+
orderToUpdate.SetOrderStockConfirmed(orderStockNotConfirmedItems);
37+
38+
var payOrderCommandMsg = new PayOrderCommandMsg(orderToUpdate.Id);
39+
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg);
40+
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg);
3741
}
3842

3943
private void CheckValidSagaId(Order orderSaga)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public class UserCheckoutAcceptedIntegrationEventHandler : IIntegrationEventHand
1414
private readonly ILoggerFactory _logger;
1515

1616
public UserCheckoutAcceptedIntegrationEventHandler(IMediator mediator,
17-
IOrderingIntegrationEventService orderingIntegrationEventService,
1817
ILoggerFactory logger)
1918
{
2019
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));

src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,22 @@ public void SetOrderStockConfirmed(IEnumerable<int> orderStockNotConfirmedItems
103103
if(orderStockNotConfirmedItems is null)
104104
{
105105
OrderStatus = OrderStatus.StockValidated;
106+
106107
_description = "All the items were confirmed with available stock.";
107-
//AddDomainEvent(new OrderStockMethodVerifiedDomainEvent(Id, OrderStatus.StockValidated));
108108
}
109109
else
110110
{
111+
OrderStatus = OrderStatus.Cancelled;
112+
111113
var itemsStockNotConfirmedProductNames = OrderItems
112114
.Where(c => orderStockNotConfirmedItems.Contains(c.ProductId))
113115
.Select(c => c.GetOrderItemProductName());
114116

115117
var itemsStockNotConfirmedDescription = string.Join(", ", itemsStockNotConfirmedProductNames);
116-
117-
OrderStatus = OrderStatus.Cancelled;
118-
_description = $"The product items don't have stock: ({itemsStockNotConfirmedDescription}).";
119-
//AddDomainEvent(new OrderStockMethodVerifiedDomainEvent(Id, OrderStatus.Cancelled));
118+
_description = $"The product items don't have stock: ({itemsStockNotConfirmedDescription}).";
120119
}
120+
121+
AddDomainEvent(new OrderStockConfirmedDomainEvent(Id, OrderStatus));
121122
}
122123

123124
private void AddOrderStartedDomainEvent(int cardTypeId, string cardNumber,

src/Services/Ordering/Ordering.Domain/Events/OrderStockMethodVerifiedDomainEvent.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
namespace Ordering.Domain.Events
1+
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
2+
3+
namespace Ordering.Domain.Events
24
{
35
using MediatR;
4-
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
6+
using System.Collections.Generic;
57

68
/// <summary>
79
/// Event used when the order stock items are verified
810
/// </summary>
9-
public class OrderStockMethodVerifiedDomainEvent
11+
public class OrderStockConfirmedDomainEvent
1012
: IAsyncNotification
1113
{
1214
public int OrderId { get; }
1315
public OrderStatus OrderStatus { get; }
1416

15-
public OrderStockMethodVerifiedDomainEvent(int orderId,
17+
public OrderStockConfirmedDomainEvent(int orderId,
1618
OrderStatus orderStatus)
1719
{
1820
OrderId = orderId;

0 commit comments

Comments
 (0)