diff --git a/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommand.cs b/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommand.cs index 950c4bdc5..d83293308 100644 --- a/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommand.cs +++ b/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommand.cs @@ -3,6 +3,7 @@ using MediatR; using System.Collections.Generic; using System.Runtime.Serialization; using System.Collections; +using Ordering.API.Application.Models; namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands { @@ -52,12 +53,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands [DataMember] public int CardTypeId { get; private set; } - [DataMember] - public int PaymentId { get; private set; } - - [DataMember] - public int BuyerId { get; private set; } - [DataMember] public IEnumerable OrderItems => _orderItems; @@ -66,11 +61,11 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands _orderItems = new List(); } - public CreateOrderCommand(List orderItems, string city, string street, string state, string country, string zipcode, + public CreateOrderCommand(List basketItems, string city, string street, string state, string country, string zipcode, 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; Street = street; State = state; @@ -82,8 +77,21 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands CardSecurityNumber = cardSecurityNumber; CardTypeId = cardTypeId; CardExpiration = cardExpiration; - PaymentId = paymentId; - BuyerId = buyerId; + } + + private List MapToOrderItems(List basketItems) + { + var result = new List(); + 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 diff --git a/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs b/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs index 2a3e8b8c0..0cc31c813 100644 --- a/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs @@ -43,7 +43,7 @@ // 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 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) { order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units); diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs new file mode 100644 index 000000000..34169d152 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs @@ -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 + { + 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)); + } + + /// + /// Integration event handler which starts the create order process + /// + /// + /// Integration event message which is sent by the + /// basket.api once it has successfully process the + /// order items. + /// + /// + 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, 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}"); + } + } +} \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs index 3a35d3f85..cdc75a589 100644 --- a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs +++ b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs @@ -28,7 +28,6 @@ namespace Ordering.API.Application.Sagas /// with the validations. /// public class OrderProcessSaga : Saga, - IIntegrationEventHandler, IIntegrationEventHandler, IAsyncRequestHandler { @@ -44,29 +43,7 @@ namespace Ordering.API.Application.Sagas _dbContextFactory = dbContextFactory; _mediator = mediator; _orderingIntegrationEventService = orderingIntegrationEventService; - } - - /// - /// Command handler which starts the create order process - /// and initializes the saga - /// - /// - /// Integration command message which is sent by the - /// basket.api once it has successfully process the - /// order items. - /// - /// - 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 - } + } /// /// Command handler which confirms that the grace period diff --git a/src/Services/Ordering/Ordering.API/Ordering.API.csproj b/src/Services/Ordering/Ordering.API/Ordering.API.csproj index 0f2af6382..d5ef524bb 100644 --- a/src/Services/Ordering/Ordering.API/Ordering.API.csproj +++ b/src/Services/Ordering/Ordering.API/Ordering.API.csproj @@ -80,7 +80,6 @@ - diff --git a/src/Services/Ordering/Ordering.API/Startup.cs b/src/Services/Ordering/Ordering.API/Startup.cs index 2ba5e69f3..315777e3b 100644 --- a/src/Services/Ordering/Ordering.API/Startup.cs +++ b/src/Services/Ordering/Ordering.API/Startup.cs @@ -31,6 +31,7 @@ using System; using System.Data.Common; using System.Reflection; + using global::Ordering.API.Application.IntegrationEvents.EventHandling; public class Startup { @@ -127,7 +128,7 @@ services.AddSingleton(); services.AddSingleton(); - //services.AddTransient(); + services.AddTransient(); services.AddTransient, OrderProcessSaga>(); services.AddTransient(); services.AddTransient();