Skip to content

Commit 276de11

Browse files
committed
Move UserCheckoutAcceptedIntegrationEvent out of the order saga
1 parent 7cbd77b commit 276de11

6 files changed

Lines changed: 75 additions & 38 deletions

File tree

src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommand.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Runtime.Serialization;
55
using System.Collections;
6+
using Ordering.API.Application.Models;
67

78
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
89
{
@@ -52,12 +53,6 @@ public class CreateOrderCommand
5253
[DataMember]
5354
public int CardTypeId { get; private set; }
5455

55-
[DataMember]
56-
public int PaymentId { get; private set; }
57-
58-
[DataMember]
59-
public int BuyerId { get; private set; }
60-
6156
[DataMember]
6257
public IEnumerable<OrderItemDTO> OrderItems => _orderItems;
6358

@@ -66,11 +61,11 @@ public CreateOrderCommand()
6661
_orderItems = new List<OrderItemDTO>();
6762
}
6863

69-
public CreateOrderCommand(List<OrderItemDTO> orderItems, string city, string street, string state, string country, string zipcode,
64+
public CreateOrderCommand(List<BasketItem> basketItems, string city, string street, string state, string country, string zipcode,
7065
string cardNumber, string cardHolderName, DateTime cardExpiration,
71-
string cardSecurityNumber, int cardTypeId, int paymentId, int buyerId) : this()
66+
string cardSecurityNumber, int cardTypeId) : this()
7267
{
73-
_orderItems = orderItems;
68+
_orderItems = MapToOrderItems(basketItems);
7469
City = city;
7570
Street = street;
7671
State = state;
@@ -82,8 +77,21 @@ public CreateOrderCommand(List<OrderItemDTO> orderItems, string city, string str
8277
CardSecurityNumber = cardSecurityNumber;
8378
CardTypeId = cardTypeId;
8479
CardExpiration = cardExpiration;
85-
PaymentId = paymentId;
86-
BuyerId = buyerId;
80+
}
81+
82+
private List<OrderItemDTO> MapToOrderItems(List<BasketItem> basketItems)
83+
{
84+
var result = new List<OrderItemDTO>();
85+
basketItems.ForEach((item) => {
86+
result.Add(new OrderItemDTO() {
87+
ProductId = int.TryParse(item.Id, out int id) ? id : -1,
88+
ProductName = item.ProductName,
89+
PictureUrl = item.PictureUrl,
90+
UnitPrice = item.UnitPrice,
91+
Units = item.Quantity
92+
});
93+
});
94+
return result;
8795
}
8896

8997
public class OrderItemDTO

src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public async Task<bool> Handle(CreateOrderCommand message)
4343
// make sure that consistency is preserved across the whole aggregate
4444
var address = new Address(message.Street, message.City, message.State, message.Country, message.ZipCode);
4545
var order = new Order(address , message.CardTypeId, message.CardNumber, message.CardSecurityNumber, message.CardHolderName, message.CardExpiration);
46-
46+
order.SetOrderStatusId(OrderStatus.Submited.Id);
4747
foreach (var item in message.OrderItems)
4848
{
4949
order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using MediatR;
2+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
3+
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
4+
using Microsoft.Extensions.Logging;
5+
using Ordering.API.Application.IntegrationEvents.Events;
6+
using System;
7+
using System.Threading.Tasks;
8+
9+
namespace Ordering.API.Application.IntegrationEvents.EventHandling
10+
{
11+
public class UserCheckoutAcceptedIntegrationEventHandler : IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>
12+
{
13+
private readonly IMediator _mediator;
14+
private readonly ILoggerFactory _logger;
15+
16+
public UserCheckoutAcceptedIntegrationEventHandler(IMediator mediator,
17+
IOrderingIntegrationEventService orderingIntegrationEventService,
18+
ILoggerFactory logger)
19+
{
20+
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
21+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
22+
}
23+
24+
/// <summary>
25+
/// Integration event handler which starts the create order process
26+
/// </summary>
27+
/// <param name="eventMsg">
28+
/// Integration event message which is sent by the
29+
/// basket.api once it has successfully process the
30+
/// order items.
31+
/// </param>
32+
/// <returns></returns>
33+
public async Task Handle(UserCheckoutAcceptedIntegrationEvent eventMsg)
34+
{
35+
var result = false;
36+
if (eventMsg.RequestId != Guid.Empty)
37+
{
38+
var createOrderCommand = new CreateOrderCommand(eventMsg.Basket.Items, eventMsg.City, eventMsg.Street,
39+
eventMsg.State, eventMsg.Country, eventMsg.ZipCode,
40+
eventMsg.CardNumber, eventMsg.CardHolderName, eventMsg.CardExpiration,
41+
eventMsg.CardSecurityNumber, eventMsg.CardTypeId);
42+
43+
var requestCreateOrder = new IdentifiedCommand<CreateOrderCommand, bool>(createOrderCommand, eventMsg.RequestId);
44+
result = await _mediator.SendAsync(requestCreateOrder);
45+
}
46+
47+
_logger.CreateLogger(nameof(UserCheckoutAcceptedIntegrationEventHandler))
48+
.LogTrace(result ? $"UserCheckoutAccepted integration event has been received and a create new order process is started with requestId: {eventMsg.RequestId}" :
49+
$"UserCheckoutAccepted integration event has been received but a new order process has failed with requestId: {eventMsg.RequestId}");
50+
}
51+
}
52+
}

src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ namespace Ordering.API.Application.Sagas
2828
/// with the validations.
2929
/// </summary>
3030
public class OrderProcessSaga : Saga<Order>,
31-
IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>,
3231
IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>,
3332
IAsyncRequestHandler<CancelOrderCommand, bool>
3433
{
@@ -44,29 +43,7 @@ public OrderProcessSaga(
4443
_dbContextFactory = dbContextFactory;
4544
_mediator = mediator;
4645
_orderingIntegrationEventService = orderingIntegrationEventService;
47-
}
48-
49-
/// <summary>
50-
/// Command handler which starts the create order process
51-
/// and initializes the saga
52-
/// </summary>
53-
/// <param name="command">
54-
/// Integration command message which is sent by the
55-
/// basket.api once it has successfully process the
56-
/// order items.
57-
/// </param>
58-
/// <returns></returns>
59-
public async Task Handle(UserCheckoutAcceptedIntegrationEvent command)
60-
{
61-
62-
var commanda = command;
63-
64-
// TODO: This handler should change to Integration command handler type once command bus is implemented
65-
66-
// TODO: Send createOrder Command
67-
68-
// TODO: Set saga timeout
69-
}
46+
}
7047

7148
/// <summary>
7249
/// Command handler which confirms that the grace period

src/Services/Ordering/Ordering.API/Ordering.API.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080

8181
<ItemGroup>
8282
<Folder Include="Application\IntegrationCommands\CommandHandlers\" />
83-
<Folder Include="Application\IntegrationEvents\EventHandling\" />
8483
<Folder Include="Infrastructure\IntegrationEventMigrations\" />
8584
</ItemGroup>
8685

src/Services/Ordering/Ordering.API/Startup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
using System;
3232
using System.Data.Common;
3333
using System.Reflection;
34+
using global::Ordering.API.Application.IntegrationEvents.EventHandling;
3435

3536
public class Startup
3637
{
@@ -127,7 +128,7 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
127128

128129
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
129130
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
130-
//services.AddTransient<UserCheckoutAcceptedIntegrationEventHandler>();
131+
services.AddTransient<IIntegrationEventHandler, UserCheckoutAcceptedIntegrationEventHandler>();
131132
services.AddTransient<IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>, OrderProcessSaga>();
132133
services.AddTransient<OrderStockConfirmedIntegrationEventHandler>();
133134
services.AddTransient<OrderStockNotConfirmedIntegrationEventHandler>();

0 commit comments

Comments
 (0)