From 76c45d128a54069b97e1f3aacdb205a65d3c1c29 Mon Sep 17 00:00:00 2001 From: hsn Date: Mon, 10 Jul 2023 00:14:30 +0300 Subject: [PATCH] OrderCompletedDomainEventHandler is added. --- .../OrderCompletedDomainEventHandler.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderCompletedDomainEventHandler.cs diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderCompletedDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderCompletedDomainEventHandler.cs new file mode 100644 index 000000000..3625c6c88 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderCompletedDomainEventHandler.cs @@ -0,0 +1,33 @@ +namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.DomainEventHandlers; + +public partial class OrderCompletedDomainEventHandler + : INotificationHandler +{ + private readonly IOrderRepository _orderRepository; + private readonly IBuyerRepository _buyerRepository; + private readonly ILogger _logger; + private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; + + public OrderCompletedDomainEventHandler( + IOrderRepository orderRepository, + ILogger logger, + IBuyerRepository buyerRepository, + IOrderingIntegrationEventService orderingIntegrationEventService) + { + _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _buyerRepository = buyerRepository ?? throw new ArgumentNullException(nameof(buyerRepository)); + _orderingIntegrationEventService = orderingIntegrationEventService; + } + + public async Task Handle(OrderCompletedDomainEvent domainEvent, CancellationToken cancellationToken) + { + OrderingApiTrace.LogOrderStatusUpdated(_logger, domainEvent.Order.Id, nameof(OrderStatus.Cancelled), OrderStatus.Cancelled.Id); + + var order = await _orderRepository.GetAsync(domainEvent.Order.Id); + var buyer = await _buyerRepository.FindByIdAsync(order.GetBuyerId.Value.ToString()); + + var integrationEvent = new OrderStatusChangedToCancelledIntegrationEvent(order.Id, order.OrderStatus.Name, buyer.Name); + await _orderingIntegrationEventService.AddAndSaveEventAsync(integrationEvent); + } +}