diff --git a/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommand.cs b/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommand.cs index 19fa3b818..fc6fd97d0 100644 --- a/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommand.cs +++ b/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommand.cs @@ -88,7 +88,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands var result = new List(); basketItems.ForEach((item) => { result.Add(new OrderItemDTO() { - ProductId = int.TryParse(item.Id, out int id) ? id : -1, + ProductId = int.TryParse(item.ProductId, out int id) ? id : -1, ProductName = item.ProductName, PictureUrl = item.PictureUrl, UnitPrice = item.UnitPrice, diff --git a/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs b/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs index c0d0a0d2b..3ef80a69a 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(message.UserId, address, message.CardTypeId, message.CardNumber, message.CardSecurityNumber, message.CardHolderName, message.CardExpiration); - order.SetOrderStatusId(OrderStatus.Submited.Id); + order.SetSubmitedStatus(); 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/DomainEventHandlers/OrderGracePeriodConfirmed/OrderStatusChangedToAwaitingValidationDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderGracePeriodConfirmed/OrderStatusChangedToAwaitingValidationDomainEventHandler.cs new file mode 100644 index 000000000..d6906705f --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderGracePeriodConfirmed/OrderStatusChangedToAwaitingValidationDomainEventHandler.cs @@ -0,0 +1,46 @@ +namespace Ordering.API.Application.DomainEventHandlers.OrderGracePeriodConfirmed +{ + using MediatR; + using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; + using Microsoft.Extensions.Logging; + using Domain.Events; + using System; + using System.Threading.Tasks; + using Ordering.API.Application.IntegrationCommands.Commands; + using Ordering.API.Application.IntegrationEvents; + using System.Linq; + + public class OrderStatusChangedToAwaitingValidationDomainEventHandler + : IAsyncNotificationHandler + { + private readonly IOrderRepository _orderRepository; + private readonly ILoggerFactory _logger; + private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; + + public OrderStatusChangedToAwaitingValidationDomainEventHandler( + IOrderRepository orderRepository, ILoggerFactory logger, + IOrderingIntegrationEventService orderingIntegrationEventService) + { + _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _orderingIntegrationEventService = orderingIntegrationEventService; + } + + public async Task Handle(OrderStatusChangedToAwaitingValidationDomainEvent orderStatusChangedToAwaitingValidationDomainEvent) + { + await _orderRepository.UnitOfWork.SaveEntitiesAsync(); + + _logger.CreateLogger(nameof(OrderStatusChangedToAwaitingValidationDomainEvent)) + .LogTrace($"Order with Id: {orderStatusChangedToAwaitingValidationDomainEvent.OrderId} has been successfully updated with " + + $"a status order id: {OrderStatus.AwaitingValidation.Id}"); + + var orderStockList = orderStatusChangedToAwaitingValidationDomainEvent.OrderItems + .Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits())); + + var confirmOrderStockEvent = new ConfirmOrderStockCommandMsg(orderStatusChangedToAwaitingValidationDomainEvent.OrderId, + orderStockList); + await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(confirmOrderStockEvent); + await _orderingIntegrationEventService.PublishThroughEventBusAsync(confirmOrderStockEvent); + } + } +} \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderPaid/OrderStatusChangedToPaidDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderPaid/OrderStatusChangedToPaidDomainEventHandler.cs new file mode 100644 index 000000000..1eb4a8953 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderPaid/OrderStatusChangedToPaidDomainEventHandler.cs @@ -0,0 +1,51 @@ +namespace Ordering.API.Application.DomainEventHandlers.OrderPaid +{ + using MediatR; + using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; + using Microsoft.Extensions.Logging; + using Domain.Events; + using System; + using System.Threading.Tasks; + using Ordering.API.Application.IntegrationCommands.Commands; + using Ordering.API.Application.IntegrationEvents; + using System.Linq; + + public class OrderStatusChangedToPaidDomainEventHandler + : IAsyncNotificationHandler + { + private readonly IOrderRepository _orderRepository; + private readonly ILoggerFactory _logger; + private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; + + public OrderStatusChangedToPaidDomainEventHandler( + IOrderRepository orderRepository, ILoggerFactory logger, + IOrderingIntegrationEventService orderingIntegrationEventService) + { + _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _orderingIntegrationEventService = orderingIntegrationEventService; + } + + public async Task Handle(OrderStatusChangedToPaidDomainEvent orderStatusChangedToPaidDomainEvent) + { + await _orderRepository.UnitOfWork.SaveEntitiesAsync(); + + _logger.CreateLogger(nameof(OrderStatusChangedToPaidDomainEventHandler)) + .LogTrace($"Order with Id: {orderStatusChangedToPaidDomainEvent.OrderId} has been successfully updated with " + + $"a status order id: {OrderStatus.Paid.Id}"); + + var orderStockList = orderStatusChangedToPaidDomainEvent.OrderItems + .Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits())); + + var decrementOrderStockCommandMsg = new DecrementOrderStockCommandMsg(orderStatusChangedToPaidDomainEvent.OrderId, + orderStockList); + await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(decrementOrderStockCommandMsg); + await _orderingIntegrationEventService.PublishThroughEventBusAsync(decrementOrderStockCommandMsg); + + //is it necessary get a DecrementOrderStockSuccessIntegrationEvent/DecrementOrderStockFailedIntegrationEvent before to call ShipOrderCommandMsg??? + var shipOrderCommandMsg = new ShipOrderCommandMsg(orderStatusChangedToPaidDomainEvent.OrderId); + await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(shipOrderCommandMsg); + await _orderingIntegrationEventService.PublishThroughEventBusAsync(shipOrderCommandMsg); + } + } +} \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler.cs deleted file mode 100644 index cd99f7d66..000000000 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System.Linq; - -namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent -{ - using MediatR; - using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; - using Microsoft.Extensions.Logging; - using Domain.Events; - using System; - using System.Threading.Tasks; - using Ordering.API.Application.IntegrationCommands.Commands; - using Ordering.API.Application.IntegrationEvents; - - public class OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler - : IAsyncNotificationHandler - { - private readonly IOrderRepository _orderRepository; - private readonly ILoggerFactory _logger; - private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; - - public OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler( - IOrderRepository orderRepository, ILoggerFactory logger, - IOrderingIntegrationEventService orderingIntegrationEventService) - { - _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); - _logger = logger ?? throw new ArgumentNullException(nameof(logger)); - _orderingIntegrationEventService = orderingIntegrationEventService; - } - - public async Task Handle(OrderStockConfirmedDomainEvent orderStockMethodVerifiedDomainEvent) - { - _logger.CreateLogger(nameof(OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler)) - .LogTrace($"Order with Id: {orderStockMethodVerifiedDomainEvent.OrderId} has been successfully updated with " + - $"a status order id: { orderStockMethodVerifiedDomainEvent.OrderStatus.Id }"); - - if (orderStockMethodVerifiedDomainEvent.OrderStatus == OrderStatus.StockValidated) - { - var payOrderCommandMsg = new PayOrderCommandMsg(orderStockMethodVerifiedDomainEvent.OrderId); - await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg); - await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg); - } - } - } -} \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmed/OrderStatusChangedToStockConfirmedDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmed/OrderStatusChangedToStockConfirmedDomainEventHandler.cs new file mode 100644 index 000000000..2de4f5095 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmed/OrderStatusChangedToStockConfirmedDomainEventHandler.cs @@ -0,0 +1,41 @@ +namespace Ordering.API.Application.DomainEventHandlers.OrderStockConfirmed +{ + using MediatR; + using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; + using Microsoft.Extensions.Logging; + using Domain.Events; + using System; + using System.Threading.Tasks; + using Ordering.API.Application.IntegrationCommands.Commands; + using Ordering.API.Application.IntegrationEvents; + + public class OrderStatusChangedToStockConfirmedDomainEventHandler + : IAsyncNotificationHandler + { + private readonly IOrderRepository _orderRepository; + private readonly ILoggerFactory _logger; + private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; + + public OrderStatusChangedToStockConfirmedDomainEventHandler( + IOrderRepository orderRepository, ILoggerFactory logger, + IOrderingIntegrationEventService orderingIntegrationEventService) + { + _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _orderingIntegrationEventService = orderingIntegrationEventService; + } + + public async Task Handle(OrderStatusChangedToStockConfirmedDomainEvent orderStatusChangedToStockConfirmedDomainEvent) + { + await _orderRepository.UnitOfWork.SaveEntitiesAsync(); + + _logger.CreateLogger(nameof(OrderStatusChangedToStockConfirmedDomainEventHandler)) + .LogTrace($"Order with Id: {orderStatusChangedToStockConfirmedDomainEvent.OrderId} has been successfully updated with " + + $"a status order id: {OrderStatus.StockConfirmed.Id}"); + + var payOrderCommandMsg = new PayOrderCommandMsg(orderStatusChangedToStockConfirmedDomainEvent.OrderId); + await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg); + await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg); + } + } +} \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmOrderStockCommandMsg.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmOrderStockCommandMsg.cs index abf93d0ef..6e78598f1 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmOrderStockCommandMsg.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmOrderStockCommandMsg.cs @@ -6,13 +6,13 @@ public class ConfirmOrderStockCommandMsg : IntegrationEvent { public int OrderId { get; } - public IEnumerable OrderStockItem { get; } + public IEnumerable OrderStockItems { get; } public ConfirmOrderStockCommandMsg(int orderId, - IEnumerable orderStockItem) + IEnumerable orderStockItems) { OrderId = orderId; - OrderStockItem = orderStockItem; + OrderStockItems = orderStockItems; } } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/DecrementOrderStockCommandMsg.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/DecrementOrderStockCommandMsg.cs new file mode 100644 index 000000000..e05a20c48 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/DecrementOrderStockCommandMsg.cs @@ -0,0 +1,18 @@ +namespace Ordering.API.Application.IntegrationCommands.Commands +{ + using System.Collections.Generic; + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; + + public class DecrementOrderStockCommandMsg : IntegrationEvent + { + public int OrderId { get; } + public IEnumerable OrderStockItems { get; } + + public DecrementOrderStockCommandMsg(int orderId, + IEnumerable orderStockItems) + { + OrderId = orderId; + OrderStockItems = orderStockItems; + } + } +} \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ShipOrderCommandMsg.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ShipOrderCommandMsg.cs new file mode 100644 index 000000000..5a8695ae9 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ShipOrderCommandMsg.cs @@ -0,0 +1,14 @@ +namespace Ordering.API.Application.IntegrationCommands.Commands +{ + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; + + public class ShipOrderCommandMsg : IntegrationEvent + { + public int OrderId { get; } + + public ShipOrderCommandMsg(int orderId) + { + OrderId = orderId; + } + } +} \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs index 485cf750f..074b2799b 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs @@ -1,14 +1,23 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling { using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; + using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; using Ordering.API.Application.IntegrationEvents.Events; using System.Threading.Tasks; public class OrderPaymentFailedIntegrationEventHandler : IIntegrationEventHandler { + private readonly IOrderRepository _orderRepository; + + public OrderPaymentFailedIntegrationEventHandler(IOrderRepository orderRepository) + { + _orderRepository = orderRepository; + } + public async Task Handle(OrderPaymentFailedIntegrationEvent @event) { + //TODO: Cancel Order } } } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs index 86e3d5482..98ba54b08 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs @@ -1,14 +1,35 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling { using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; + using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; using Ordering.API.Application.IntegrationEvents.Events; + using Ordering.Domain.Exceptions; using System.Threading.Tasks; public class OrderPaymentSuccededIntegrationEventHandler : IIntegrationEventHandler { + private readonly IOrderRepository _orderRepository; + + public OrderPaymentSuccededIntegrationEventHandler(IOrderRepository orderRepository) + { + _orderRepository = orderRepository; + } + public async Task Handle(OrderPaymentSuccededIntegrationEvent @event) { + var order = await _orderRepository.GetAsync(@event.OrderId); + CheckValidSagaId(order); + + order.SetPaidStatus(); + } + + private void CheckValidSagaId(Order orderSaga) + { + if (orderSaga is null) + { + throw new OrderingDomainException("Not able to process order saga event. Reason: no valid orderId"); + } } } -} +} \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs index 4336e9326..98d4fda08 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs @@ -22,7 +22,7 @@ var order = await _orderRepository.GetAsync(@event.OrderId); CheckValidSagaId(order); - order.SetOrderStockConfirmed(); + order.SetStockConfirmedStatus(); } private void CheckValidSagaId(Order orderSaga) diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs index a60e59f89..d80a966f0 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs @@ -28,7 +28,7 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling .FindAll(c => !c.Confirmed) .Select(c => c.ProductId); - orderToUpdate.SetOrderStockConfirmed(orderStockNotConfirmedItems); + orderToUpdate.SetStockConfirmedStatus(orderStockNotConfirmedItems); } private void CheckValidSagaId(Order orderSaga) diff --git a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs index 8a4c51795..9ebeb21b7 100644 --- a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs +++ b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs @@ -7,15 +7,11 @@ using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency; using Ordering.API.Application.Commands; using Ordering.API.Application.IntegrationCommands.Commands; -using Ordering.API.Application.IntegrationEvents.Events; using Ordering.Domain.Exceptions; using System; -using System.Collections.Generic; using System.Linq; -using System.Reflection; using System.Threading.Tasks; using Ordering.API.Application.IntegrationEvents; -using Ordering.API.Application.IntegrationEvents.Events; namespace Ordering.API.Application.Sagas { @@ -27,7 +23,7 @@ namespace Ordering.API.Application.Sagas /// the opportunity to cancel the order before proceeding /// with the validations. /// - public class OrderProcessSaga : Saga, + public class OrderProcessSaga : OrderSaga, IIntegrationEventHandler, IAsyncRequestHandler { @@ -63,17 +59,9 @@ namespace Ordering.API.Application.Sagas if (orderSaga.OrderStatus != OrderStatus.Cancelled) { - orderSaga.SetOrderStatusId(OrderStatus.AwaitingValidation.Id); - await SaveChangesAsync(); - - var orderStockList = orderSaga.OrderItems - .Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits())); - - var confirmOrderStockEvent = new ConfirmOrderStockCommandMsg(orderSaga.Id, orderStockList); + orderSaga.SetAwaitingValidationStatus(); - await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(confirmOrderStockEvent); - - await _orderingIntegrationEventService.PublishThroughEventBusAsync(confirmOrderStockEvent); + await SaveChangesAsync(); } } diff --git a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderSaga.cs b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderSaga.cs new file mode 100644 index 000000000..dd8e46ac7 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderSaga.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; +using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; + +namespace Ordering.API.Application.Sagas +{ + public abstract class OrderSaga : Saga + { + private OrderingContext _orderingContext; + + public OrderSaga(OrderingContext orderingContext) : base(orderingContext) + { + _orderingContext = orderingContext; + } + + public override Order FindSagaById(int id) + { + var order = _orderingContext.Orders + .Single(c => c.Id == id); + + _orderingContext.Entry(order) + .Member("OrderStatus"); + + return order; + } + + public override async Task SaveChangesAsync() + { + return await _orderingContext.SaveEntitiesAsync(); + } + } +} diff --git a/src/Services/Ordering/Ordering.API/Application/Sagas/Saga.cs b/src/Services/Ordering/Ordering.API/Application/Sagas/Saga.cs index 14925f93f..caaa3fa37 100644 --- a/src/Services/Ordering/Ordering.API/Application/Sagas/Saga.cs +++ b/src/Services/Ordering/Ordering.API/Application/Sagas/Saga.cs @@ -14,17 +14,13 @@ namespace Ordering.API.Application.Sagas _dbContext = dbContext; } - protected TEntity FindSagaById(int id, DbContext context = null) - { - var ctx = context ?? _dbContext; - return ctx.Set().Where(x => x.Id == id).SingleOrDefault(); - } + public abstract TEntity FindSagaById(int id); - protected async Task SaveChangesAsync(DbContext context = null) - { - var ctx = context ?? _dbContext; - var result = await ctx.SaveChangesAsync(); - return result > 0; - } + public abstract Task SaveChangesAsync(); + //{ + // var ctx = context ?? _dbContext; + // var result = await ctx.SaveChangesAsync(); + // return result > 0; + //} } } diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/OrderingContextSeed.cs b/src/Services/Ordering/Ordering.API/Infrastructure/OrderingContextSeed.cs index b41d019fb..68b1baf34 100644 --- a/src/Services/Ordering/Ordering.API/Infrastructure/OrderingContextSeed.cs +++ b/src/Services/Ordering/Ordering.API/Infrastructure/OrderingContextSeed.cs @@ -33,7 +33,7 @@ { context.OrderStatus.Add(OrderStatus.Submited); context.OrderStatus.Add(OrderStatus.AwaitingValidation); - context.OrderStatus.Add(OrderStatus.StockValidated); + context.OrderStatus.Add(OrderStatus.StockConfirmed); context.OrderStatus.Add(OrderStatus.Paid); context.OrderStatus.Add(OrderStatus.Shipped); context.OrderStatus.Add(OrderStatus.Cancelled); diff --git a/src/Services/Ordering/Ordering.API/Startup.cs b/src/Services/Ordering/Ordering.API/Startup.cs index b5b01e068..4f3ca9dfa 100644 --- a/src/Services/Ordering/Ordering.API/Startup.cs +++ b/src/Services/Ordering/Ordering.API/Startup.cs @@ -7,7 +7,6 @@ using global::Ordering.API.Application.IntegrationEvents.Events; using global::Ordering.API.Infrastructure.Middlewares; using global::Ordering.API.Application.IntegrationCommands.Commands; - using global::Ordering.API.Application.IntegrationEvents.Events; using global::Ordering.API.Application.Sagas; using Infrastructure; using Infrastructure.Auth; @@ -31,7 +30,6 @@ using System; using System.Data.Common; using System.Reflection; - using global::Ordering.API.Application.IntegrationEvents.EventHandling; public class Startup { @@ -110,7 +108,7 @@ services.AddTransient(); services.AddTransient>( sp => (DbConnection c) => new IntegrationEventLogService(c)); - var serviceProvider = services.BuildServiceProvider(); + services.AddTransient(); services.AddSingleton(sp =>