@ -0,0 +1,46 @@ | |||
namespace Ordering.API.Application.DomainEventHandlers.OrderGracePeriodConfirmed | |||
{ | |||
using MediatR; | |||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | |||
using Microsoft.Extensions.Logging; | |||
using Domain.Events; | |||
using System; | |||
using System.Threading.Tasks; | |||
using Ordering.API.Application.IntegrationCommands.Commands; | |||
using Ordering.API.Application.IntegrationEvents; | |||
using System.Linq; | |||
public class OrderStatusChangedToAwaitingValidationDomainEventHandler | |||
: IAsyncNotificationHandler<OrderStatusChangedToAwaitingValidationDomainEvent> | |||
{ | |||
private readonly IOrderRepository _orderRepository; | |||
private readonly ILoggerFactory _logger; | |||
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; | |||
public OrderStatusChangedToAwaitingValidationDomainEventHandler( | |||
IOrderRepository orderRepository, ILoggerFactory logger, | |||
IOrderingIntegrationEventService orderingIntegrationEventService) | |||
{ | |||
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); | |||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | |||
_orderingIntegrationEventService = orderingIntegrationEventService; | |||
} | |||
public async Task Handle(OrderStatusChangedToAwaitingValidationDomainEvent orderStatusChangedToAwaitingValidationDomainEvent) | |||
{ | |||
await _orderRepository.UnitOfWork.SaveEntitiesAsync(); | |||
_logger.CreateLogger(nameof(OrderStatusChangedToAwaitingValidationDomainEvent)) | |||
.LogTrace($"Order with Id: {orderStatusChangedToAwaitingValidationDomainEvent.OrderId} has been successfully updated with " + | |||
$"a status order id: {OrderStatus.AwaitingValidation.Id}"); | |||
var orderStockList = orderStatusChangedToAwaitingValidationDomainEvent.OrderItems | |||
.Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits())); | |||
var confirmOrderStockEvent = new ConfirmOrderStockCommandMsg(orderStatusChangedToAwaitingValidationDomainEvent.OrderId, | |||
orderStockList); | |||
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(confirmOrderStockEvent); | |||
await _orderingIntegrationEventService.PublishThroughEventBusAsync(confirmOrderStockEvent); | |||
} | |||
} | |||
} |
@ -0,0 +1,51 @@ | |||
namespace Ordering.API.Application.DomainEventHandlers.OrderPaid | |||
{ | |||
using MediatR; | |||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | |||
using Microsoft.Extensions.Logging; | |||
using Domain.Events; | |||
using System; | |||
using System.Threading.Tasks; | |||
using Ordering.API.Application.IntegrationCommands.Commands; | |||
using Ordering.API.Application.IntegrationEvents; | |||
using System.Linq; | |||
public class OrderStatusChangedToPaidDomainEventHandler | |||
: IAsyncNotificationHandler<OrderStatusChangedToPaidDomainEvent> | |||
{ | |||
private readonly IOrderRepository _orderRepository; | |||
private readonly ILoggerFactory _logger; | |||
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; | |||
public OrderStatusChangedToPaidDomainEventHandler( | |||
IOrderRepository orderRepository, ILoggerFactory logger, | |||
IOrderingIntegrationEventService orderingIntegrationEventService) | |||
{ | |||
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); | |||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | |||
_orderingIntegrationEventService = orderingIntegrationEventService; | |||
} | |||
public async Task Handle(OrderStatusChangedToPaidDomainEvent orderStatusChangedToPaidDomainEvent) | |||
{ | |||
await _orderRepository.UnitOfWork.SaveEntitiesAsync(); | |||
_logger.CreateLogger(nameof(OrderStatusChangedToPaidDomainEventHandler)) | |||
.LogTrace($"Order with Id: {orderStatusChangedToPaidDomainEvent.OrderId} has been successfully updated with " + | |||
$"a status order id: {OrderStatus.Paid.Id}"); | |||
var orderStockList = orderStatusChangedToPaidDomainEvent.OrderItems | |||
.Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits())); | |||
var decrementOrderStockCommandMsg = new DecrementOrderStockCommandMsg(orderStatusChangedToPaidDomainEvent.OrderId, | |||
orderStockList); | |||
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(decrementOrderStockCommandMsg); | |||
await _orderingIntegrationEventService.PublishThroughEventBusAsync(decrementOrderStockCommandMsg); | |||
//is it necessary get a DecrementOrderStockSuccessIntegrationEvent/DecrementOrderStockFailedIntegrationEvent before to call ShipOrderCommandMsg??? | |||
var shipOrderCommandMsg = new ShipOrderCommandMsg(orderStatusChangedToPaidDomainEvent.OrderId); | |||
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(shipOrderCommandMsg); | |||
await _orderingIntegrationEventService.PublishThroughEventBusAsync(shipOrderCommandMsg); | |||
} | |||
} | |||
} |
@ -1,44 +0,0 @@ | |||
using System.Linq; | |||
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; | |||
using Ordering.API.Application.IntegrationCommands.Commands; | |||
using Ordering.API.Application.IntegrationEvents; | |||
public class OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler | |||
: IAsyncNotificationHandler<OrderStockConfirmedDomainEvent> | |||
{ | |||
private readonly IOrderRepository _orderRepository; | |||
private readonly ILoggerFactory _logger; | |||
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; | |||
public OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler( | |||
IOrderRepository orderRepository, ILoggerFactory logger, | |||
IOrderingIntegrationEventService orderingIntegrationEventService) | |||
{ | |||
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); | |||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | |||
_orderingIntegrationEventService = orderingIntegrationEventService; | |||
} | |||
public async Task Handle(OrderStockConfirmedDomainEvent orderStockMethodVerifiedDomainEvent) | |||
{ | |||
_logger.CreateLogger(nameof(OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler)) | |||
.LogTrace($"Order with Id: {orderStockMethodVerifiedDomainEvent.OrderId} has been successfully updated with " + | |||
$"a status order id: { orderStockMethodVerifiedDomainEvent.OrderStatus.Id }"); | |||
if (orderStockMethodVerifiedDomainEvent.OrderStatus == OrderStatus.StockValidated) | |||
{ | |||
var payOrderCommandMsg = new PayOrderCommandMsg(orderStockMethodVerifiedDomainEvent.OrderId); | |||
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg); | |||
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg); | |||
} | |||
} | |||
} | |||
} |
@ -0,0 +1,41 @@ | |||
namespace Ordering.API.Application.DomainEventHandlers.OrderStockConfirmed | |||
{ | |||
using MediatR; | |||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | |||
using Microsoft.Extensions.Logging; | |||
using Domain.Events; | |||
using System; | |||
using System.Threading.Tasks; | |||
using Ordering.API.Application.IntegrationCommands.Commands; | |||
using Ordering.API.Application.IntegrationEvents; | |||
public class OrderStatusChangedToStockConfirmedDomainEventHandler | |||
: IAsyncNotificationHandler<OrderStatusChangedToStockConfirmedDomainEvent> | |||
{ | |||
private readonly IOrderRepository _orderRepository; | |||
private readonly ILoggerFactory _logger; | |||
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; | |||
public OrderStatusChangedToStockConfirmedDomainEventHandler( | |||
IOrderRepository orderRepository, ILoggerFactory logger, | |||
IOrderingIntegrationEventService orderingIntegrationEventService) | |||
{ | |||
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); | |||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | |||
_orderingIntegrationEventService = orderingIntegrationEventService; | |||
} | |||
public async Task Handle(OrderStatusChangedToStockConfirmedDomainEvent orderStatusChangedToStockConfirmedDomainEvent) | |||
{ | |||
await _orderRepository.UnitOfWork.SaveEntitiesAsync(); | |||
_logger.CreateLogger(nameof(OrderStatusChangedToStockConfirmedDomainEventHandler)) | |||
.LogTrace($"Order with Id: {orderStatusChangedToStockConfirmedDomainEvent.OrderId} has been successfully updated with " + | |||
$"a status order id: {OrderStatus.StockConfirmed.Id}"); | |||
var payOrderCommandMsg = new PayOrderCommandMsg(orderStatusChangedToStockConfirmedDomainEvent.OrderId); | |||
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg); | |||
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg); | |||
} | |||
} | |||
} |
@ -0,0 +1,18 @@ | |||
namespace Ordering.API.Application.IntegrationCommands.Commands | |||
{ | |||
using System.Collections.Generic; | |||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; | |||
public class DecrementOrderStockCommandMsg : IntegrationEvent | |||
{ | |||
public int OrderId { get; } | |||
public IEnumerable<OrderStockItem> OrderStockItems { get; } | |||
public DecrementOrderStockCommandMsg(int orderId, | |||
IEnumerable<OrderStockItem> orderStockItems) | |||
{ | |||
OrderId = orderId; | |||
OrderStockItems = orderStockItems; | |||
} | |||
} | |||
} |
@ -0,0 +1,14 @@ | |||
namespace Ordering.API.Application.IntegrationCommands.Commands | |||
{ | |||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; | |||
public class ShipOrderCommandMsg : IntegrationEvent | |||
{ | |||
public int OrderId { get; } | |||
public ShipOrderCommandMsg(int orderId) | |||
{ | |||
OrderId = orderId; | |||
} | |||
} | |||
} |
@ -1,14 +1,23 @@ | |||
namespace Ordering.API.Application.IntegrationEvents.EventHandling | |||
{ | |||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | |||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | |||
using Ordering.API.Application.IntegrationEvents.Events; | |||
using System.Threading.Tasks; | |||
public class OrderPaymentFailedIntegrationEventHandler : | |||
IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent> | |||
{ | |||
private readonly IOrderRepository _orderRepository; | |||
public OrderPaymentFailedIntegrationEventHandler(IOrderRepository orderRepository) | |||
{ | |||
_orderRepository = orderRepository; | |||
} | |||
public async Task Handle(OrderPaymentFailedIntegrationEvent @event) | |||
{ | |||
//TODO: Cancel Order | |||
} | |||
} | |||
} |
@ -1,14 +1,35 @@ | |||
namespace Ordering.API.Application.IntegrationEvents.EventHandling | |||
{ | |||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | |||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | |||
using Ordering.API.Application.IntegrationEvents.Events; | |||
using Ordering.Domain.Exceptions; | |||
using System.Threading.Tasks; | |||
public class OrderPaymentSuccededIntegrationEventHandler : | |||
IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent> | |||
{ | |||
private readonly IOrderRepository _orderRepository; | |||
public OrderPaymentSuccededIntegrationEventHandler(IOrderRepository orderRepository) | |||
{ | |||
_orderRepository = orderRepository; | |||
} | |||
public async Task Handle(OrderPaymentSuccededIntegrationEvent @event) | |||
{ | |||
var order = await _orderRepository.GetAsync(@event.OrderId); | |||
CheckValidSagaId(order); | |||
order.SetPaidStatus(); | |||
} | |||
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,36 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Threading.Tasks; | |||
using Microsoft.EntityFrameworkCore; | |||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | |||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; | |||
namespace Ordering.API.Application.Sagas | |||
{ | |||
public abstract class OrderSaga : Saga<Order> | |||
{ | |||
private OrderingContext _orderingContext; | |||
public OrderSaga(OrderingContext orderingContext) : base(orderingContext) | |||
{ | |||
_orderingContext = orderingContext; | |||
} | |||
public override Order FindSagaById(int id) | |||
{ | |||
var order = _orderingContext.Orders | |||
.Single(c => c.Id == id); | |||
_orderingContext.Entry(order) | |||
.Member("OrderStatus"); | |||
return order; | |||
} | |||
public override async Task<bool> SaveChangesAsync() | |||
{ | |||
return await _orderingContext.SaveEntitiesAsync(); | |||
} | |||
} | |||
} |