Move UserCheckoutAcceptedIntegrationEvent out of the order saga
This commit is contained in:
parent
7cbd77bc7a
commit
276de11bec
@ -3,6 +3,7 @@ using MediatR;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Runtime.Serialization;
|
using System.Runtime.Serialization;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
using Ordering.API.Application.Models;
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
||||||
{
|
{
|
||||||
@ -52,12 +53,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
|||||||
[DataMember]
|
[DataMember]
|
||||||
public int CardTypeId { get; private set; }
|
public int CardTypeId { get; private set; }
|
||||||
|
|
||||||
[DataMember]
|
|
||||||
public int PaymentId { get; private set; }
|
|
||||||
|
|
||||||
[DataMember]
|
|
||||||
public int BuyerId { get; private set; }
|
|
||||||
|
|
||||||
[DataMember]
|
[DataMember]
|
||||||
public IEnumerable<OrderItemDTO> OrderItems => _orderItems;
|
public IEnumerable<OrderItemDTO> OrderItems => _orderItems;
|
||||||
|
|
||||||
@ -66,11 +61,11 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
|||||||
_orderItems = new List<OrderItemDTO>();
|
_orderItems = new List<OrderItemDTO>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public CreateOrderCommand(List<OrderItemDTO> orderItems, string city, string street, string state, string country, string zipcode,
|
public CreateOrderCommand(List<BasketItem> basketItems, string city, string street, string state, string country, string zipcode,
|
||||||
string cardNumber, string cardHolderName, DateTime cardExpiration,
|
string cardNumber, string cardHolderName, DateTime cardExpiration,
|
||||||
string cardSecurityNumber, int cardTypeId, int paymentId, int buyerId) : this()
|
string cardSecurityNumber, int cardTypeId) : this()
|
||||||
{
|
{
|
||||||
_orderItems = orderItems;
|
_orderItems = MapToOrderItems(basketItems);
|
||||||
City = city;
|
City = city;
|
||||||
Street = street;
|
Street = street;
|
||||||
State = state;
|
State = state;
|
||||||
@ -82,8 +77,21 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
|||||||
CardSecurityNumber = cardSecurityNumber;
|
CardSecurityNumber = cardSecurityNumber;
|
||||||
CardTypeId = cardTypeId;
|
CardTypeId = cardTypeId;
|
||||||
CardExpiration = cardExpiration;
|
CardExpiration = cardExpiration;
|
||||||
PaymentId = paymentId;
|
}
|
||||||
BuyerId = buyerId;
|
|
||||||
|
private List<OrderItemDTO> MapToOrderItems(List<BasketItem> basketItems)
|
||||||
|
{
|
||||||
|
var result = new List<OrderItemDTO>();
|
||||||
|
basketItems.ForEach((item) => {
|
||||||
|
result.Add(new OrderItemDTO() {
|
||||||
|
ProductId = int.TryParse(item.Id, out int id) ? id : -1,
|
||||||
|
ProductName = item.ProductName,
|
||||||
|
PictureUrl = item.PictureUrl,
|
||||||
|
UnitPrice = item.UnitPrice,
|
||||||
|
Units = item.Quantity
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class OrderItemDTO
|
public class OrderItemDTO
|
||||||
|
@ -43,7 +43,7 @@
|
|||||||
// make sure that consistency is preserved across the whole aggregate
|
// make sure that consistency is preserved across the whole aggregate
|
||||||
var address = new Address(message.Street, message.City, message.State, message.Country, message.ZipCode);
|
var address = new Address(message.Street, message.City, message.State, message.Country, message.ZipCode);
|
||||||
var order = new Order(address , message.CardTypeId, message.CardNumber, message.CardSecurityNumber, message.CardHolderName, message.CardExpiration);
|
var order = new Order(address , message.CardTypeId, message.CardNumber, message.CardSecurityNumber, message.CardHolderName, message.CardExpiration);
|
||||||
|
order.SetOrderStatusId(OrderStatus.Submited.Id);
|
||||||
foreach (var item in message.OrderItems)
|
foreach (var item in message.OrderItems)
|
||||||
{
|
{
|
||||||
order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units);
|
order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units);
|
||||||
|
@ -0,0 +1,52 @@
|
|||||||
|
using MediatR;
|
||||||
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Ordering.API.Application.IntegrationEvents.Events;
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
||||||
|
{
|
||||||
|
public class UserCheckoutAcceptedIntegrationEventHandler : IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>
|
||||||
|
{
|
||||||
|
private readonly IMediator _mediator;
|
||||||
|
private readonly ILoggerFactory _logger;
|
||||||
|
|
||||||
|
public UserCheckoutAcceptedIntegrationEventHandler(IMediator mediator,
|
||||||
|
IOrderingIntegrationEventService orderingIntegrationEventService,
|
||||||
|
ILoggerFactory logger)
|
||||||
|
{
|
||||||
|
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Integration event handler which starts the create order process
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="eventMsg">
|
||||||
|
/// Integration event message which is sent by the
|
||||||
|
/// basket.api once it has successfully process the
|
||||||
|
/// order items.
|
||||||
|
/// </param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task Handle(UserCheckoutAcceptedIntegrationEvent eventMsg)
|
||||||
|
{
|
||||||
|
var result = false;
|
||||||
|
if (eventMsg.RequestId != Guid.Empty)
|
||||||
|
{
|
||||||
|
var createOrderCommand = new CreateOrderCommand(eventMsg.Basket.Items, eventMsg.City, eventMsg.Street,
|
||||||
|
eventMsg.State, eventMsg.Country, eventMsg.ZipCode,
|
||||||
|
eventMsg.CardNumber, eventMsg.CardHolderName, eventMsg.CardExpiration,
|
||||||
|
eventMsg.CardSecurityNumber, eventMsg.CardTypeId);
|
||||||
|
|
||||||
|
var requestCreateOrder = new IdentifiedCommand<CreateOrderCommand, bool>(createOrderCommand, eventMsg.RequestId);
|
||||||
|
result = await _mediator.SendAsync(requestCreateOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.CreateLogger(nameof(UserCheckoutAcceptedIntegrationEventHandler))
|
||||||
|
.LogTrace(result ? $"UserCheckoutAccepted integration event has been received and a create new order process is started with requestId: {eventMsg.RequestId}" :
|
||||||
|
$"UserCheckoutAccepted integration event has been received but a new order process has failed with requestId: {eventMsg.RequestId}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -28,7 +28,6 @@ namespace Ordering.API.Application.Sagas
|
|||||||
/// with the validations.
|
/// with the validations.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class OrderProcessSaga : Saga<Order>,
|
public class OrderProcessSaga : Saga<Order>,
|
||||||
IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>,
|
|
||||||
IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>,
|
IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>,
|
||||||
IAsyncRequestHandler<CancelOrderCommand, bool>
|
IAsyncRequestHandler<CancelOrderCommand, bool>
|
||||||
{
|
{
|
||||||
@ -44,29 +43,7 @@ namespace Ordering.API.Application.Sagas
|
|||||||
_dbContextFactory = dbContextFactory;
|
_dbContextFactory = dbContextFactory;
|
||||||
_mediator = mediator;
|
_mediator = mediator;
|
||||||
_orderingIntegrationEventService = orderingIntegrationEventService;
|
_orderingIntegrationEventService = orderingIntegrationEventService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Command handler which starts the create order process
|
|
||||||
/// and initializes the saga
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="command">
|
|
||||||
/// Integration command message which is sent by the
|
|
||||||
/// basket.api once it has successfully process the
|
|
||||||
/// order items.
|
|
||||||
/// </param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public async Task Handle(UserCheckoutAcceptedIntegrationEvent command)
|
|
||||||
{
|
|
||||||
|
|
||||||
var commanda = command;
|
|
||||||
|
|
||||||
// TODO: This handler should change to Integration command handler type once command bus is implemented
|
|
||||||
|
|
||||||
// TODO: Send createOrder Command
|
|
||||||
|
|
||||||
// TODO: Set saga timeout
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Command handler which confirms that the grace period
|
/// Command handler which confirms that the grace period
|
||||||
|
@ -80,7 +80,6 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Application\IntegrationCommands\CommandHandlers\" />
|
<Folder Include="Application\IntegrationCommands\CommandHandlers\" />
|
||||||
<Folder Include="Application\IntegrationEvents\EventHandling\" />
|
|
||||||
<Folder Include="Infrastructure\IntegrationEventMigrations\" />
|
<Folder Include="Infrastructure\IntegrationEventMigrations\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -31,6 +31,7 @@
|
|||||||
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
|
||||||
{
|
{
|
||||||
@ -127,7 +128,7 @@
|
|||||||
|
|
||||||
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
||||||
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
||||||
//services.AddTransient<UserCheckoutAcceptedIntegrationEventHandler>();
|
services.AddTransient<IIntegrationEventHandler, UserCheckoutAcceptedIntegrationEventHandler>();
|
||||||
services.AddTransient<IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>, OrderProcessSaga>();
|
services.AddTransient<IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>, OrderProcessSaga>();
|
||||||
services.AddTransient<OrderStockConfirmedIntegrationEventHandler>();
|
services.AddTransient<OrderStockConfirmedIntegrationEventHandler>();
|
||||||
services.AddTransient<OrderStockNotConfirmedIntegrationEventHandler>();
|
services.AddTransient<OrderStockNotConfirmedIntegrationEventHandler>();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user