Skip to content

Commit 9a6ad2c

Browse files
committed
Continue implementing OrderStockConfirmed events
1 parent b598c91 commit 9a6ad2c

11 files changed

Lines changed: 189 additions & 29 deletions

File tree

src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/ConfirmOrderStockIntegrationEventHandler.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
3-
using Catalog.API.Infrastructure.Exceptions;
4-
using Catalog.API.IntegrationEvents;
5-
using Microsoft.eShopOnContainers.Services.Catalog.API.Model;
6-
7-
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.EventHandling
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.EventHandling
82
{
93
using BuildingBlocks.EventBus.Abstractions;
104
using System.Threading.Tasks;
115
using BuildingBlocks.EventBus.Events;
126
using Infrastructure;
13-
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using global::Catalog.API.Infrastructure.Exceptions;
10+
using global::Catalog.API.IntegrationEvents;
11+
using Model;
1412
using Events;
1513

1614
public class ConfirmOrderStockIntegrationEventHandler : IIntegrationEventHandler<ConfirmOrderStockIntegrationEvent>
@@ -41,12 +39,15 @@ public async Task Handle(ConfirmOrderStockIntegrationEvent @event)
4139
}
4240

4341
//Create Integration Event to be published through the Event Bus
44-
var integrationEvent = confirmedOrderStockItems.Any(c => !c.Confirmed)
42+
var confirmedIntegrationEvent = confirmedOrderStockItems.Any(c => !c.Confirmed)
4543
? (IntegrationEvent) new OrderStockNotConfirmedIntegrationEvent(@event.OrderId, confirmedOrderStockItems)
4644
: new OrderStockConfirmedIntegrationEvent(@event.OrderId);
47-
45+
46+
// Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
47+
await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(confirmedIntegrationEvent);
48+
4849
// Publish through the Event Bus and mark the saved event as published
49-
await _catalogIntegrationEventService.PublishThroughEventBusAsync(integrationEvent);
50+
await _catalogIntegrationEventService.PublishThroughEventBusAsync(confirmedIntegrationEvent);
5051
}
5152

5253
private void CheckValidcatalogItemId(CatalogItem catalogItem)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
2+
{
3+
using MediatR;
4+
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
5+
using Microsoft.Extensions.Logging;
6+
using Domain.Events;
7+
using System;
8+
using System.Threading.Tasks;
9+
10+
public class UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler
11+
: IAsyncNotificationHandler<OrderStockMethodVerifiedDomainEvent>
12+
{
13+
private readonly IOrderRepository _orderRepository;
14+
private readonly ILoggerFactory _logger;
15+
16+
public UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler(
17+
IOrderRepository orderRepository, ILoggerFactory logger)
18+
{
19+
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
20+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
21+
}
22+
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
26+
public async Task Handle(OrderStockMethodVerifiedDomainEvent orderStockMethodVerifiedDomainEvent)
27+
{
28+
var orderToUpdate = await _orderRepository.GetAsync(orderStockMethodVerifiedDomainEvent.OrderId);
29+
orderToUpdate.SetOrderStatusId(orderStockMethodVerifiedDomainEvent.OrderStatus.Id);
30+
31+
_orderRepository.Update(orderToUpdate);
32+
33+
await _orderRepository.UnitOfWork
34+
.SaveEntitiesAsync();
35+
36+
_logger.CreateLogger(nameof(UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler))
37+
.LogTrace($"Order with Id: {orderStockMethodVerifiedDomainEvent.OrderId} has been successfully updated with " +
38+
$"a status order id: { orderStockMethodVerifiedDomainEvent.OrderStatus.Id }");
39+
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);
48+
}
49+
}
50+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Ordering.API.Application.IntegrationCommands.Commands
2+
{
3+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
4+
5+
public class PayOrderCommandMsg : IntegrationEvent
6+
{
7+
public int OrderId { get; }
8+
9+
public PayOrderCommandMsg(int orderId)
10+
{
11+
OrderId = orderId;
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,48 @@
11
namespace Ordering.API.Application.IntegrationEvents.EventHandling
22
{
33
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
4-
using System;
54
using System.Threading.Tasks;
65
using Events;
6+
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
7+
using Ordering.API.Application.IntegrationCommands.Commands;
8+
using Ordering.Domain.Exceptions;
79

810
public class OrderStockConfirmedIntegrationEventHandler : IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>
911
{
12+
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
13+
private readonly IOrderRepository _orderRepository;
14+
15+
public OrderStockConfirmedIntegrationEventHandler(IOrderRepository orderRepository,
16+
IOrderingIntegrationEventService orderingIntegrationEventService)
17+
{
18+
_orderRepository = orderRepository;
19+
_orderingIntegrationEventService = orderingIntegrationEventService;
20+
}
21+
1022
public async Task Handle(OrderStockConfirmedIntegrationEvent @event)
1123
{
1224
//TODO: 1) Updates the state to "StockValidated" and any meaningful OrderContextDescription message saying that all the items were confirmed with available stock, etc
13-
//TODO: 2) Sends a Command-message (PayOrderCommand msg/bus) to the Payment svc. from Ordering micro (thru Command Bus, as a message, NOT http)
25+
var order = await _orderRepository.GetAsync(@event.OrderId);
26+
CheckValidSagaId(order);
27+
28+
order.SetOrderStockConfirmed(true);
1429

15-
throw new NotImplementedException();
30+
//Create Integration Event to be published through the Event Bus
31+
var payOrderCommandMsg = new PayOrderCommandMsg(order.Id);
32+
33+
// Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
34+
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg);
35+
36+
// Publish through the Event Bus and mark the saved event as published
37+
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg);
38+
}
39+
40+
private void CheckValidSagaId(Order orderSaga)
41+
{
42+
if (orderSaga is null)
43+
{
44+
throw new OrderingDomainException("Not able to process order saga event. Reason: no valid orderId");
45+
}
1646
}
1747
}
1848
}

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,45 @@
44
using System;
55
using System.Threading.Tasks;
66
using Events;
7+
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
8+
using Ordering.API.Application.Sagas;
9+
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
10+
using Ordering.Domain.Exceptions;
711

812
public class OrderStockNotConfirmedIntegrationEventHandler : IIntegrationEventHandler<OrderStockNotConfirmedIntegrationEvent>
913
{
14+
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
15+
private readonly IOrderRepository _orderRepository;
16+
17+
public OrderStockNotConfirmedIntegrationEventHandler(IOrderRepository orderRepository,
18+
IOrderingIntegrationEventService orderingIntegrationEventService)
19+
{
20+
_orderRepository = orderRepository;
21+
_orderingIntegrationEventService = orderingIntegrationEventService;
22+
}
23+
1024
public async Task Handle(OrderStockNotConfirmedIntegrationEvent @event)
1125
{
1226
//TODO: must update the order state to cancelled and the CurrentOrderStateContextDescription with the reasons of no-stock confirm
13-
//TODO: for this/that articles which is info coming in that integration event. --> ORDER PROCESS
27+
var order = await _orderRepository.GetAsync(@event.OrderId);
28+
CheckValidSagaId(order);
29+
30+
order.SetOrderStockConfirmed(false);
1431

15-
throw new NotImplementedException();
32+
var orderStockNotConfirmedItems = @event.OrderStockItems.FindAll(c => !c.Confirmed);
33+
34+
foreach (var orderStockNotConfirmedItem in orderStockNotConfirmedItems)
35+
{
36+
//TODO: Add messages
37+
}
38+
}
39+
40+
private void CheckValidSagaId(Order orderSaga)
41+
{
42+
if (orderSaga is null)
43+
{
44+
throw new OrderingDomainException("Not able to process order saga event. Reason: no valid orderId");
45+
}
1646
}
1747
}
1848
}

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ public class OrderStockNotConfirmedIntegrationEvent : IntegrationEvent
88
{
99
public int OrderId { get; }
1010

11-
public IEnumerable<ConfirmedOrderStockItem> OrderStockItem { get; }
11+
public List<ConfirmedOrderStockItem> OrderStockItems { get; }
1212

1313
public OrderStockNotConfirmedIntegrationEvent(int orderId,
14-
IEnumerable<ConfirmedOrderStockItem> orderStockItem)
14+
List<ConfirmedOrderStockItem> orderStockItems)
1515
{
1616
OrderId = orderId;
17-
OrderStockItem = orderStockItem;
17+
OrderStockItems = orderStockItems;
1818
}
1919
}
2020

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,24 @@ public async Task Handle(SubmitOrderCommandMsg command)
7878
/// period has completed.
7979
/// </param>
8080
/// <returns></returns>
81-
public async Task Handle(ConfirmGracePeriodCommandMsg command)
81+
public async Task Handle(ConfirmGracePeriodCommandMsg @event)
8282
{
83-
var orderSaga = FindSagaById(command.OrderId);
83+
var orderSaga = FindSagaById(@event.OrderId);
8484
CheckValidSagaId(orderSaga);
8585

8686
if (orderSaga.OrderStatus != OrderStatus.Cancelled)
8787
{
8888
orderSaga.SetOrderStatusId(OrderStatus.AwaitingValidation.Id);
8989
await SaveChangesAsync();
9090

91-
var orderStockList = orderSaga.OrderItems
92-
.Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits()));
91+
var orderStockList = orderSaga.OrderItems
92+
.Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits()));
9393

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

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

src/Services/Ordering/Ordering.API/Ordering.API.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
</ItemGroup>
8080

8181
<ItemGroup>
82-
<Folder Include="Application\DomainEventHandlers\OrderStockMethodVerified\" />
8382
<Folder Include="Application\IntegrationCommands\CommandHandlers\" />
8483
<Folder Include="Infrastructure\IntegrationEventMigrations\" />
8584
</ItemGroup>

src/Services/Ordering/Ordering.API/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
127127

128128
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
129129
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
130+
130131
services.AddTransient<UserCheckoutAcceptedIntegrationEventHandler>();
131132
services.AddTransient<IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>, OrderProcessSaga>();
132-
services.AddTransient<IIntegrationEventHandler<SubmitOrderCommandMsg>, OrderProcessSaga>();
133133
services.AddTransient<OrderStockConfirmedIntegrationEventHandler>();
134134
services.AddTransient<OrderStockNotConfirmedIntegrationEventHandler>();
135135
services.AddOptions();

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ public void SetOrderStatusId(int id)
9999
_orderStatusId = id;
100100
}
101101

102+
public void SetOrderStockConfirmed(bool confirmed)
103+
{
104+
if(confirmed)
105+
{
106+
OrderStatus = OrderStatus.StockValidated;
107+
AddDomainEvent(new OrderStockMethodVerifiedDomainEvent(Id, OrderStatus.StockValidated));
108+
}
109+
else
110+
{
111+
OrderStatus = OrderStatus.Cancelled;
112+
AddDomainEvent(new OrderStockMethodVerifiedDomainEvent(Id, OrderStatus.Cancelled));
113+
}
114+
}
115+
102116
private void AddOrderStartedDomainEvent(int cardTypeId, string cardNumber,
103117
string cardSecurityNumber, string cardHolderName, DateTime cardExpiration)
104118
{

0 commit comments

Comments
 (0)