Browse Source

SaveChangesAsync() from Integration events

pull/809/head
Christian Arenas 7 years ago
parent
commit
2b202bdc6c
6 changed files with 18 additions and 14 deletions
  1. +2
    -0
      src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs
  2. +2
    -0
      src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs
  3. +2
    -0
      src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs
  4. +2
    -0
      src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs
  5. +2
    -3
      src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs
  6. +8
    -11
      src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs

+ 2
- 0
src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs View File

@ -20,6 +20,8 @@
var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
orderToUpdate.SetCancelledStatus(); orderToUpdate.SetCancelledStatus();
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
} }
} }
} }

+ 2
- 0
src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs View File

@ -20,6 +20,8 @@
var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
orderToUpdate.SetPaidStatus(); orderToUpdate.SetPaidStatus();
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
} }
} }
} }

+ 2
- 0
src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs View File

@ -20,6 +20,8 @@
var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
orderToUpdate.SetStockConfirmedStatus(); orderToUpdate.SetStockConfirmedStatus();
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
} }
} }
} }

+ 2
- 0
src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs View File

@ -26,6 +26,8 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling
.Select(c => c.ProductId); .Select(c => c.ProductId);
orderToUpdate.SetStockConfirmedStatus(orderStockNotConfirmedItems); orderToUpdate.SetStockConfirmedStatus(orderStockNotConfirmedItems);
await _orderRepository.UnitOfWork.SaveChangesAsync();
} }
} }
} }

+ 2
- 3
src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs View File

@ -43,15 +43,14 @@ namespace Ordering.API.Application.Sagas
/// period has completed. /// period has completed.
/// </param> /// </param>
/// <returns></returns> /// <returns></returns>
public async Task Handle(ConfirmGracePeriodCommandMsg @event)
public async Task Handle(ConfirmGracePeriodCommandMsg command)
{ {
var orderSaga = FindSagaById(@event.OrderId);
var orderSaga = FindSagaById(command.OrderId);
CheckValidSagaId(orderSaga); CheckValidSagaId(orderSaga);
if (orderSaga.OrderStatus != OrderStatus.Cancelled) if (orderSaga.OrderStatus != OrderStatus.Cancelled)
{ {
orderSaga.SetAwaitingValidationStatus(); orderSaga.SetAwaitingValidationStatus();
await SaveChangesAsync(); await SaveChangesAsync();
} }
} }


+ 8
- 11
src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs View File

@ -30,7 +30,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
// but only through the method OrderAggrergateRoot.AddOrderItem() which includes behaviour. // but only through the method OrderAggrergateRoot.AddOrderItem() which includes behaviour.
private readonly List<OrderItem> _orderItems; private readonly List<OrderItem> _orderItems;
public IEnumerable<OrderItem> OrderItems => _orderItems.AsReadOnly();
public IReadOnlyList<OrderItem> OrderItems => _orderItems;
// Using List<>.AsReadOnly() // Using List<>.AsReadOnly()
// This will create a read only wrapper around the private list so is protected against "external updates". // This will create a read only wrapper around the private list so is protected against "external updates".
// 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) // 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 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
StatusChangeException(); StatusChangeException();
} }
_orderStatusId = OrderStatus.AwaitingValidation.Id;
AddDomainEvent(new OrderStatusChangedToAwaitingValidationDomainEvent(Id, _orderItems));
AddDomainEvent(new OrderStatusChangedToAwaitingValidationDomainEvent(Id, OrderItems));
_orderStatusId = OrderStatus.AwaitingValidation.Id;
} }
public void SetStockConfirmedStatus(IEnumerable<int> orderStockNotConfirmedItems = null) public void SetStockConfirmedStatus(IEnumerable<int> orderStockNotConfirmedItems = null)
@ -121,15 +121,14 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
if (orderStockNotConfirmedItems is null) if (orderStockNotConfirmedItems is null)
{ {
OrderStatus = OrderStatus.StockConfirmed;
AddDomainEvent(new OrderStatusChangedToStockConfirmedDomainEvent(Id));
_orderStatusId = OrderStatus.StockConfirmed.Id;
_description = "All the items were confirmed with available stock."; _description = "All the items were confirmed with available stock.";
AddDomainEvent(new OrderStatusChangedToStockConfirmedDomainEvent(Id));
} }
else else
{ {
OrderStatus = OrderStatus.Cancelled;
_orderStatusId = OrderStatus.Cancelled.Id;
var itemsStockNotConfirmedProductNames = OrderItems var itemsStockNotConfirmedProductNames = OrderItems
.Where(c => orderStockNotConfirmedItems.Contains(c.ProductId)) .Where(c => orderStockNotConfirmedItems.Contains(c.ProductId))
@ -147,10 +146,10 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
StatusChangeException(); StatusChangeException();
} }
AddDomainEvent(new OrderStatusChangedToPaidDomainEvent(Id, OrderItems));
_orderStatusId = OrderStatus.Paid.Id; _orderStatusId = OrderStatus.Paid.Id;
_description = "The payment was performed at a simulated \"American Bank checking bank account endinf on XX35071\""; _description = "The payment was performed at a simulated \"American Bank checking bank account endinf on XX35071\"";
AddDomainEvent(new OrderStatusChangedToPaidDomainEvent(Id, OrderItems));
} }
public void SetShippedStatus() public void SetShippedStatus()
@ -162,8 +161,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
_orderStatusId = OrderStatus.Shipped.Id; _orderStatusId = OrderStatus.Shipped.Id;
_description = ""; _description = "";
//Call Domain Event
} }
public void SetCancelledStatus() public void SetCancelledStatus()


Loading…
Cancel
Save