Browse Source

OrderStockConfirmedDomainEvent implemented

pull/809/head
Christian Arenas 7 years ago
parent
commit
d6ccf27100
5 changed files with 34 additions and 31 deletions
  1. +14
    -17
      src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler.cs
  2. +8
    -4
      src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs
  3. +0
    -1
      src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs
  4. +6
    -5
      src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs
  5. +6
    -4
      src/Services/Ordering/Ordering.Domain/Events/OrderStockMethodVerifiedDomainEvent.cs

src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler.cs → src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler.cs View File

@ -1,4 +1,6 @@
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
using System.Linq;
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
{
using MediatR;
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
@ -9,14 +11,14 @@
using Ordering.API.Application.IntegrationCommands.Commands;
using Ordering.API.Application.IntegrationEvents;
public class UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler
: IAsyncNotificationHandler<OrderStockMethodVerifiedDomainEvent>
public class OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler
: IAsyncNotificationHandler<OrderStockConfirmedDomainEvent>
{
private readonly IOrderRepository _orderRepository;
private readonly ILoggerFactory _logger;
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
public UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler(
public OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler(
IOrderRepository orderRepository, ILoggerFactory logger,
IOrderingIntegrationEventService orderingIntegrationEventService)
{
@ -25,23 +27,18 @@
_orderingIntegrationEventService = orderingIntegrationEventService;
}
public async Task Handle(OrderStockMethodVerifiedDomainEvent orderStockMethodVerifiedDomainEvent)
public async Task Handle(OrderStockConfirmedDomainEvent orderStockMethodVerifiedDomainEvent)
{
var orderToUpdate = await _orderRepository.GetAsync(orderStockMethodVerifiedDomainEvent.OrderId);
orderToUpdate.SetOrderStatusId(orderStockMethodVerifiedDomainEvent.OrderStatus.Id);
_orderRepository.Update(orderToUpdate);
await _orderRepository.UnitOfWork
.SaveEntitiesAsync();
_logger.CreateLogger(nameof(UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler))
_logger.CreateLogger(nameof(OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler))
.LogTrace($"Order with Id: {orderStockMethodVerifiedDomainEvent.OrderId} has been successfully updated with " +
$"a status order id: { orderStockMethodVerifiedDomainEvent.OrderStatus.Id }");
var payOrderCommandMsg = new PayOrderCommandMsg(orderToUpdate.Id);
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg);
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg);
if (orderStockMethodVerifiedDomainEvent.OrderStatus == OrderStatus.StockValidated)
{
var payOrderCommandMsg = new PayOrderCommandMsg(orderStockMethodVerifiedDomainEvent.OrderId);
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg);
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg);
}
}
}
}

+ 8
- 4
src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs View File

@ -1,4 +1,5 @@
using System.Linq;
using Ordering.API.Application.IntegrationCommands.Commands;
namespace Ordering.API.Application.IntegrationEvents.EventHandling
{
@ -25,15 +26,18 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling
public async Task Handle(OrderStockNotConfirmedIntegrationEvent @event)
{
//TODO: must update the order state to cancelled and the CurrentOrderStateContextDescription with the reasons of no-stock confirm
var order = await _orderRepository.GetAsync(@event.OrderId);
CheckValidSagaId(order);
var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
CheckValidSagaId(orderToUpdate);
var orderStockNotConfirmedItems = @event.OrderStockItems
.FindAll(c => !c.Confirmed)
.Select(c => c.ProductId);
order.SetOrderStockConfirmed(orderStockNotConfirmedItems);
orderToUpdate.SetOrderStockConfirmed(orderStockNotConfirmedItems);
var payOrderCommandMsg = new PayOrderCommandMsg(orderToUpdate.Id);
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg);
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg);
}
private void CheckValidSagaId(Order orderSaga)


+ 0
- 1
src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs View File

@ -14,7 +14,6 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling
private readonly ILoggerFactory _logger;
public UserCheckoutAcceptedIntegrationEventHandler(IMediator mediator,
IOrderingIntegrationEventService orderingIntegrationEventService,
ILoggerFactory logger)
{
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));


+ 6
- 5
src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs View File

@ -103,21 +103,22 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
if(orderStockNotConfirmedItems is null)
{
OrderStatus = OrderStatus.StockValidated;
_description = "All the items were confirmed with available stock.";
//AddDomainEvent(new OrderStockMethodVerifiedDomainEvent(Id, OrderStatus.StockValidated));
}
else
{
OrderStatus = OrderStatus.Cancelled;
var itemsStockNotConfirmedProductNames = OrderItems
.Where(c => orderStockNotConfirmedItems.Contains(c.ProductId))
.Select(c => c.GetOrderItemProductName());
var itemsStockNotConfirmedDescription = string.Join(", ", itemsStockNotConfirmedProductNames);
OrderStatus = OrderStatus.Cancelled;
_description = $"The product items don't have stock: ({itemsStockNotConfirmedDescription}).";
//AddDomainEvent(new OrderStockMethodVerifiedDomainEvent(Id, OrderStatus.Cancelled));
_description = $"The product items don't have stock: ({itemsStockNotConfirmedDescription}).";
}
AddDomainEvent(new OrderStockConfirmedDomainEvent(Id, OrderStatus));
}
private void AddOrderStartedDomainEvent(int cardTypeId, string cardNumber,


+ 6
- 4
src/Services/Ordering/Ordering.Domain/Events/OrderStockMethodVerifiedDomainEvent.cs View File

@ -1,18 +1,20 @@
namespace Ordering.Domain.Events
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
namespace Ordering.Domain.Events
{
using MediatR;
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
using System.Collections.Generic;
/// <summary>
/// Event used when the order stock items are verified
/// </summary>
public class OrderStockMethodVerifiedDomainEvent
public class OrderStockConfirmedDomainEvent
: IAsyncNotification
{
public int OrderId { get; }
public OrderStatus OrderStatus { get; }
public OrderStockMethodVerifiedDomainEvent(int orderId,
public OrderStockConfirmedDomainEvent(int orderId,
OrderStatus orderStatus)
{
OrderId = orderId;


Loading…
Cancel
Save