Merge
This commit is contained in:
commit
3f9574c8c9
@ -90,3 +90,5 @@ services:
|
||||
build:
|
||||
context: ./src/Services/Payment/Payment.API
|
||||
dockerfile: Dockerfile
|
||||
depends_on:
|
||||
- rabbitmq
|
||||
|
@ -165,12 +165,6 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
|
||||
|
||||
protected virtual void ConfigureEventBus(IApplicationBuilder app)
|
||||
{
|
||||
//var catalogPriceHandler = app.ApplicationServices
|
||||
// .GetService<IIntegrationEventHandler<ProductPriceChangedIntegrationEvent>>();
|
||||
|
||||
//var orderStartedHandler = app.ApplicationServices
|
||||
// .GetService<IIntegrationEventHandler<OrderStartedIntegrationEvent>>();
|
||||
|
||||
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
||||
eventBus.Subscribe<ProductPriceChangedIntegrationEvent, ProductPriceChangedIntegrationEventHandler>();
|
||||
eventBus.Subscribe<OrderStartedIntegrationEvent, OrderStartedIntegrationEventHandler>();
|
||||
|
@ -70,18 +70,18 @@
|
||||
{
|
||||
return new List<CatalogItem>()
|
||||
{
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Bot Black Hoodie", Name = ".NET Bot Black Hoodie", Price = 19.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/1" },
|
||||
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=2, Description = ".NET Black & White Mug", Name = ".NET Black & White Mug", Price= 8.50M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/2" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/3" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation T-shirt", Name = ".NET Foundation T-shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/4" },
|
||||
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/5" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Hoodie", Name = ".NET Blue Hoodie", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/6" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/7" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Hoodie", Name = "Kudu Purple Hoodie", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/8" },
|
||||
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=5, Description = "Cup<T> White Mug", Name = "Cup<T> White Mug", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/9" },
|
||||
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/10" },
|
||||
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup<T> Sheet", Name = "Cup<T> Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/11" },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/12" }
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Bot Black Hoodie", Name = ".NET Bot Black Hoodie", Price = 19.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/1", AvailableStock = 1},
|
||||
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=2, Description = ".NET Black & White Mug", Name = ".NET Black & White Mug", Price= 8.50M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/2", AvailableStock = 1 },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/3", AvailableStock = 1 },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation T-shirt", Name = ".NET Foundation T-shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/4", AvailableStock = 1 },
|
||||
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/5", AvailableStock = 1 },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Hoodie", Name = ".NET Blue Hoodie", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/6", AvailableStock = 1 },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/7", AvailableStock = 1 },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Hoodie", Name = "Kudu Purple Hoodie", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/8", AvailableStock = 1 },
|
||||
new CatalogItem() { CatalogTypeId=1,CatalogBrandId=5, Description = "Cup<T> White Mug", Name = "Cup<T> White Mug", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/9", AvailableStock = 1 },
|
||||
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/10", AvailableStock = 1 },
|
||||
new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup<T> Sheet", Name = "Cup<T> Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/11", AvailableStock = 1 },
|
||||
new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/12", AvailableStock = 1 }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -12,23 +12,23 @@
|
||||
using Commands;
|
||||
using IntegrationEvents.Events;
|
||||
|
||||
public class ConfirmOrderStockCommandMsgHandler : IIntegrationEventHandler<ConfirmOrderStockCommandMsg>
|
||||
public class ConfirmOrderStockCommandHandler : IIntegrationEventHandler<ConfirmOrderStockCommand>
|
||||
{
|
||||
private readonly CatalogContext _catalogContext;
|
||||
private readonly ICatalogIntegrationEventService _catalogIntegrationEventService;
|
||||
|
||||
public ConfirmOrderStockCommandMsgHandler(CatalogContext catalogContext,
|
||||
public ConfirmOrderStockCommandHandler(CatalogContext catalogContext,
|
||||
ICatalogIntegrationEventService catalogIntegrationEventService)
|
||||
{
|
||||
_catalogContext = catalogContext;
|
||||
_catalogIntegrationEventService = catalogIntegrationEventService;
|
||||
}
|
||||
|
||||
public async Task Handle(ConfirmOrderStockCommandMsg @event)
|
||||
public async Task Handle(ConfirmOrderStockCommand command)
|
||||
{
|
||||
var confirmedOrderStockItems = new List<ConfirmedOrderStockItem>();
|
||||
|
||||
foreach (var orderStockItem in @event.OrderStockItems)
|
||||
foreach (var orderStockItem in command.OrderStockItems)
|
||||
{
|
||||
var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId);
|
||||
CheckValidcatalogItemId(catalogItem);
|
||||
@ -39,9 +39,9 @@
|
||||
confirmedOrderStockItems.Add(confirmedOrderStockItem);
|
||||
}
|
||||
|
||||
var confirmedIntegrationEvent = confirmedOrderStockItems.Any(c => !c.Confirmed)
|
||||
? (IntegrationEvent) new OrderStockNotConfirmedIntegrationEvent(@event.OrderId, confirmedOrderStockItems)
|
||||
: new OrderStockConfirmedIntegrationEvent(@event.OrderId);
|
||||
var confirmedIntegrationEvent = confirmedOrderStockItems.Any(c => !c.HasStock)
|
||||
? (IntegrationEvent) new OrderStockNotConfirmedIntegrationEvent(command.OrderId, confirmedOrderStockItems)
|
||||
: new OrderStockConfirmedIntegrationEvent(command.OrderId);
|
||||
|
||||
await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(confirmedIntegrationEvent);
|
||||
await _catalogIntegrationEventService.PublishThroughEventBusAsync(confirmedIntegrationEvent);
|
@ -3,24 +3,21 @@
|
||||
using BuildingBlocks.EventBus.Abstractions;
|
||||
using System.Threading.Tasks;
|
||||
using Infrastructure;
|
||||
using global::Catalog.API.Infrastructure.Exceptions;
|
||||
using global::Catalog.API.IntegrationEvents;
|
||||
using Model;
|
||||
using Commands;
|
||||
|
||||
public class DecrementOrderStockCommandMsgHandler : IIntegrationEventHandler<DecrementOrderStockCommandMsg>
|
||||
public class DecrementOrderStockCommandHandler : IIntegrationEventHandler<DecrementOrderStockCommand>
|
||||
{
|
||||
private readonly CatalogContext _catalogContext;
|
||||
|
||||
public DecrementOrderStockCommandMsgHandler(CatalogContext catalogContext)
|
||||
public DecrementOrderStockCommandHandler(CatalogContext catalogContext)
|
||||
{
|
||||
_catalogContext = catalogContext;
|
||||
}
|
||||
|
||||
public async Task Handle(DecrementOrderStockCommandMsg @event)
|
||||
public async Task Handle(DecrementOrderStockCommand command)
|
||||
{
|
||||
//we're not blocking stock/inventory
|
||||
foreach (var orderStockItem in @event.OrderStockItems)
|
||||
foreach (var orderStockItem in command.OrderStockItems)
|
||||
{
|
||||
var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId);
|
||||
|
@ -3,12 +3,12 @@
|
||||
using BuildingBlocks.EventBus.Events;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class ConfirmOrderStockCommandMsg : IntegrationEvent
|
||||
public class ConfirmOrderStockCommand : IntegrationEvent
|
||||
{
|
||||
public int OrderId { get; }
|
||||
public IEnumerable<OrderStockItem> OrderStockItems { get; }
|
||||
|
||||
public ConfirmOrderStockCommandMsg(int orderId,
|
||||
public ConfirmOrderStockCommand(int orderId,
|
||||
IEnumerable<OrderStockItem> orderStockItems)
|
||||
{
|
||||
OrderId = orderId;
|
@ -3,12 +3,12 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||
|
||||
public class DecrementOrderStockCommandMsg : IntegrationEvent
|
||||
public class DecrementOrderStockCommand : IntegrationEvent
|
||||
{
|
||||
public int OrderId { get; }
|
||||
public IEnumerable<OrderStockItem> OrderStockItems { get; }
|
||||
|
||||
public DecrementOrderStockCommandMsg(int orderId,
|
||||
public DecrementOrderStockCommand(int orderId,
|
||||
IEnumerable<OrderStockItem> orderStockItems)
|
||||
{
|
||||
OrderId = orderId;
|
@ -7,25 +7,25 @@
|
||||
{
|
||||
public int OrderId { get; }
|
||||
|
||||
public IEnumerable<ConfirmedOrderStockItem> OrderStockItem { get; }
|
||||
public List<ConfirmedOrderStockItem> OrderStockItems { get; }
|
||||
|
||||
public OrderStockNotConfirmedIntegrationEvent(int orderId,
|
||||
IEnumerable<ConfirmedOrderStockItem> orderStockItem)
|
||||
public OrderStockNotConfirmedIntegrationEvent(int orderId,
|
||||
List<ConfirmedOrderStockItem> orderStockItems)
|
||||
{
|
||||
OrderId = orderId;
|
||||
OrderStockItem = orderStockItem;
|
||||
OrderStockItems = orderStockItems;
|
||||
}
|
||||
}
|
||||
|
||||
public class ConfirmedOrderStockItem
|
||||
{
|
||||
public int ProductId { get; }
|
||||
public bool Confirmed { get; }
|
||||
public bool HasStock { get; }
|
||||
|
||||
public ConfirmedOrderStockItem(int productId, bool confirmed)
|
||||
public ConfirmedOrderStockItem(int productId, bool hasStock)
|
||||
{
|
||||
ProductId = productId;
|
||||
Confirmed = confirmed;
|
||||
HasStock = hasStock;
|
||||
}
|
||||
}
|
||||
}
|
@ -121,10 +121,7 @@
|
||||
return new DefaultRabbitMQPersistentConnection(factory, logger);
|
||||
});
|
||||
|
||||
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
||||
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
||||
services.AddTransient<IIntegrationEventHandler<ConfirmOrderStockCommandMsg>, ConfirmOrderStockCommandMsgHandler>();
|
||||
services.AddTransient<IIntegrationEventHandler<DecrementOrderStockCommandMsg>, DecrementOrderStockCommandMsgHandler>();
|
||||
RegisterServiceBus(services);
|
||||
|
||||
var container = new ContainerBuilder();
|
||||
container.Populate(services);
|
||||
@ -188,12 +185,24 @@
|
||||
}
|
||||
}
|
||||
|
||||
private void RegisterServiceBus(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
||||
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
||||
|
||||
services.AddTransient<IIntegrationEventHandler<ConfirmOrderStockCommand>,
|
||||
ConfirmOrderStockCommandHandler>();
|
||||
services.AddTransient<IIntegrationEventHandler<DecrementOrderStockCommand>,
|
||||
DecrementOrderStockCommandHandler>();
|
||||
|
||||
}
|
||||
|
||||
private void ConfigureEventBus(IApplicationBuilder app)
|
||||
{
|
||||
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
||||
|
||||
eventBus.Subscribe<ConfirmOrderStockCommandMsg, IIntegrationEventHandler<ConfirmOrderStockCommandMsg>>();
|
||||
eventBus.Subscribe<DecrementOrderStockCommandMsg, IIntegrationEventHandler<DecrementOrderStockCommandMsg>>();
|
||||
eventBus.Subscribe<ConfirmOrderStockCommand, IIntegrationEventHandler<ConfirmOrderStockCommand>>();
|
||||
eventBus.Subscribe<DecrementOrderStockCommand, IIntegrationEventHandler<DecrementOrderStockCommand>>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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.SetSubmitedStatus();
|
||||
|
||||
foreach (var item in message.OrderItems)
|
||||
{
|
||||
order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units);
|
||||
|
@ -28,8 +28,6 @@
|
||||
|
||||
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}");
|
||||
@ -37,10 +35,10 @@
|
||||
var orderStockList = orderStatusChangedToAwaitingValidationDomainEvent.OrderItems
|
||||
.Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits()));
|
||||
|
||||
var confirmOrderStockEvent = new ConfirmOrderStockCommandMsg(orderStatusChangedToAwaitingValidationDomainEvent.OrderId,
|
||||
var confirmOrderStockCommand = new ConfirmOrderStockCommand(orderStatusChangedToAwaitingValidationDomainEvent.OrderId,
|
||||
orderStockList);
|
||||
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(confirmOrderStockEvent);
|
||||
await _orderingIntegrationEventService.PublishThroughEventBusAsync(confirmOrderStockEvent);
|
||||
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(confirmOrderStockCommand);
|
||||
await _orderingIntegrationEventService.PublishThroughEventBusAsync(confirmOrderStockCommand);
|
||||
}
|
||||
}
|
||||
}
|
@ -28,8 +28,6 @@
|
||||
|
||||
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}");
|
||||
@ -37,15 +35,10 @@
|
||||
var orderStockList = orderStatusChangedToPaidDomainEvent.OrderItems
|
||||
.Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits()));
|
||||
|
||||
var decrementOrderStockCommandMsg = new DecrementOrderStockCommandMsg(orderStatusChangedToPaidDomainEvent.OrderId,
|
||||
var decrementOrderStockCommand = new DecrementOrderStockCommand(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);
|
||||
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(decrementOrderStockCommand);
|
||||
await _orderingIntegrationEventService.PublishThroughEventBusAsync(decrementOrderStockCommand);
|
||||
}
|
||||
}
|
||||
}
|
@ -27,15 +27,13 @@
|
||||
|
||||
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);
|
||||
var payOrderCommand = new PayOrderCommand(orderStatusChangedToStockConfirmedDomainEvent.OrderId);
|
||||
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommand);
|
||||
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommand);
|
||||
}
|
||||
}
|
||||
}
|
@ -2,11 +2,11 @@
|
||||
|
||||
namespace Ordering.API.Application.IntegrationCommands.Commands
|
||||
{
|
||||
public class ConfirmGracePeriodCommandMsg : IntegrationEvent
|
||||
public class ConfirmGracePeriodCommand : IntegrationEvent
|
||||
{
|
||||
public int OrderId { get; }
|
||||
|
||||
public ConfirmGracePeriodCommandMsg(int orderId) =>
|
||||
public ConfirmGracePeriodCommand(int orderId) =>
|
||||
OrderId = orderId;
|
||||
}
|
||||
}
|
@ -3,12 +3,12 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||
|
||||
public class ConfirmOrderStockCommandMsg : IntegrationEvent
|
||||
public class ConfirmOrderStockCommand : IntegrationEvent
|
||||
{
|
||||
public int OrderId { get; }
|
||||
public IEnumerable<OrderStockItem> OrderStockItems { get; }
|
||||
|
||||
public ConfirmOrderStockCommandMsg(int orderId,
|
||||
public ConfirmOrderStockCommand(int orderId,
|
||||
IEnumerable<OrderStockItem> orderStockItems)
|
||||
{
|
||||
OrderId = orderId;
|
@ -3,12 +3,12 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||
|
||||
public class DecrementOrderStockCommandMsg : IntegrationEvent
|
||||
public class DecrementOrderStockCommand : IntegrationEvent
|
||||
{
|
||||
public int OrderId { get; }
|
||||
public IEnumerable<OrderStockItem> OrderStockItems { get; }
|
||||
|
||||
public DecrementOrderStockCommandMsg(int orderId,
|
||||
public DecrementOrderStockCommand(int orderId,
|
||||
IEnumerable<OrderStockItem> orderStockItems)
|
||||
{
|
||||
OrderId = orderId;
|
@ -2,11 +2,11 @@
|
||||
{
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||
|
||||
public class PayOrderCommandMsg : IntegrationEvent
|
||||
public class PayOrderCommand : IntegrationEvent
|
||||
{
|
||||
public int OrderId { get; }
|
||||
|
||||
public PayOrderCommandMsg(int orderId)
|
||||
public PayOrderCommand(int orderId)
|
||||
{
|
||||
OrderId = orderId;
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -17,9 +17,11 @@
|
||||
|
||||
public async Task Handle(OrderPaymentFailedIntegrationEvent @event)
|
||||
{
|
||||
var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
|
||||
var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId);
|
||||
|
||||
orderToUpdate.SetCancelledStatus();
|
||||
|
||||
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,9 +17,11 @@
|
||||
|
||||
public async Task Handle(OrderPaymentSuccededIntegrationEvent @event)
|
||||
{
|
||||
var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
|
||||
var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId);
|
||||
|
||||
orderToUpdate.SetPaidStatus();
|
||||
|
||||
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
|
||||
}
|
||||
}
|
||||
}
|
@ -17,9 +17,11 @@
|
||||
|
||||
public async Task Handle(OrderStockConfirmedIntegrationEvent @event)
|
||||
{
|
||||
var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
|
||||
var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId);
|
||||
|
||||
orderToUpdate.SetStockConfirmedStatus();
|
||||
|
||||
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
|
||||
}
|
||||
}
|
||||
}
|
@ -19,13 +19,15 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
||||
|
||||
public async Task Handle(OrderStockNotConfirmedIntegrationEvent @event)
|
||||
{
|
||||
var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
|
||||
var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId);
|
||||
|
||||
var orderStockNotConfirmedItems = @event.OrderStockItems
|
||||
.FindAll(c => !c.Confirmed)
|
||||
.FindAll(c => !c.HasStock)
|
||||
.Select(c => c.ProductId);
|
||||
|
||||
orderToUpdate.SetStockConfirmedStatus(orderStockNotConfirmedItems);
|
||||
|
||||
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
|
||||
}
|
||||
}
|
||||
}
|
@ -21,12 +21,12 @@ namespace Ordering.API.Application.IntegrationEvents.Events
|
||||
public class ConfirmedOrderStockItem
|
||||
{
|
||||
public int ProductId { get; }
|
||||
public bool Confirmed { get; }
|
||||
public bool HasStock { get; }
|
||||
|
||||
public ConfirmedOrderStockItem(int productId, bool confirmed)
|
||||
public ConfirmedOrderStockItem(int productId, bool hasStock)
|
||||
{
|
||||
ProductId = productId;
|
||||
Confirmed = confirmed;
|
||||
HasStock = hasStock;
|
||||
}
|
||||
}
|
||||
}
|
@ -21,7 +21,7 @@ namespace Ordering.API.Application.Sagas
|
||||
/// with the validations.
|
||||
/// </summary>
|
||||
public class OrderProcessSaga : OrderSaga,
|
||||
IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>,
|
||||
IIntegrationEventHandler<ConfirmGracePeriodCommand>,
|
||||
IAsyncRequestHandler<CancelOrderCommand, bool>,
|
||||
IAsyncRequestHandler<ShipOrderCommand, bool>
|
||||
{
|
||||
@ -43,17 +43,13 @@ namespace Ordering.API.Application.Sagas
|
||||
/// period has completed.
|
||||
/// </param>
|
||||
/// <returns></returns>
|
||||
public async Task Handle(ConfirmGracePeriodCommandMsg @event)
|
||||
public async Task Handle(ConfirmGracePeriodCommand command)
|
||||
{
|
||||
var orderSaga = FindSagaById(@event.OrderId);
|
||||
var orderSaga = FindSagaById(command.OrderId);
|
||||
CheckValidSagaId(orderSaga);
|
||||
|
||||
if (orderSaga.OrderStatus != OrderStatus.Cancelled)
|
||||
{
|
||||
orderSaga.SetAwaitingValidationStatus();
|
||||
|
||||
await SaveChangesAsync();
|
||||
}
|
||||
orderSaga.SetAwaitingValidationStatus();
|
||||
await SaveChangesAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -68,14 +64,9 @@ namespace Ordering.API.Application.Sagas
|
||||
var orderSaga = FindSagaById(command.OrderNumber);
|
||||
CheckValidSagaId(orderSaga);
|
||||
|
||||
// Not possible to cancel order when
|
||||
// it has already been shipped
|
||||
if (orderSaga.GetOrderStatusId() != OrderStatus.Cancelled.Id
|
||||
|| orderSaga.GetOrderStatusId() != OrderStatus.Shipped.Id)
|
||||
{
|
||||
orderSaga.SetCancelledStatus();
|
||||
result = await SaveChangesAsync();
|
||||
}
|
||||
orderSaga.SetCancelledStatus();
|
||||
result = await SaveChangesAsync();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -91,13 +82,9 @@ namespace Ordering.API.Application.Sagas
|
||||
var orderSaga = FindSagaById(command.OrderNumber);
|
||||
CheckValidSagaId(orderSaga);
|
||||
|
||||
// Only ship order when
|
||||
// its status is paid
|
||||
if (orderSaga.GetOrderStatusId() == OrderStatus.Paid.Id)
|
||||
{
|
||||
orderSaga.SetShippedStatus();
|
||||
result = await SaveChangesAsync();
|
||||
}
|
||||
orderSaga.SetShippedStatus();
|
||||
result = await SaveChangesAsync();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||
@ -20,10 +18,9 @@ namespace Ordering.API.Application.Sagas
|
||||
public override Order FindSagaById(int id)
|
||||
{
|
||||
var order = _orderingContext.Orders
|
||||
.Single(c => c.Id == id);
|
||||
|
||||
_orderingContext.Entry(order)
|
||||
.Member("OrderStatus");
|
||||
.Include(c => c.OrderStatus)
|
||||
.Include(c => c.OrderItems)
|
||||
.Single(c => c.Id == id);
|
||||
|
||||
return order;
|
||||
}
|
||||
|
@ -31,6 +31,7 @@
|
||||
using System;
|
||||
using System.Data.Common;
|
||||
using System.Reflection;
|
||||
using global::Ordering.API.Application.IntegrationEvents.EventHandling;
|
||||
|
||||
public class Startup
|
||||
{
|
||||
@ -124,6 +125,7 @@
|
||||
return new DefaultRabbitMQPersistentConnection(factory, logger);
|
||||
});
|
||||
|
||||
RegisterServiceBus(services);
|
||||
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
||||
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
||||
services.AddTransient<UserCheckoutAcceptedIntegrationEventHandler>();
|
||||
@ -170,10 +172,33 @@
|
||||
|
||||
}
|
||||
|
||||
private void RegisterServiceBus(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
||||
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
||||
|
||||
services.AddTransient<IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>>();
|
||||
services.AddTransient<IIntegrationEventHandler<ConfirmGracePeriodCommand>, OrderProcessSaga>();
|
||||
services.AddTransient<IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>,
|
||||
OrderStockConfirmedIntegrationEventHandler>();
|
||||
services.AddTransient<IIntegrationEventHandler<OrderStockNotConfirmedIntegrationEvent>,
|
||||
OrderStockNotConfirmedIntegrationEventHandler>();
|
||||
services.AddTransient<IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>,
|
||||
OrderPaymentFailedIntegrationEventHandler>();
|
||||
services.AddTransient<IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>,
|
||||
OrderPaymentSuccededIntegrationEventHandler>();
|
||||
}
|
||||
|
||||
private void ConfigureEventBus(IApplicationBuilder app)
|
||||
{
|
||||
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
||||
|
||||
eventBus.Subscribe<UserCheckoutAcceptedIntegrationEvent, IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>>();
|
||||
eventBus.Subscribe<ConfirmGracePeriodCommand, IIntegrationEventHandler<ConfirmGracePeriodCommand>>();
|
||||
eventBus.Subscribe<OrderStockConfirmedIntegrationEvent, IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>>();
|
||||
eventBus.Subscribe<OrderStockNotConfirmedIntegrationEvent, IIntegrationEventHandler<OrderStockNotConfirmedIntegrationEvent>>();
|
||||
eventBus.Subscribe<OrderPaymentFailedIntegrationEvent, IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>>();
|
||||
eventBus.Subscribe<OrderPaymentSuccededIntegrationEvent, IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>>();
|
||||
eventBus.Subscribe<UserCheckoutAcceptedIntegrationEvent, UserCheckoutAcceptedIntegrationEventHandler>();
|
||||
|
||||
eventBus.Subscribe<ConfirmGracePeriodCommandMsg, IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>>();
|
||||
|
@ -13,5 +13,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
||||
void Update(Order order);
|
||||
|
||||
Task<Order> GetAsync(int orderId);
|
||||
|
||||
Task<Order> GetWithDependenciesAsync(int orderId);
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
||||
// but only through the method OrderAggrergateRoot.AddOrderItem() which includes behaviour.
|
||||
private readonly List<OrderItem> _orderItems;
|
||||
|
||||
public IEnumerable<OrderItem> OrderItems => _orderItems.AsReadOnly();
|
||||
public IReadOnlyCollection<OrderItem> OrderItems => _orderItems;
|
||||
// Using List<>.AsReadOnly()
|
||||
// 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)
|
||||
@ -95,21 +95,18 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
||||
}
|
||||
|
||||
#region Status Changes
|
||||
public void SetSubmitedStatus()
|
||||
{
|
||||
_orderStatusId = OrderStatus.Submited.Id;
|
||||
}
|
||||
|
||||
public void SetAwaitingValidationStatus()
|
||||
{
|
||||
if (_orderStatusId != OrderStatus.Submited.Id)
|
||||
if (_orderStatusId != OrderStatus.Submited.Id &&
|
||||
_orderStatusId != OrderStatus.Cancelled.Id)
|
||||
{
|
||||
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)
|
||||
@ -121,15 +118,14 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
||||
|
||||
if (orderStockNotConfirmedItems is null)
|
||||
{
|
||||
OrderStatus = OrderStatus.StockConfirmed;
|
||||
|
||||
_description = "All the items were confirmed with available stock.";
|
||||
|
||||
AddDomainEvent(new OrderStatusChangedToStockConfirmedDomainEvent(Id));
|
||||
|
||||
_orderStatusId = OrderStatus.StockConfirmed.Id;
|
||||
_description = "All the items were confirmed with available stock.";
|
||||
}
|
||||
else
|
||||
{
|
||||
OrderStatus = OrderStatus.Cancelled;
|
||||
_orderStatusId = OrderStatus.Cancelled.Id;
|
||||
|
||||
var itemsStockNotConfirmedProductNames = OrderItems
|
||||
.Where(c => orderStockNotConfirmedItems.Contains(c.ProductId))
|
||||
@ -147,10 +143,10 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
||||
StatusChangeException();
|
||||
}
|
||||
|
||||
AddDomainEvent(new OrderStatusChangedToPaidDomainEvent(Id, OrderItems));
|
||||
|
||||
_orderStatusId = OrderStatus.Paid.Id;
|
||||
_description = "The payment was performed at a simulated \"American Bank checking bank account endinf on XX35071\"";
|
||||
|
||||
AddDomainEvent(new OrderStatusChangedToPaidDomainEvent(Id, OrderItems));
|
||||
}
|
||||
|
||||
public void SetShippedStatus()
|
||||
@ -162,15 +158,13 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
||||
|
||||
_orderStatusId = OrderStatus.Shipped.Id;
|
||||
_description = "";
|
||||
|
||||
//Call Domain Event
|
||||
}
|
||||
|
||||
public void SetCancelledStatus()
|
||||
{
|
||||
if (_orderStatusId == OrderStatus.Submited.Id)
|
||||
{
|
||||
_description = "The order was cancelled before the grace period was confirm.";
|
||||
_description = "The order was cancelled before the grace period was confirmed.";
|
||||
}
|
||||
else if (_orderStatusId == OrderStatus.AwaitingValidation.Id)
|
||||
{
|
||||
@ -194,11 +188,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
||||
|
||||
#endregion
|
||||
|
||||
public int GetOrderStatusId()
|
||||
{
|
||||
return _orderStatusId;
|
||||
}
|
||||
|
||||
private void AddOrderStartedDomainEvent(string userId, int cardTypeId, string cardNumber,
|
||||
string cardSecurityNumber, string cardHolderName, DateTime cardExpiration)
|
||||
{
|
||||
|
@ -36,6 +36,14 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositor
|
||||
return await _context.Orders.FindAsync(orderId);
|
||||
}
|
||||
|
||||
public async Task<Order> GetWithDependenciesAsync(int orderId)
|
||||
{
|
||||
return await _context.Orders
|
||||
.Include(c => c.OrderStatus)
|
||||
.Include(c => c.OrderItems)
|
||||
.SingleAsync(c => c.Id == orderId);
|
||||
}
|
||||
|
||||
public void Update(Order order)
|
||||
{
|
||||
_context.Entry(order).State = EntityState.Modified;
|
||||
|
@ -3,18 +3,17 @@
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||
using Payment.API.IntegrationCommands.Commands;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using Payment.API.IntegrationEvents;
|
||||
using Payment.API.IntegrationEvents.Events;
|
||||
|
||||
public class PayOrderCommandMsgHandler : IIntegrationEventHandler<PayOrderCommandMsg>
|
||||
public class PayOrderCommandHandler : IIntegrationEventHandler<PayOrderCommand>
|
||||
{
|
||||
private readonly IPaymentIntegrationEventService _paymentIntegrationEventService;
|
||||
|
||||
public PayOrderCommandMsgHandler(IPaymentIntegrationEventService paymentIntegrationEventService)
|
||||
public PayOrderCommandHandler(IPaymentIntegrationEventService paymentIntegrationEventService)
|
||||
=> _paymentIntegrationEventService = paymentIntegrationEventService;
|
||||
|
||||
public async Task Handle(PayOrderCommandMsg @event)
|
||||
public async Task Handle(PayOrderCommand @event)
|
||||
{
|
||||
//PAYMENT SUCCESSED
|
||||
var orderPaymentSuccededIntegrationEvent = new OrderPaymentSuccededIntegrationEvent(@event.OrderId);
|
||||
@ -25,4 +24,4 @@
|
||||
//_paymentIntegrationEventService.PublishThroughEventBus(orderPaymentFailedIntegrationEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -2,10 +2,10 @@
|
||||
{
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||
|
||||
public class PayOrderCommandMsg : IntegrationEvent
|
||||
public class PayOrderCommand : IntegrationEvent
|
||||
{
|
||||
public int OrderId { get; }
|
||||
|
||||
public PayOrderCommandMsg(int orderId) => OrderId = orderId;
|
||||
public PayOrderCommand(int orderId) => OrderId = orderId;
|
||||
}
|
||||
}
|
@ -29,6 +29,9 @@
|
||||
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusRabbitMQ\EventBusRabbitMQ.csproj" />
|
||||
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBus\EventBus.csproj" />
|
||||
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\IntegrationEventLogEF\IntegrationEventLogEF.csproj" />
|
||||
<ProjectReference Include="..\..\..\BuildingBlocks\HealthChecks\src\Microsoft.AspNetCore.HealthChecks\Microsoft.AspNetCore.HealthChecks.csproj" />
|
||||
<ProjectReference Include="..\..\..\BuildingBlocks\HealthChecks\src\Microsoft.Extensions.HealthChecks.SqlServer\Microsoft.Extensions.HealthChecks.SqlServer.csproj" />
|
||||
<ProjectReference Include="..\..\..\BuildingBlocks\HealthChecks\src\Microsoft.Extensions.HealthChecks\Microsoft.Extensions.HealthChecks.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -11,6 +11,8 @@ using Payment.API.IntegrationCommands.Commands;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
|
||||
using RabbitMQ.Client;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
||||
using Payment.API.IntegrationEvents;
|
||||
using Payment.API.IntegrationCommands.CommandHandlers;
|
||||
|
||||
namespace Payment.API
|
||||
{
|
||||
@ -34,6 +36,7 @@ namespace Payment.API
|
||||
// Add framework services.
|
||||
services.AddMvc();
|
||||
|
||||
services.AddTransient<IPaymentIntegrationEventService, PaymentIntegrationEventService>();
|
||||
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
|
||||
{
|
||||
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
|
||||
@ -46,8 +49,7 @@ namespace Payment.API
|
||||
return new DefaultRabbitMQPersistentConnection(factory, logger);
|
||||
});
|
||||
|
||||
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
||||
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
||||
RegisterServiceBus(services);
|
||||
|
||||
services.AddSwaggerGen();
|
||||
services.ConfigureSwaggerGen(options =>
|
||||
@ -81,10 +83,18 @@ namespace Payment.API
|
||||
ConfigureEventBus(app);
|
||||
}
|
||||
|
||||
private void RegisterServiceBus(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
||||
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
||||
|
||||
services.AddTransient<IIntegrationEventHandler<PayOrderCommand>, PayOrderCommandHandler>();
|
||||
}
|
||||
|
||||
private void ConfigureEventBus(IApplicationBuilder app)
|
||||
{
|
||||
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
||||
eventBus.Subscribe<PayOrderCommandMsg, IIntegrationEventHandler<PayOrderCommandMsg>>();
|
||||
eventBus.Subscribe<PayOrderCommand, IIntegrationEventHandler<PayOrderCommand>>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,11 @@
|
||||
namespace SagaManager.IntegrationEvents.Events
|
||||
{
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||
|
||||
public class ConfirmGracePeriodCommand : IntegrationEvent
|
||||
{
|
||||
public int OrderId { get;}
|
||||
|
||||
public ConfirmGracePeriodCommand(int orderId) => OrderId = orderId;
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
namespace SagaManager.IntegrationEvents.Events
|
||||
{
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||
|
||||
// Integration Events notes:
|
||||
// An Event is “something that has happened in the past”, therefore its name has to be
|
||||
// An Integration Event is an event that can cause side effects to other microsrvices, Bounded-Contexts or external systems.
|
||||
public class ConfirmGracePeriodCommandMsg : IntegrationEvent
|
||||
{
|
||||
public int OrderId { get;}
|
||||
|
||||
public ConfirmGracePeriodCommandMsg(int orderId) => OrderId = orderId;
|
||||
}
|
||||
}
|
@ -9,13 +9,9 @@
|
||||
private readonly IEventBus _eventBus;
|
||||
|
||||
public SagaManagerIntegrationEventService(IEventBus eventBus)
|
||||
{
|
||||
_eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
|
||||
}
|
||||
=> _eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
|
||||
|
||||
public void PublishThroughEventBus(IntegrationEvent evt)
|
||||
{
|
||||
_eventBus.Publish(evt);
|
||||
}
|
||||
|
||||
public void PublishThroughEventBus(IntegrationEvent evt) => _eventBus.Publish(evt);
|
||||
}
|
||||
}
|
@ -1,34 +1,27 @@
|
||||
using System.Reflection;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SagaManager.IntegrationEvents;
|
||||
|
||||
namespace SagaManager
|
||||
namespace SagaManager
|
||||
{
|
||||
using System.IO;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
using Autofac;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using RabbitMQ.Client;
|
||||
using Services;
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
using Autofac;
|
||||
using System.Threading.Tasks;
|
||||
using IntegrationEvents;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static IConfigurationRoot Configuration { get; set; }
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
MainAsync().Wait();
|
||||
}
|
||||
|
||||
public static void Main(string[] args) => MainAsync().Wait();
|
||||
|
||||
static async Task MainAsync()
|
||||
{
|
||||
StartUp();
|
||||
@ -44,8 +37,8 @@ namespace SagaManager
|
||||
|
||||
while (true)
|
||||
{
|
||||
sagaManagerService.CheckFinishedGracePeriodOrders();
|
||||
await Task.Delay(30000);
|
||||
sagaManagerService.CheckConfirmedGracePeriodOrders();
|
||||
await Task.Delay(90000);
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,8 +59,7 @@ namespace SagaManager
|
||||
.Configure<SagaManagerSettings>(Configuration)
|
||||
.AddSingleton<ISagaManagerService, SagaManagerService>()
|
||||
.AddSingleton<ISagaManagerIntegrationEventService, SagaManagerIntegrationEventService>()
|
||||
.AddSingleton<IEventBus, EventBusRabbitMQ>()
|
||||
.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>()
|
||||
|
||||
.AddSingleton<IRabbitMQPersistentConnection>(sp =>
|
||||
{
|
||||
var settings = sp.GetRequiredService<IOptions<SagaManagerSettings>>().Value;
|
||||
@ -78,8 +70,7 @@ namespace SagaManager
|
||||
};
|
||||
|
||||
return new DefaultRabbitMQPersistentConnection(factory, logger);
|
||||
})
|
||||
.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
||||
});
|
||||
|
||||
RegisterServiceBus(services);
|
||||
|
||||
|
@ -2,6 +2,6 @@
|
||||
{
|
||||
public interface ISagaManagerService
|
||||
{
|
||||
void CheckFinishedGracePeriodOrders();
|
||||
void CheckConfirmedGracePeriodOrders();
|
||||
}
|
||||
}
|
@ -1,12 +1,9 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace SagaManager.Services
|
||||
namespace SagaManager.Services
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Dapper;
|
||||
using IntegrationEvents;
|
||||
using IntegrationEvents.Events;
|
||||
@ -26,17 +23,18 @@ namespace SagaManager.Services
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CheckFinishedGracePeriodOrders()
|
||||
public void CheckConfirmedGracePeriodOrders()
|
||||
{
|
||||
var orderIds = GetFinishedGracePeriodOrders();
|
||||
var orderIds = GetConfirmedGracePeriodOrders();
|
||||
|
||||
foreach (var orderId in orderIds)
|
||||
{
|
||||
Publish(orderId);
|
||||
var confirmGracePeriodEvent = new ConfirmGracePeriodCommand(orderId);
|
||||
_sagaManagerIntegrationEventService.PublishThroughEventBus(confirmGracePeriodEvent);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<int> GetFinishedGracePeriodOrders()
|
||||
private IEnumerable<int> GetConfirmedGracePeriodOrders()
|
||||
{
|
||||
IEnumerable<int> orderIds = new List<int>();
|
||||
using (var conn = new SqlConnection(_settings.ConnectionString))
|
||||
@ -60,13 +58,5 @@ namespace SagaManager.Services
|
||||
|
||||
return orderIds;
|
||||
}
|
||||
|
||||
private void Publish(int orderId)
|
||||
{
|
||||
var confirmGracePeriodEvent = new ConfirmGracePeriodCommandMsg(orderId);
|
||||
|
||||
// Publish through the Event Bus
|
||||
_sagaManagerIntegrationEventService.PublishThroughEventBus(confirmGracePeriodEvent);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user