|
|
@ -1,5 +1,4 @@ |
|
|
|
using Autofac.Features.OwnedInstances; |
|
|
|
using MediatR; |
|
|
|
using MediatR; |
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; |
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands; |
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; |
|
|
@ -7,15 +6,10 @@ using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; |
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency; |
|
|
|
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 System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Reflection; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Ordering.API.Application.IntegrationEvents; |
|
|
|
using Ordering.API.Application.IntegrationEvents.Events; |
|
|
|
|
|
|
|
namespace Ordering.API.Application.Sagas |
|
|
|
{ |
|
|
@ -29,19 +23,16 @@ namespace Ordering.API.Application.Sagas |
|
|
|
/// </summary>
|
|
|
|
public class OrderProcessSaga : Saga<Order>, |
|
|
|
IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>, |
|
|
|
IAsyncRequestHandler<CancelOrderCommand, bool> |
|
|
|
IAsyncRequestHandler<CancelOrderCommand, bool>, |
|
|
|
IAsyncRequestHandler<ShipOrderCommand, bool> |
|
|
|
{ |
|
|
|
private readonly IMediator _mediator; |
|
|
|
private readonly Func<Owned<OrderingContext>> _dbContextFactory; |
|
|
|
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; |
|
|
|
|
|
|
|
public OrderProcessSaga( |
|
|
|
Func<Owned<OrderingContext>> dbContextFactory, OrderingContext orderingContext, |
|
|
|
IMediator mediator, IOrderingIntegrationEventService orderingIntegrationEventService) |
|
|
|
OrderingContext orderingContext, |
|
|
|
IOrderingIntegrationEventService orderingIntegrationEventService) |
|
|
|
: base(orderingContext) |
|
|
|
{ |
|
|
|
_dbContextFactory = dbContextFactory; |
|
|
|
_mediator = mediator; |
|
|
|
_orderingIntegrationEventService = orderingIntegrationEventService; |
|
|
|
} |
|
|
|
|
|
|
@ -85,12 +76,41 @@ namespace Ordering.API.Application.Sagas |
|
|
|
/// <returns></returns>
|
|
|
|
public async Task<bool> Handle(CancelOrderCommand command) |
|
|
|
{ |
|
|
|
var result = false; |
|
|
|
var orderSaga = FindSagaById(command.OrderNumber); |
|
|
|
CheckValidSagaId(orderSaga); |
|
|
|
|
|
|
|
// Set order status tu cancelled
|
|
|
|
// Not possible to cancel order when
|
|
|
|
// it has already been shipped
|
|
|
|
if (orderSaga.GetOrderStatusId() != OrderStatus.Cancelled.Id |
|
|
|
|| orderSaga.GetOrderStatusId() != OrderStatus.Shipped.Id) |
|
|
|
{ |
|
|
|
orderSaga.SetOrderStatusId(OrderStatus.Cancelled.Id); |
|
|
|
result = await SaveChangesAsync(); |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handler which processes the command when
|
|
|
|
/// administrator executes ship order from app
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="command"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public async Task<bool> Handle(ShipOrderCommand command) |
|
|
|
{ |
|
|
|
var result = false; |
|
|
|
var orderSaga = FindSagaById(command.OrderNumber); |
|
|
|
CheckValidSagaId(orderSaga); |
|
|
|
|
|
|
|
return true; |
|
|
|
// Only ship order when
|
|
|
|
// its status is paid
|
|
|
|
if (orderSaga.GetOrderStatusId() == OrderStatus.Paid.Id) |
|
|
|
{ |
|
|
|
orderSaga.SetOrderStatusId(OrderStatus.Shipped.Id); |
|
|
|
result = await SaveChangesAsync(); |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
private void CheckValidSagaId(Order orderSaga) |
|
|
@ -115,6 +135,18 @@ namespace Ordering.API.Application.Sagas |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public class ShipOrderCommandIdentifiedHandler : IdentifierCommandHandler<ShipOrderCommand, bool> |
|
|
|
{ |
|
|
|
public ShipOrderCommandIdentifiedHandler(IMediator mediator, IRequestManager requestManager) : base(mediator, requestManager) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
protected override bool CreateResultForDuplicateRequest() |
|
|
|
{ |
|
|
|
return true; // Ignore duplicate requests for processing order.
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#endregion
|
|
|
|
} |
|
|
|
} |