Skip to content

Commit 2b202bd

Browse files
committed
SaveChangesAsync() from Integration events
1 parent 888dca0 commit 2b202bd

6 files changed

Lines changed: 18 additions & 14 deletions

File tree

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public async Task Handle(OrderPaymentFailedIntegrationEvent @event)
2020
var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
2121

2222
orderToUpdate.SetCancelledStatus();
23+
24+
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
2325
}
2426
}
2527
}

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public async Task Handle(OrderPaymentSuccededIntegrationEvent @event)
2020
var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
2121

2222
orderToUpdate.SetPaidStatus();
23+
24+
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
2325
}
2426
}
2527
}

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public async Task Handle(OrderStockConfirmedIntegrationEvent @event)
2020
var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
2121

2222
orderToUpdate.SetStockConfirmedStatus();
23+
24+
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
2325
}
2426
}
2527
}

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public async Task Handle(OrderStockNotConfirmedIntegrationEvent @event)
2626
.Select(c => c.ProductId);
2727

2828
orderToUpdate.SetStockConfirmedStatus(orderStockNotConfirmedItems);
29+
30+
await _orderRepository.UnitOfWork.SaveChangesAsync();
2931
}
3032
}
3133
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,14 @@ public OrderProcessSaga(
4343
/// period has completed.
4444
/// </param>
4545
/// <returns></returns>
46-
public async Task Handle(ConfirmGracePeriodCommandMsg @event)
46+
public async Task Handle(ConfirmGracePeriodCommandMsg command)
4747
{
48-
var orderSaga = FindSagaById(@event.OrderId);
48+
var orderSaga = FindSagaById(command.OrderId);
4949
CheckValidSagaId(orderSaga);
5050

5151
if (orderSaga.OrderStatus != OrderStatus.Cancelled)
5252
{
5353
orderSaga.SetAwaitingValidationStatus();
54-
5554
await SaveChangesAsync();
5655
}
5756
}

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class Order
3030
// but only through the method OrderAggrergateRoot.AddOrderItem() which includes behaviour.
3131
private readonly List<OrderItem> _orderItems;
3232

33-
public IEnumerable<OrderItem> OrderItems => _orderItems.AsReadOnly();
33+
public IReadOnlyList<OrderItem> OrderItems => _orderItems;
3434
// Using List<>.AsReadOnly()
3535
// This will create a read only wrapper around the private list so is protected against "external updates".
3636
// It's much cheaper than .ToList() because it will not have to copy all items in a new collection. (Just one heap alloc for the wrapper instance)
@@ -107,9 +107,9 @@ public void SetAwaitingValidationStatus()
107107
StatusChangeException();
108108
}
109109

110-
_orderStatusId = OrderStatus.AwaitingValidation.Id;
110+
AddDomainEvent(new OrderStatusChangedToAwaitingValidationDomainEvent(Id, _orderItems));
111111

112-
AddDomainEvent(new OrderStatusChangedToAwaitingValidationDomainEvent(Id, OrderItems));
112+
_orderStatusId = OrderStatus.AwaitingValidation.Id;
113113
}
114114

115115
public void SetStockConfirmedStatus(IEnumerable<int> orderStockNotConfirmedItems = null)
@@ -121,15 +121,14 @@ public void SetStockConfirmedStatus(IEnumerable<int> orderStockNotConfirmedItems
121121

122122
if (orderStockNotConfirmedItems is null)
123123
{
124-
OrderStatus = OrderStatus.StockConfirmed;
124+
AddDomainEvent(new OrderStatusChangedToStockConfirmedDomainEvent(Id));
125125

126+
_orderStatusId = OrderStatus.StockConfirmed.Id;
126127
_description = "All the items were confirmed with available stock.";
127-
128-
AddDomainEvent(new OrderStatusChangedToStockConfirmedDomainEvent(Id));
129128
}
130129
else
131130
{
132-
OrderStatus = OrderStatus.Cancelled;
131+
_orderStatusId = OrderStatus.Cancelled.Id;
133132

134133
var itemsStockNotConfirmedProductNames = OrderItems
135134
.Where(c => orderStockNotConfirmedItems.Contains(c.ProductId))
@@ -147,10 +146,10 @@ public void SetPaidStatus()
147146
StatusChangeException();
148147
}
149148

149+
AddDomainEvent(new OrderStatusChangedToPaidDomainEvent(Id, OrderItems));
150+
150151
_orderStatusId = OrderStatus.Paid.Id;
151152
_description = "The payment was performed at a simulated \"American Bank checking bank account endinf on XX35071\"";
152-
153-
AddDomainEvent(new OrderStatusChangedToPaidDomainEvent(Id, OrderItems));
154153
}
155154

156155
public void SetShippedStatus()
@@ -162,8 +161,6 @@ public void SetShippedStatus()
162161

163162
_orderStatusId = OrderStatus.Shipped.Id;
164163
_description = "";
165-
166-
//Call Domain Event
167164
}
168165

169166
public void SetCancelledStatus()

0 commit comments

Comments
 (0)