@ -0,0 +1,50 @@ | |||||
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent | |||||
{ | |||||
using MediatR; | |||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | |||||
using Microsoft.Extensions.Logging; | |||||
using Domain.Events; | |||||
using System; | |||||
using System.Threading.Tasks; | |||||
public class UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler | |||||
: IAsyncNotificationHandler<OrderStockMethodVerifiedDomainEvent> | |||||
{ | |||||
private readonly IOrderRepository _orderRepository; | |||||
private readonly ILoggerFactory _logger; | |||||
public UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler( | |||||
IOrderRepository orderRepository, ILoggerFactory logger) | |||||
{ | |||||
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); | |||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | |||||
} | |||||
// 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); | |||||
orderToUpdate.SetOrderStatusId(orderStockMethodVerifiedDomainEvent.OrderStatus.Id); | |||||
_orderRepository.Update(orderToUpdate); | |||||
await _orderRepository.UnitOfWork | |||||
.SaveEntitiesAsync(); | |||||
_logger.CreateLogger(nameof(UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler)) | |||||
.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); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,14 @@ | |||||
namespace Ordering.API.Application.IntegrationCommands.Commands | |||||
{ | |||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; | |||||
public class PayOrderCommandMsg : IntegrationEvent | |||||
{ | |||||
public int OrderId { get; } | |||||
public PayOrderCommandMsg(int orderId) | |||||
{ | |||||
OrderId = orderId; | |||||
} | |||||
} | |||||
} |
@ -1,18 +1,48 @@ | |||||
namespace Ordering.API.Application.IntegrationEvents.EventHandling | namespace Ordering.API.Application.IntegrationEvents.EventHandling | ||||
{ | { | ||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | ||||
using System; | |||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
using Events; | using Events; | ||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | |||||
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; | |||||
public OrderStockConfirmedIntegrationEventHandler(IOrderRepository orderRepository, | |||||
IOrderingIntegrationEventService orderingIntegrationEventService) | |||||
{ | |||||
_orderRepository = orderRepository; | |||||
_orderingIntegrationEventService = orderingIntegrationEventService; | |||||
} | |||||
public async Task Handle(OrderStockConfirmedIntegrationEvent @event) | public async Task Handle(OrderStockConfirmedIntegrationEvent @event) | ||||
{ | { | ||||
//TODO: 1) Updates the state to "StockValidated" and any meaningful OrderContextDescription message saying that all the items were confirmed with available stock, etc | //TODO: 1) Updates the state to "StockValidated" and any meaningful OrderContextDescription message saying that all the items were confirmed with available stock, etc | ||||
//TODO: 2) Sends a Command-message (PayOrderCommand msg/bus) to the Payment svc. from Ordering micro (thru Command Bus, as a message, NOT http) | |||||
var order = await _orderRepository.GetAsync(@event.OrderId); | |||||
CheckValidSagaId(order); | |||||
order.SetOrderStockConfirmed(true); | |||||
throw new NotImplementedException(); | |||||
//Create Integration Event to be published through the Event Bus | |||||
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); | |||||
} | |||||
private void CheckValidSagaId(Order orderSaga) | |||||
{ | |||||
if (orderSaga is null) | |||||
{ | |||||
throw new OrderingDomainException("Not able to process order saga event. Reason: no valid orderId"); | |||||
} | |||||
} | } | ||||
} | } | ||||
} | } |
@ -0,0 +1,22 @@ | |||||
namespace Ordering.Domain.Events | |||||
{ | |||||
using MediatR; | |||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | |||||
/// <summary> | |||||
/// Event used when the order stock items are verified | |||||
/// </summary> | |||||
public class OrderStockMethodVerifiedDomainEvent | |||||
: IAsyncNotification | |||||
{ | |||||
public int OrderId { get; } | |||||
public OrderStatus OrderStatus { get; } | |||||
public OrderStockMethodVerifiedDomainEvent(int orderId, | |||||
OrderStatus orderStatus) | |||||
{ | |||||
OrderId = orderId; | |||||
OrderStatus = orderStatus; | |||||
} | |||||
} | |||||
} |