Skip to content

Commit 63cba2c

Browse files
committed
Merge branch 'order-processflow-redesign' of https://github.com/dotnet-architecture/eShopOnContainers into order-processflow-redesign
# Conflicts: # src/Services/SagaManager/SagaManager/Program.cs
2 parents b12e8e3 + f3b6444 commit 63cba2c

14 files changed

Lines changed: 62 additions & 109 deletions

File tree

src/Services/Basket/Basket.API/Startup.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,6 @@ protected virtual void ConfigureAuth(IApplicationBuilder app)
165165

166166
protected virtual void ConfigureEventBus(IApplicationBuilder app)
167167
{
168-
var catalogPriceHandler = app.ApplicationServices
169-
.GetService<IIntegrationEventHandler<ProductPriceChangedIntegrationEvent>>();
170-
171-
var orderStartedHandler = app.ApplicationServices
172-
.GetService<IIntegrationEventHandler<OrderStartedIntegrationEvent>>();
173-
174168
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
175169

176170
eventBus.Subscribe<ProductPriceChangedIntegrationEvent, ProductPriceChangedIntegrationEventHandler>();

src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/DecrementOrderStockCommandMsgHandler.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
using BuildingBlocks.EventBus.Abstractions;
44
using System.Threading.Tasks;
55
using Infrastructure;
6-
using global::Catalog.API.Infrastructure.Exceptions;
7-
using global::Catalog.API.IntegrationEvents;
8-
using Model;
96
using Commands;
107

118
public class DecrementOrderStockCommandMsgHandler : IIntegrationEventHandler<DecrementOrderStockCommandMsg>

src/Services/Catalog/Catalog.API/Startup.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,7 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
121121
return new DefaultRabbitMQPersistentConnection(factory, logger);
122122
});
123123

124-
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
125-
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
126-
services.AddTransient<IIntegrationEventHandler<ConfirmOrderStockCommandMsg>, ConfirmOrderStockCommandMsgHandler>();
127-
services.AddTransient<IIntegrationEventHandler<DecrementOrderStockCommandMsg>, DecrementOrderStockCommandMsgHandler>();
124+
RegisterServiceBus(services);
128125

129126
var container = new ContainerBuilder();
130127
container.Populate(services);
@@ -188,6 +185,18 @@ private void WaitForSqlAvailability(CatalogContext ctx, ILoggerFactory loggerFac
188185
}
189186
}
190187

188+
private void RegisterServiceBus(IServiceCollection services)
189+
{
190+
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
191+
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
192+
193+
services.AddTransient<IIntegrationEventHandler<ConfirmOrderStockCommandMsg>,
194+
ConfirmOrderStockCommandMsgHandler>();
195+
services.AddTransient<IIntegrationEventHandler<DecrementOrderStockCommandMsg>,
196+
DecrementOrderStockCommandMsgHandler>();
197+
198+
}
199+
191200
private void ConfigureEventBus(IApplicationBuilder app)
192201
{
193202
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();

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(message.UserId, address, message.CardTypeId, message.CardNumber, message.CardSecurityNumber, message.CardHolderName, message.CardExpiration);
46-
order.SetSubmitedStatus();
46+
4747
foreach (var item in message.OrderItems)
4848
{
4949
order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units);

src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderGracePeriodConfirmed/OrderStatusChangedToAwaitingValidationDomainEventHandler.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ public OrderStatusChangedToAwaitingValidationDomainEventHandler(
2828

2929
public async Task Handle(OrderStatusChangedToAwaitingValidationDomainEvent orderStatusChangedToAwaitingValidationDomainEvent)
3030
{
31-
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
32-
3331
_logger.CreateLogger(nameof(OrderStatusChangedToAwaitingValidationDomainEvent))
3432
.LogTrace($"Order with Id: {orderStatusChangedToAwaitingValidationDomainEvent.OrderId} has been successfully updated with " +
3533
$"a status order id: {OrderStatus.AwaitingValidation.Id}");

src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderPaid/OrderStatusChangedToPaidDomainEventHandler.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ public OrderStatusChangedToPaidDomainEventHandler(
2828

2929
public async Task Handle(OrderStatusChangedToPaidDomainEvent orderStatusChangedToPaidDomainEvent)
3030
{
31-
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
32-
3331
_logger.CreateLogger(nameof(OrderStatusChangedToPaidDomainEventHandler))
3432
.LogTrace($"Order with Id: {orderStatusChangedToPaidDomainEvent.OrderId} has been successfully updated with " +
3533
$"a status order id: {OrderStatus.Paid.Id}");

src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmed/OrderStatusChangedToStockConfirmedDomainEventHandler.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public OrderStatusChangedToStockConfirmedDomainEventHandler(
2727

2828
public async Task Handle(OrderStatusChangedToStockConfirmedDomainEvent orderStatusChangedToStockConfirmedDomainEvent)
2929
{
30-
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
31-
3230
_logger.CreateLogger(nameof(OrderStatusChangedToStockConfirmedDomainEventHandler))
3331
.LogTrace($"Order with Id: {orderStatusChangedToStockConfirmedDomainEvent.OrderId} has been successfully updated with " +
3432
$"a status order id: {OrderStatus.StockConfirmed.Id}");

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

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,8 @@ public async Task Handle(ConfirmGracePeriodCommandMsg command)
4848
var orderSaga = FindSagaById(command.OrderId);
4949
CheckValidSagaId(orderSaga);
5050

51-
if (orderSaga.OrderStatus != OrderStatus.Cancelled)
52-
{
53-
orderSaga.SetAwaitingValidationStatus();
54-
await SaveChangesAsync();
55-
}
51+
orderSaga.SetAwaitingValidationStatus();
52+
await SaveChangesAsync();
5653
}
5754

5855
/// <summary>
@@ -67,14 +64,9 @@ public async Task<bool> Handle(CancelOrderCommand command)
6764
var orderSaga = FindSagaById(command.OrderNumber);
6865
CheckValidSagaId(orderSaga);
6966

70-
// Not possible to cancel order when
71-
// it has already been shipped
72-
if (orderSaga.GetOrderStatusId() != OrderStatus.Cancelled.Id
73-
|| orderSaga.GetOrderStatusId() != OrderStatus.Shipped.Id)
74-
{
75-
orderSaga.SetCancelledStatus();
76-
result = await SaveChangesAsync();
77-
}
67+
orderSaga.SetCancelledStatus();
68+
result = await SaveChangesAsync();
69+
7870
return result;
7971
}
8072

@@ -90,13 +82,9 @@ public async Task<bool> Handle(ShipOrderCommand command)
9082
var orderSaga = FindSagaById(command.OrderNumber);
9183
CheckValidSagaId(orderSaga);
9284

93-
// Only ship order when
94-
// its status is paid
95-
if (orderSaga.GetOrderStatusId() == OrderStatus.Paid.Id)
96-
{
97-
orderSaga.SetShippedStatus();
98-
result = await SaveChangesAsync();
99-
}
85+
orderSaga.SetShippedStatus();
86+
result = await SaveChangesAsync();
87+
10088
return result;
10189
}
10290

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
using global::Ordering.API.Application.IntegrationEvents.Events;
99
using global::Ordering.API.Application.Sagas;
1010
using global::Ordering.API.Infrastructure.Middlewares;
11-
using global::Ordering.API.Application.IntegrationCommands.Commands;
12-
using global::Ordering.API.Application.Sagas;
1311
using Infrastructure;
1412
using Infrastructure.Auth;
1513
using Infrastructure.AutofacModules;
@@ -126,18 +124,7 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
126124
return new DefaultRabbitMQPersistentConnection(factory, logger);
127125
});
128126

129-
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
130-
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
131-
services.AddTransient<IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>>();
132-
services.AddTransient<IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>, OrderProcessSaga>();
133-
services.AddTransient<IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>,
134-
OrderStockConfirmedIntegrationEventHandler>();
135-
services.AddTransient<IIntegrationEventHandler<OrderStockNotConfirmedIntegrationEvent>,
136-
OrderStockNotConfirmedIntegrationEventHandler>();
137-
services.AddTransient<IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>,
138-
OrderPaymentFailedIntegrationEventHandler>();
139-
services.AddTransient<IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>,
140-
OrderPaymentSuccededIntegrationEventHandler>();
127+
RegisterServiceBus(services);
141128
services.AddOptions();
142129

143130
//configure autofac
@@ -178,6 +165,23 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
178165

179166
}
180167

168+
private void RegisterServiceBus(IServiceCollection services)
169+
{
170+
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
171+
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
172+
173+
services.AddTransient<IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>>();
174+
services.AddTransient<IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>, OrderProcessSaga>();
175+
services.AddTransient<IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>,
176+
OrderStockConfirmedIntegrationEventHandler>();
177+
services.AddTransient<IIntegrationEventHandler<OrderStockNotConfirmedIntegrationEvent>,
178+
OrderStockNotConfirmedIntegrationEventHandler>();
179+
services.AddTransient<IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>,
180+
OrderPaymentFailedIntegrationEventHandler>();
181+
services.AddTransient<IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>,
182+
OrderPaymentSuccededIntegrationEventHandler>();
183+
}
184+
181185
private void ConfigureEventBus(IApplicationBuilder app)
182186
{
183187
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();

src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,11 @@ public void SetBuyerId(int id)
9595
}
9696

9797
#region Status Changes
98-
public void SetSubmitedStatus()
99-
{
100-
_orderStatusId = OrderStatus.Submited.Id;
101-
}
10298

10399
public void SetAwaitingValidationStatus()
104100
{
105-
if (_orderStatusId != OrderStatus.Submited.Id)
101+
if (_orderStatusId != OrderStatus.Submited.Id &&
102+
_orderStatusId != OrderStatus.Cancelled.Id)
106103
{
107104
StatusChangeException();
108105
}
@@ -167,7 +164,7 @@ public void SetCancelledStatus()
167164
{
168165
if (_orderStatusId == OrderStatus.Submited.Id)
169166
{
170-
_description = "The order was cancelled before the grace period was confirm.";
167+
_description = "The order was cancelled before the grace period was confirmed.";
171168
}
172169
else if (_orderStatusId == OrderStatus.AwaitingValidation.Id)
173170
{
@@ -191,11 +188,6 @@ public void SetCancelledStatus()
191188

192189
#endregion
193190

194-
public int GetOrderStatusId()
195-
{
196-
return _orderStatusId;
197-
}
198-
199191
private void AddOrderStartedDomainEvent(string userId, int cardTypeId, string cardNumber,
200192
string cardSecurityNumber, string cardHolderName, DateTime cardExpiration)
201193
{

0 commit comments

Comments
 (0)