- Change Integration Command to Integration Events
- Refactor PublishThroughEventBusAsync methods from OrderingIntegrationEventService - Add private empty Address constructor. - Modify order aggregate methods. - Remove GetWithDependenciesAsync methods and modify GetAsync with entity framework Explicit loading.
This commit is contained in:
parent
e842cd81b3
commit
55a82c24f4
@ -36,11 +36,7 @@ namespace Ordering.API.Application.DomainEventHandlers.BuyerAndPaymentMethodVeri
|
|||||||
|
|
||||||
var orderStartedIntegrationEvent = new OrderStartedIntegrationEvent(buyerPaymentMethodVerifiedEvent.Buyer.IdentityGuid);
|
var orderStartedIntegrationEvent = new OrderStartedIntegrationEvent(buyerPaymentMethodVerifiedEvent.Buyer.IdentityGuid);
|
||||||
|
|
||||||
await _orderingIntegrationEventService
|
await _orderingIntegrationEventService.PublishThroughEventBusAsync(orderStartedIntegrationEvent);
|
||||||
.SaveEventAndOrderingContextChangesAsync(orderStartedIntegrationEvent);
|
|
||||||
|
|
||||||
await _orderingIntegrationEventService
|
|
||||||
.PublishThroughEventBusAsync(orderStartedIntegrationEvent);
|
|
||||||
|
|
||||||
_logger.CreateLogger(nameof(UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler))
|
_logger.CreateLogger(nameof(UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler))
|
||||||
.LogTrace($"Order with Id: {buyerPaymentMethodVerifiedEvent.OrderId} has been successfully updated with a payment method id: { buyerPaymentMethodVerifiedEvent.Payment.Id }");
|
.LogTrace($"Order with Id: {buyerPaymentMethodVerifiedEvent.OrderId} has been successfully updated with a payment method id: { buyerPaymentMethodVerifiedEvent.Payment.Id }");
|
||||||
|
@ -6,9 +6,9 @@
|
|||||||
using Domain.Events;
|
using Domain.Events;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Ordering.API.Application.IntegrationCommands.Commands;
|
|
||||||
using Ordering.API.Application.IntegrationEvents;
|
using Ordering.API.Application.IntegrationEvents;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Ordering.API.Application.IntegrationEvents.Events;
|
||||||
|
|
||||||
public class OrderStatusChangedToAwaitingValidationDomainEventHandler
|
public class OrderStatusChangedToAwaitingValidationDomainEventHandler
|
||||||
: IAsyncNotificationHandler<OrderStatusChangedToAwaitingValidationDomainEvent>
|
: IAsyncNotificationHandler<OrderStatusChangedToAwaitingValidationDomainEvent>
|
||||||
@ -35,10 +35,9 @@
|
|||||||
var orderStockList = orderStatusChangedToAwaitingValidationDomainEvent.OrderItems
|
var orderStockList = orderStatusChangedToAwaitingValidationDomainEvent.OrderItems
|
||||||
.Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits()));
|
.Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits()));
|
||||||
|
|
||||||
var confirmOrderStockCommand = new ConfirmOrderStockCommand(orderStatusChangedToAwaitingValidationDomainEvent.OrderId,
|
var orderStatusChangedToAwaitingValidationIntegrationEvent = new OrderStatusChangedToAwaitingValidationIntegrationEvent(
|
||||||
orderStockList);
|
orderStatusChangedToAwaitingValidationDomainEvent.OrderId, orderStockList);
|
||||||
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(confirmOrderStockCommand);
|
await _orderingIntegrationEventService.PublishThroughEventBusAsync(orderStatusChangedToAwaitingValidationIntegrationEvent);
|
||||||
await _orderingIntegrationEventService.PublishThroughEventBusAsync(confirmOrderStockCommand);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,9 +6,9 @@
|
|||||||
using Domain.Events;
|
using Domain.Events;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Ordering.API.Application.IntegrationCommands.Commands;
|
|
||||||
using Ordering.API.Application.IntegrationEvents;
|
using Ordering.API.Application.IntegrationEvents;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Ordering.API.Application.IntegrationEvents.Events;
|
||||||
|
|
||||||
public class OrderStatusChangedToPaidDomainEventHandler
|
public class OrderStatusChangedToPaidDomainEventHandler
|
||||||
: IAsyncNotificationHandler<OrderStatusChangedToPaidDomainEvent>
|
: IAsyncNotificationHandler<OrderStatusChangedToPaidDomainEvent>
|
||||||
@ -35,10 +35,9 @@
|
|||||||
var orderStockList = orderStatusChangedToPaidDomainEvent.OrderItems
|
var orderStockList = orderStatusChangedToPaidDomainEvent.OrderItems
|
||||||
.Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits()));
|
.Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits()));
|
||||||
|
|
||||||
var decrementOrderStockCommand = new DecrementOrderStockCommand(orderStatusChangedToPaidDomainEvent.OrderId,
|
var orderStatusChangedToPaidIntegrationEvent = new OrderStatusChangedToPaidIntegrationEvent(orderStatusChangedToPaidDomainEvent.OrderId,
|
||||||
orderStockList);
|
orderStockList);
|
||||||
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(decrementOrderStockCommand);
|
await _orderingIntegrationEventService.PublishThroughEventBusAsync(orderStatusChangedToPaidIntegrationEvent);
|
||||||
await _orderingIntegrationEventService.PublishThroughEventBusAsync(decrementOrderStockCommand);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,8 +6,8 @@
|
|||||||
using Domain.Events;
|
using Domain.Events;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Ordering.API.Application.IntegrationCommands.Commands;
|
|
||||||
using Ordering.API.Application.IntegrationEvents;
|
using Ordering.API.Application.IntegrationEvents;
|
||||||
|
using Ordering.API.Application.IntegrationEvents.Events;
|
||||||
|
|
||||||
public class OrderStatusChangedToStockConfirmedDomainEventHandler
|
public class OrderStatusChangedToStockConfirmedDomainEventHandler
|
||||||
: IAsyncNotificationHandler<OrderStatusChangedToStockConfirmedDomainEvent>
|
: IAsyncNotificationHandler<OrderStatusChangedToStockConfirmedDomainEvent>
|
||||||
@ -31,9 +31,8 @@
|
|||||||
.LogTrace($"Order with Id: {orderStatusChangedToStockConfirmedDomainEvent.OrderId} has been successfully updated with " +
|
.LogTrace($"Order with Id: {orderStatusChangedToStockConfirmedDomainEvent.OrderId} has been successfully updated with " +
|
||||||
$"a status order id: {OrderStatus.StockConfirmed.Id}");
|
$"a status order id: {OrderStatus.StockConfirmed.Id}");
|
||||||
|
|
||||||
var payOrderCommand = new PayOrderCommand(orderStatusChangedToStockConfirmedDomainEvent.OrderId);
|
var orderStatusChangedToStockConfirmedIntegrationEvent = new OrderStatusChangedToStockConfirmedIntegrationEvent(orderStatusChangedToStockConfirmedDomainEvent.OrderId);
|
||||||
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommand);
|
await _orderingIntegrationEventService.PublishThroughEventBusAsync(orderStatusChangedToStockConfirmedIntegrationEvent);
|
||||||
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommand);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,12 +0,0 @@
|
|||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
|
||||||
|
|
||||||
namespace Ordering.API.Application.IntegrationCommands.Commands
|
|
||||||
{
|
|
||||||
public class ConfirmGracePeriodCommand : IntegrationEvent
|
|
||||||
{
|
|
||||||
public int OrderId { get; }
|
|
||||||
|
|
||||||
public ConfirmGracePeriodCommand(int orderId) =>
|
|
||||||
OrderId = orderId;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
namespace Ordering.API.Application.IntegrationCommands.Commands
|
|
||||||
{
|
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
|
||||||
|
|
||||||
public class PayOrderCommand : IntegrationEvent
|
|
||||||
{
|
|
||||||
public int OrderId { get; }
|
|
||||||
|
|
||||||
public PayOrderCommand(int orderId)
|
|
||||||
{
|
|
||||||
OrderId = orderId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
public async Task Handle(OrderPaymentFailedIntegrationEvent @event)
|
public async Task Handle(OrderPaymentFailedIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId);
|
var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
|
||||||
|
|
||||||
orderToUpdate.SetCancelledStatus();
|
orderToUpdate.SetCancelledStatus();
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
public async Task Handle(OrderPaymentSuccededIntegrationEvent @event)
|
public async Task Handle(OrderPaymentSuccededIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId);
|
var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
|
||||||
|
|
||||||
orderToUpdate.SetPaidStatus();
|
orderToUpdate.SetPaidStatus();
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
public async Task Handle(OrderStockConfirmedIntegrationEvent @event)
|
public async Task Handle(OrderStockConfirmedIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId);
|
var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
|
||||||
|
|
||||||
orderToUpdate.SetStockConfirmedStatus();
|
orderToUpdate.SetStockConfirmedStatus();
|
||||||
|
|
||||||
|
@ -1,33 +0,0 @@
|
|||||||
using System.Linq;
|
|
||||||
using Ordering.API.Application.IntegrationCommands.Commands;
|
|
||||||
|
|
||||||
namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
|
||||||
{
|
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Events;
|
|
||||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
|
||||||
|
|
||||||
public class OrderStockNotConfirmedIntegrationEventHandler : IIntegrationEventHandler<OrderStockNotConfirmedIntegrationEvent>
|
|
||||||
{
|
|
||||||
private readonly IOrderRepository _orderRepository;
|
|
||||||
|
|
||||||
public OrderStockNotConfirmedIntegrationEventHandler(IOrderRepository orderRepository)
|
|
||||||
{
|
|
||||||
_orderRepository = orderRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task Handle(OrderStockNotConfirmedIntegrationEvent @event)
|
|
||||||
{
|
|
||||||
var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId);
|
|
||||||
|
|
||||||
var orderStockNotConfirmedItems = @event.OrderStockItems
|
|
||||||
.FindAll(c => !c.HasStock)
|
|
||||||
.Select(c => c.ProductId);
|
|
||||||
|
|
||||||
orderToUpdate.SetStockConfirmedStatus(orderStockNotConfirmedItems);
|
|
||||||
|
|
||||||
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,31 @@
|
|||||||
|
namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
||||||
|
{
|
||||||
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Events;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||||
|
|
||||||
|
public class OrderStockRejectedIntegrationEventHandler : IIntegrationEventHandler<OrderStockRejectedIntegrationEvent>
|
||||||
|
{
|
||||||
|
private readonly IOrderRepository _orderRepository;
|
||||||
|
|
||||||
|
public OrderStockRejectedIntegrationEventHandler(IOrderRepository orderRepository)
|
||||||
|
{
|
||||||
|
_orderRepository = orderRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Handle(OrderStockRejectedIntegrationEvent @event)
|
||||||
|
{
|
||||||
|
var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
|
||||||
|
|
||||||
|
var orderStockNotConfirmedItems = @event.OrderStockItems
|
||||||
|
.FindAll(c => !c.HasStock)
|
||||||
|
.Select(c => c.ProductId);
|
||||||
|
|
||||||
|
orderToUpdate.SetCancelledStatusWhenStockIsRejected(orderStockNotConfirmedItems);
|
||||||
|
|
||||||
|
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
namespace Ordering.API.Application.IntegrationEvents.Events
|
||||||
|
{
|
||||||
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||||
|
|
||||||
|
public class GracePeriodConfirmedIntegrationEvent : IntegrationEvent
|
||||||
|
{
|
||||||
|
public int OrderId { get; }
|
||||||
|
|
||||||
|
public GracePeriodConfirmedIntegrationEvent(int orderId) =>
|
||||||
|
OrderId = orderId;
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +1,11 @@
|
|||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
namespace Ordering.API.Application.IntegrationEvents.Events
|
||||||
|
|
||||||
namespace Ordering.API.Application.IntegrationEvents.Events
|
|
||||||
{
|
{
|
||||||
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||||
|
|
||||||
public class OrderPaymentFailedIntegrationEvent : IntegrationEvent
|
public class OrderPaymentFailedIntegrationEvent : IntegrationEvent
|
||||||
{
|
{
|
||||||
public int OrderId { get; }
|
public int OrderId { get; }
|
||||||
|
|
||||||
public OrderPaymentFailedIntegrationEvent(int orderId) => OrderId = orderId;
|
public OrderPaymentFailedIntegrationEvent(int orderId) => OrderId = orderId;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,11 +1,11 @@
|
|||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
namespace Ordering.API.Application.IntegrationEvents.Events
|
||||||
|
|
||||||
namespace Ordering.API.Application.IntegrationEvents.Events
|
|
||||||
{
|
{
|
||||||
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||||
|
|
||||||
public class OrderPaymentSuccededIntegrationEvent : IntegrationEvent
|
public class OrderPaymentSuccededIntegrationEvent : IntegrationEvent
|
||||||
{
|
{
|
||||||
public int OrderId { get; }
|
public int OrderId { get; }
|
||||||
|
|
||||||
public OrderPaymentSuccededIntegrationEvent(int orderId) => OrderId = orderId;
|
public OrderPaymentSuccededIntegrationEvent(int orderId) => OrderId = orderId;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,14 +1,14 @@
|
|||||||
namespace Ordering.API.Application.IntegrationCommands.Commands
|
namespace Ordering.API.Application.IntegrationEvents.Events
|
||||||
{
|
{
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||||
|
|
||||||
public class ConfirmOrderStockCommand : IntegrationEvent
|
public class OrderStatusChangedToAwaitingValidationIntegrationEvent : IntegrationEvent
|
||||||
{
|
{
|
||||||
public int OrderId { get; }
|
public int OrderId { get; }
|
||||||
public IEnumerable<OrderStockItem> OrderStockItems { get; }
|
public IEnumerable<OrderStockItem> OrderStockItems { get; }
|
||||||
|
|
||||||
public ConfirmOrderStockCommand(int orderId,
|
public OrderStatusChangedToAwaitingValidationIntegrationEvent(int orderId,
|
||||||
IEnumerable<OrderStockItem> orderStockItems)
|
IEnumerable<OrderStockItem> orderStockItems)
|
||||||
{
|
{
|
||||||
OrderId = orderId;
|
OrderId = orderId;
|
@ -1,14 +1,14 @@
|
|||||||
namespace Ordering.API.Application.IntegrationCommands.Commands
|
namespace Ordering.API.Application.IntegrationEvents.Events
|
||||||
{
|
{
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||||
|
|
||||||
public class DecrementOrderStockCommand : IntegrationEvent
|
public class OrderStatusChangedToPaidIntegrationEvent : IntegrationEvent
|
||||||
{
|
{
|
||||||
public int OrderId { get; }
|
public int OrderId { get; }
|
||||||
public IEnumerable<OrderStockItem> OrderStockItems { get; }
|
public IEnumerable<OrderStockItem> OrderStockItems { get; }
|
||||||
|
|
||||||
public DecrementOrderStockCommand(int orderId,
|
public OrderStatusChangedToPaidIntegrationEvent(int orderId,
|
||||||
IEnumerable<OrderStockItem> orderStockItems)
|
IEnumerable<OrderStockItem> orderStockItems)
|
||||||
{
|
{
|
||||||
OrderId = orderId;
|
OrderId = orderId;
|
@ -0,0 +1,12 @@
|
|||||||
|
namespace Ordering.API.Application.IntegrationEvents.Events
|
||||||
|
{
|
||||||
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||||
|
|
||||||
|
public class OrderStatusChangedToStockConfirmedIntegrationEvent : IntegrationEvent
|
||||||
|
{
|
||||||
|
public int OrderId { get; }
|
||||||
|
|
||||||
|
public OrderStatusChangedToStockConfirmedIntegrationEvent(int orderId)
|
||||||
|
=> OrderId = orderId;
|
||||||
|
}
|
||||||
|
}
|
@ -1,16 +1,15 @@
|
|||||||
using System.Collections.Generic;
|
namespace Ordering.API.Application.IntegrationEvents.Events
|
||||||
|
|
||||||
namespace Ordering.API.Application.IntegrationEvents.Events
|
|
||||||
{
|
{
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
public class OrderStockNotConfirmedIntegrationEvent : IntegrationEvent
|
public class OrderStockRejectedIntegrationEvent : IntegrationEvent
|
||||||
{
|
{
|
||||||
public int OrderId { get; }
|
public int OrderId { get; }
|
||||||
|
|
||||||
public List<ConfirmedOrderStockItem> OrderStockItems { get; }
|
public List<ConfirmedOrderStockItem> OrderStockItems { get; }
|
||||||
|
|
||||||
public OrderStockNotConfirmedIntegrationEvent(int orderId,
|
public OrderStockRejectedIntegrationEvent(int orderId,
|
||||||
List<ConfirmedOrderStockItem> orderStockItems)
|
List<ConfirmedOrderStockItem> orderStockItems)
|
||||||
{
|
{
|
||||||
OrderId = orderId;
|
OrderId = orderId;
|
@ -5,7 +5,6 @@ namespace Ordering.API.Application.IntegrationEvents
|
|||||||
{
|
{
|
||||||
public interface IOrderingIntegrationEventService
|
public interface IOrderingIntegrationEventService
|
||||||
{
|
{
|
||||||
Task SaveEventAndOrderingContextChangesAsync(IntegrationEvent evt);
|
|
||||||
Task PublishThroughEventBusAsync(IntegrationEvent evt);
|
Task PublishThroughEventBusAsync(IntegrationEvent evt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,11 +30,12 @@ namespace Ordering.API.Application.IntegrationEvents
|
|||||||
|
|
||||||
public async Task PublishThroughEventBusAsync(IntegrationEvent evt)
|
public async Task PublishThroughEventBusAsync(IntegrationEvent evt)
|
||||||
{
|
{
|
||||||
|
await SaveEventAndOrderingContextChangesAsync(evt);
|
||||||
_eventBus.Publish(evt);
|
_eventBus.Publish(evt);
|
||||||
await _eventLogService.MarkEventAsPublishedAsync(evt);
|
await _eventLogService.MarkEventAsPublishedAsync(evt);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SaveEventAndOrderingContextChangesAsync(IntegrationEvent evt)
|
private async Task SaveEventAndOrderingContextChangesAsync(IntegrationEvent evt)
|
||||||
{
|
{
|
||||||
//Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction():
|
//Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction():
|
||||||
//See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
//See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
||||||
|
@ -5,8 +5,7 @@ using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.Order
|
|||||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
|
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
|
||||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
|
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
|
||||||
using Ordering.API.Application.Commands;
|
using Ordering.API.Application.Commands;
|
||||||
using Ordering.API.Application.IntegrationCommands.Commands;
|
using Ordering.API.Application.IntegrationEvents.Events;
|
||||||
using Ordering.API.Application.IntegrationEvents;
|
|
||||||
using Ordering.Domain.Exceptions;
|
using Ordering.Domain.Exceptions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -21,7 +20,7 @@ namespace Ordering.API.Application.Sagas
|
|||||||
/// with the validations.
|
/// with the validations.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class OrderProcessSaga : OrderSaga,
|
public class OrderProcessSaga : OrderSaga,
|
||||||
IIntegrationEventHandler<ConfirmGracePeriodCommand>,
|
IIntegrationEventHandler<GracePeriodConfirmedIntegrationEvent>,
|
||||||
IAsyncRequestHandler<CancelOrderCommand, bool>,
|
IAsyncRequestHandler<CancelOrderCommand, bool>,
|
||||||
IAsyncRequestHandler<ShipOrderCommand, bool>
|
IAsyncRequestHandler<ShipOrderCommand, bool>
|
||||||
{
|
{
|
||||||
@ -43,9 +42,9 @@ namespace Ordering.API.Application.Sagas
|
|||||||
/// period has completed.
|
/// period has completed.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task Handle(ConfirmGracePeriodCommand command)
|
public async Task Handle(GracePeriodConfirmedIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
var orderSaga = FindSagaById(command.OrderId);
|
var orderSaga = FindSagaById(@event.OrderId);
|
||||||
CheckValidSagaId(orderSaga);
|
CheckValidSagaId(orderSaga);
|
||||||
|
|
||||||
orderSaga.SetAwaitingValidationStatus();
|
orderSaga.SetAwaitingValidationStatus();
|
||||||
@ -96,8 +95,6 @@ namespace Ordering.API.Application.Sagas
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#region CommandHandlerIdentifiers
|
|
||||||
|
|
||||||
public class CancelOrderCommandIdentifiedHandler : IdentifierCommandHandler<CancelOrderCommand, bool>
|
public class CancelOrderCommandIdentifiedHandler : IdentifierCommandHandler<CancelOrderCommand, bool>
|
||||||
{
|
{
|
||||||
public CancelOrderCommandIdentifiedHandler(IMediator mediator, IRequestManager requestManager) : base(mediator, requestManager)
|
public CancelOrderCommandIdentifiedHandler(IMediator mediator, IRequestManager requestManager) : base(mediator, requestManager)
|
||||||
@ -121,7 +118,5 @@ namespace Ordering.API.Application.Sagas
|
|||||||
return true; // Ignore duplicate requests for processing order.
|
return true; // Ignore duplicate requests for processing order.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ namespace Ordering.API.Application.Sagas
|
|||||||
var order = _orderingContext.Orders
|
var order = _orderingContext.Orders
|
||||||
.Include(c => c.OrderStatus)
|
.Include(c => c.OrderStatus)
|
||||||
.Include(c => c.OrderItems)
|
.Include(c => c.OrderItems)
|
||||||
|
.Include(c => c.Address)
|
||||||
.Single(c => c.Id == id);
|
.Single(c => c.Id == id);
|
||||||
|
|
||||||
return order;
|
return order;
|
||||||
|
@ -80,6 +80,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Application\IntegrationCommands\CommandHandlers\" />
|
<Folder Include="Application\IntegrationCommands\CommandHandlers\" />
|
||||||
|
<Folder Include="Application\IntegrationCommands\Commands\" />
|
||||||
<Folder Include="Infrastructure\IntegrationEventMigrations\" />
|
<Folder Include="Infrastructure\IntegrationEventMigrations\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -3,10 +3,8 @@
|
|||||||
using AspNetCore.Http;
|
using AspNetCore.Http;
|
||||||
using Autofac;
|
using Autofac;
|
||||||
using Autofac.Extensions.DependencyInjection;
|
using Autofac.Extensions.DependencyInjection;
|
||||||
using global::Ordering.API.Application.IntegrationCommands.Commands;
|
|
||||||
using global::Ordering.API.Application.IntegrationEvents;
|
using global::Ordering.API.Application.IntegrationEvents;
|
||||||
using global::Ordering.API.Application.IntegrationEvents.Events;
|
using global::Ordering.API.Application.IntegrationEvents.Events;
|
||||||
using global::Ordering.API.Application.Sagas;
|
|
||||||
using global::Ordering.API.Infrastructure.Middlewares;
|
using global::Ordering.API.Infrastructure.Middlewares;
|
||||||
using Infrastructure;
|
using Infrastructure;
|
||||||
using Infrastructure.Auth;
|
using Infrastructure.Auth;
|
||||||
@ -30,7 +28,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using global::Ordering.API.Application.IntegrationEvents.EventHandling;
|
|
||||||
|
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
@ -169,17 +166,6 @@
|
|||||||
{
|
{
|
||||||
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
||||||
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
||||||
|
|
||||||
services.AddTransient<IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>>();
|
|
||||||
services.AddTransient<IIntegrationEventHandler<ConfirmGracePeriodCommand>, OrderProcessSaga>();
|
|
||||||
services.AddTransient<IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>,
|
|
||||||
OrderStockConfirmedIntegrationEventHandler>();
|
|
||||||
services.AddTransient<IIntegrationEventHandler<OrderStockNotConfirmedIntegrationEvent>,
|
|
||||||
OrderStockNotConfirmedIntegrationEventHandler>();
|
|
||||||
services.AddTransient<IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>,
|
|
||||||
OrderPaymentFailedIntegrationEventHandler>();
|
|
||||||
services.AddTransient<IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>,
|
|
||||||
OrderPaymentSuccededIntegrationEventHandler>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ConfigureEventBus(IApplicationBuilder app)
|
private void ConfigureEventBus(IApplicationBuilder app)
|
||||||
@ -187,9 +173,9 @@
|
|||||||
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
||||||
|
|
||||||
eventBus.Subscribe<UserCheckoutAcceptedIntegrationEvent, IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>>();
|
eventBus.Subscribe<UserCheckoutAcceptedIntegrationEvent, IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>>();
|
||||||
eventBus.Subscribe<ConfirmGracePeriodCommand, IIntegrationEventHandler<ConfirmGracePeriodCommand>>();
|
eventBus.Subscribe<GracePeriodConfirmedIntegrationEvent, IIntegrationEventHandler<GracePeriodConfirmedIntegrationEvent>>();
|
||||||
eventBus.Subscribe<OrderStockConfirmedIntegrationEvent, IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>>();
|
eventBus.Subscribe<OrderStockConfirmedIntegrationEvent, IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>>();
|
||||||
eventBus.Subscribe<OrderStockNotConfirmedIntegrationEvent, IIntegrationEventHandler<OrderStockNotConfirmedIntegrationEvent>>();
|
eventBus.Subscribe<OrderStockRejectedIntegrationEvent, IIntegrationEventHandler<OrderStockRejectedIntegrationEvent>>();
|
||||||
eventBus.Subscribe<OrderPaymentFailedIntegrationEvent, IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>>();
|
eventBus.Subscribe<OrderPaymentFailedIntegrationEvent, IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>>();
|
||||||
eventBus.Subscribe<OrderPaymentSuccededIntegrationEvent, IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>>();
|
eventBus.Subscribe<OrderPaymentSuccededIntegrationEvent, IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>>();
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,8 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
|||||||
|
|
||||||
public String ZipCode { get; private set; }
|
public String ZipCode { get; private set; }
|
||||||
|
|
||||||
|
private Address() { }
|
||||||
|
|
||||||
public Address(string street, string city, string state, string country, string zipcode)
|
public Address(string street, string city, string state, string country, string zipcode)
|
||||||
{
|
{
|
||||||
Street = street;
|
Street = street;
|
||||||
|
@ -13,7 +13,5 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
|||||||
void Update(Order order);
|
void Update(Order order);
|
||||||
|
|
||||||
Task<Order> GetAsync(int orderId);
|
Task<Order> GetAsync(int orderId);
|
||||||
|
|
||||||
Task<Order> GetWithDependenciesAsync(int orderId);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,14 +94,12 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
|||||||
_buyerId = id;
|
_buyerId = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Status Changes
|
|
||||||
|
|
||||||
public void SetAwaitingValidationStatus()
|
public void SetAwaitingValidationStatus()
|
||||||
{
|
{
|
||||||
if (_orderStatusId != OrderStatus.Submited.Id &&
|
if (_orderStatusId == OrderStatus.Cancelled.Id ||
|
||||||
_orderStatusId != OrderStatus.Cancelled.Id)
|
_orderStatusId != OrderStatus.Submited.Id)
|
||||||
{
|
{
|
||||||
StatusChangeException();
|
StatusChangeException(OrderStatus.AwaitingValidation);
|
||||||
}
|
}
|
||||||
|
|
||||||
AddDomainEvent(new OrderStatusChangedToAwaitingValidationDomainEvent(Id, _orderItems));
|
AddDomainEvent(new OrderStatusChangedToAwaitingValidationDomainEvent(Id, _orderItems));
|
||||||
@ -109,38 +107,24 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
|||||||
_orderStatusId = OrderStatus.AwaitingValidation.Id;
|
_orderStatusId = OrderStatus.AwaitingValidation.Id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetStockConfirmedStatus(IEnumerable<int> orderStockNotConfirmedItems = null)
|
public void SetStockConfirmedStatus()
|
||||||
{
|
{
|
||||||
if (_orderStatusId != OrderStatus.AwaitingValidation.Id)
|
if (_orderStatusId != OrderStatus.AwaitingValidation.Id)
|
||||||
{
|
{
|
||||||
StatusChangeException();
|
StatusChangeException(OrderStatus.StockConfirmed);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (orderStockNotConfirmedItems is null)
|
AddDomainEvent(new OrderStatusChangedToStockConfirmedDomainEvent(Id));
|
||||||
{
|
|
||||||
AddDomainEvent(new OrderStatusChangedToStockConfirmedDomainEvent(Id));
|
|
||||||
|
|
||||||
_orderStatusId = OrderStatus.StockConfirmed.Id;
|
_orderStatusId = OrderStatus.StockConfirmed.Id;
|
||||||
_description = "All the items were confirmed with available stock.";
|
_description = "All the items were confirmed with available stock.";
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_orderStatusId = OrderStatus.Cancelled.Id;
|
|
||||||
|
|
||||||
var itemsStockNotConfirmedProductNames = OrderItems
|
|
||||||
.Where(c => orderStockNotConfirmedItems.Contains(c.ProductId))
|
|
||||||
.Select(c => c.GetOrderItemProductName());
|
|
||||||
|
|
||||||
var itemsStockNotConfirmedDescription = string.Join(", ", itemsStockNotConfirmedProductNames);
|
|
||||||
_description = $"The product items don't have stock: ({itemsStockNotConfirmedDescription}).";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetPaidStatus()
|
public void SetPaidStatus()
|
||||||
{
|
{
|
||||||
if (_orderStatusId != OrderStatus.StockConfirmed.Id)
|
if (_orderStatusId != OrderStatus.StockConfirmed.Id)
|
||||||
{
|
{
|
||||||
StatusChangeException();
|
StatusChangeException(OrderStatus.Paid);
|
||||||
}
|
}
|
||||||
|
|
||||||
AddDomainEvent(new OrderStatusChangedToPaidDomainEvent(Id, OrderItems));
|
AddDomainEvent(new OrderStatusChangedToPaidDomainEvent(Id, OrderItems));
|
||||||
@ -153,40 +137,41 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
|||||||
{
|
{
|
||||||
if (_orderStatusId != OrderStatus.Paid.Id)
|
if (_orderStatusId != OrderStatus.Paid.Id)
|
||||||
{
|
{
|
||||||
StatusChangeException();
|
StatusChangeException(OrderStatus.Shipped);
|
||||||
}
|
}
|
||||||
|
|
||||||
_orderStatusId = OrderStatus.Shipped.Id;
|
_orderStatusId = OrderStatus.Shipped.Id;
|
||||||
_description = "";
|
_description = "The order was shipped.";
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetCancelledStatus()
|
public void SetCancelledStatus()
|
||||||
{
|
{
|
||||||
if (_orderStatusId == OrderStatus.Submited.Id)
|
if (_orderStatusId == OrderStatus.Paid.Id ||
|
||||||
|
_orderStatusId == OrderStatus.Shipped.Id)
|
||||||
{
|
{
|
||||||
_description = "The order was cancelled before the grace period was confirmed.";
|
StatusChangeException(OrderStatus.Cancelled);
|
||||||
}
|
|
||||||
else if (_orderStatusId == OrderStatus.AwaitingValidation.Id)
|
|
||||||
{
|
|
||||||
_description = "The order was cancelled before to check the order stock items.";
|
|
||||||
}
|
|
||||||
else if (_orderStatusId == OrderStatus.StockConfirmed.Id)
|
|
||||||
{
|
|
||||||
_description = "The order was cancelled before to pay the order.";
|
|
||||||
}
|
|
||||||
else if (_orderStatusId == OrderStatus.Paid.Id)
|
|
||||||
{
|
|
||||||
_description = "The order was cancelled before to ship the order.";
|
|
||||||
}
|
|
||||||
else if(_orderStatusId == OrderStatus.Shipped.Id)
|
|
||||||
{
|
|
||||||
throw new OrderingDomainException("Not possible to change order status. Reason: cannot cancel order it is already shipped.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_orderStatusId = OrderStatus.Cancelled.Id;
|
_orderStatusId = OrderStatus.Cancelled.Id;
|
||||||
|
_description = $"The order was cancelled.";
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
public void SetCancelledStatusWhenStockIsRejected(IEnumerable<int> orderStockNotConfirmedItems)
|
||||||
|
{
|
||||||
|
if (_orderStatusId != OrderStatus.AwaitingValidation.Id)
|
||||||
|
{
|
||||||
|
StatusChangeException(OrderStatus.Cancelled);
|
||||||
|
}
|
||||||
|
|
||||||
|
_orderStatusId = OrderStatus.Cancelled.Id;
|
||||||
|
|
||||||
|
var itemsStockNotConfirmedProductNames = OrderItems
|
||||||
|
.Where(c => orderStockNotConfirmedItems.Contains(c.ProductId))
|
||||||
|
.Select(c => c.GetOrderItemProductName());
|
||||||
|
|
||||||
|
var itemsStockNotConfirmedDescription = string.Join(", ", itemsStockNotConfirmedProductNames);
|
||||||
|
_description = $"The product items don't have stock: ({itemsStockNotConfirmedDescription}).";
|
||||||
|
}
|
||||||
|
|
||||||
private void AddOrderStartedDomainEvent(string userId, int cardTypeId, string cardNumber,
|
private void AddOrderStartedDomainEvent(string userId, int cardTypeId, string cardNumber,
|
||||||
string cardSecurityNumber, string cardHolderName, DateTime cardExpiration)
|
string cardSecurityNumber, string cardHolderName, DateTime cardExpiration)
|
||||||
@ -198,9 +183,9 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
|||||||
this.AddDomainEvent(orderStartedDomainEvent);
|
this.AddDomainEvent(orderStartedDomainEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void StatusChangeException()
|
private void StatusChangeException(OrderStatus orderStatusToChange)
|
||||||
{
|
{
|
||||||
throw new OrderingDomainException("Not able to process order event. Reason: no valid order status change");
|
throw new OrderingDomainException($"Not possible to change order status from {OrderStatus.Name} to {orderStatusToChange.Name}.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,15 +33,18 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositor
|
|||||||
|
|
||||||
public async Task<Order> GetAsync(int orderId)
|
public async Task<Order> GetAsync(int orderId)
|
||||||
{
|
{
|
||||||
return await _context.Orders.FindAsync(orderId);
|
var order = await _context.Orders.FindAsync(orderId);
|
||||||
}
|
if (order != null)
|
||||||
|
{
|
||||||
|
await _context.Entry(order)
|
||||||
|
.Collection(i => i.OrderItems).LoadAsync();
|
||||||
|
await _context.Entry(order)
|
||||||
|
.Reference(i => i.OrderStatus).LoadAsync();
|
||||||
|
await _context.Entry(order)
|
||||||
|
.Reference(i => i.Address).LoadAsync();
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<Order> GetWithDependenciesAsync(int orderId)
|
return order;
|
||||||
{
|
|
||||||
return await _context.Orders
|
|
||||||
.Include(c => c.OrderStatus)
|
|
||||||
.Include(c => c.OrderItems)
|
|
||||||
.SingleAsync(c => c.Id == orderId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update(Order order)
|
public void Update(Order order)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user