minor changes
This commit is contained in:
parent
3556a5d126
commit
8b80eeae52
@ -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>();
|
||||
|
@ -3,9 +3,6 @@
|
||||
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>
|
||||
|
@ -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,6 +185,18 @@
|
||||
}
|
||||
}
|
||||
|
||||
private void RegisterServiceBus(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
||||
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
||||
|
||||
services.AddTransient<IIntegrationEventHandler<ConfirmOrderStockCommandMsg>,
|
||||
ConfirmOrderStockCommandMsgHandler>();
|
||||
services.AddTransient<IIntegrationEventHandler<DecrementOrderStockCommandMsg>,
|
||||
DecrementOrderStockCommandMsgHandler>();
|
||||
|
||||
}
|
||||
|
||||
private void ConfigureEventBus(IApplicationBuilder app)
|
||||
{
|
||||
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
||||
|
@ -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}");
|
||||
|
@ -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}");
|
||||
|
@ -27,8 +27,6 @@
|
||||
|
||||
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}");
|
||||
|
@ -21,7 +21,7 @@
|
||||
|
||||
orderToUpdate.SetCancelledStatus();
|
||||
|
||||
await _orderRepository.UnitOfWork.SaveChangesAsync();
|
||||
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,7 @@
|
||||
|
||||
orderToUpdate.SetPaidStatus();
|
||||
|
||||
await _orderRepository.UnitOfWork.SaveChangesAsync();
|
||||
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
|
||||
}
|
||||
}
|
||||
}
|
@ -21,7 +21,7 @@
|
||||
|
||||
orderToUpdate.SetStockConfirmedStatus();
|
||||
|
||||
await _orderRepository.UnitOfWork.SaveChangesAsync();
|
||||
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
|
||||
}
|
||||
}
|
||||
}
|
@ -27,7 +27,7 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
||||
|
||||
orderToUpdate.SetStockConfirmedStatus(orderStockNotConfirmedItems);
|
||||
|
||||
await _orderRepository.UnitOfWork.SaveChangesAsync();
|
||||
await _orderRepository.UnitOfWork.SaveEntitiesAsync();
|
||||
}
|
||||
}
|
||||
}
|
@ -48,11 +48,8 @@ namespace Ordering.API.Application.Sagas
|
||||
var orderSaga = FindSagaById(command.OrderId);
|
||||
CheckValidSagaId(orderSaga);
|
||||
|
||||
if (orderSaga.OrderStatus != OrderStatus.Cancelled)
|
||||
{
|
||||
orderSaga.SetAwaitingValidationStatus();
|
||||
await SaveChangesAsync();
|
||||
}
|
||||
orderSaga.SetAwaitingValidationStatus();
|
||||
await SaveChangesAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -67,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;
|
||||
}
|
||||
|
||||
@ -90,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;
|
||||
}
|
||||
|
||||
|
@ -8,8 +8,6 @@
|
||||
using global::Ordering.API.Application.IntegrationEvents.Events;
|
||||
using global::Ordering.API.Application.Sagas;
|
||||
using global::Ordering.API.Infrastructure.Middlewares;
|
||||
using global::Ordering.API.Application.IntegrationCommands.Commands;
|
||||
using global::Ordering.API.Application.Sagas;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Auth;
|
||||
using Infrastructure.AutofacModules;
|
||||
@ -126,18 +124,7 @@
|
||||
return new DefaultRabbitMQPersistentConnection(factory, logger);
|
||||
});
|
||||
|
||||
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
||||
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
||||
services.AddTransient<IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>>();
|
||||
services.AddTransient<IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>, OrderProcessSaga>();
|
||||
services.AddTransient<IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>,
|
||||
OrderStockConfirmedIntegrationEventHandler>();
|
||||
services.AddTransient<IIntegrationEventHandler<OrderStockNotConfirmedIntegrationEvent>,
|
||||
OrderStockNotConfirmedIntegrationEventHandler>();
|
||||
services.AddTransient<IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>,
|
||||
OrderPaymentFailedIntegrationEventHandler>();
|
||||
services.AddTransient<IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>,
|
||||
OrderPaymentSuccededIntegrationEventHandler>();
|
||||
RegisterServiceBus(services);
|
||||
services.AddOptions();
|
||||
|
||||
//configure autofac
|
||||
@ -178,6 +165,23 @@
|
||||
|
||||
}
|
||||
|
||||
private void RegisterServiceBus(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
||||
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
||||
|
||||
services.AddTransient<IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>>();
|
||||
services.AddTransient<IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>, 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>();
|
||||
|
@ -95,14 +95,11 @@ 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();
|
||||
}
|
||||
@ -167,7 +164,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
|
||||
{
|
||||
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)
|
||||
{
|
||||
@ -191,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)
|
||||
{
|
||||
|
@ -2,9 +2,6 @@
|
||||
{
|
||||
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;}
|
||||
|
@ -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,33 +1,26 @@
|
||||
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()
|
||||
{
|
||||
@ -45,7 +38,7 @@ namespace SagaManager
|
||||
while (true)
|
||||
{
|
||||
sagaManagerService.CheckFinishedGracePeriodOrders();
|
||||
await Task.Delay(30000);
|
||||
await Task.Delay(300000);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
|
@ -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;
|
||||
@ -32,7 +29,9 @@ namespace SagaManager.Services
|
||||
|
||||
foreach (var orderId in orderIds)
|
||||
{
|
||||
Publish(orderId);
|
||||
var confirmGracePeriodEvent = new ConfirmGracePeriodCommandMsg(orderId);
|
||||
|
||||
_sagaManagerIntegrationEventService.PublishThroughEventBus(confirmGracePeriodEvent);
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,13 +59,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