Remove PaymentIntegrationEventService and remove integration command to integration event

This commit is contained in:
Christian Arenas 2017-05-17 19:42:45 +02:00
parent 511bc05cb4
commit cceca92cf9
9 changed files with 67 additions and 77 deletions

View File

@ -1,27 +0,0 @@
namespace Payment.API.IntegrationCommands.CommandHandlers
{
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Payment.API.IntegrationCommands.Commands;
using System.Threading.Tasks;
using Payment.API.IntegrationEvents;
using Payment.API.IntegrationEvents.Events;
public class PayOrderCommandHandler : IIntegrationEventHandler<PayOrderCommand>
{
private readonly IPaymentIntegrationEventService _paymentIntegrationEventService;
public PayOrderCommandHandler(IPaymentIntegrationEventService paymentIntegrationEventService)
=> _paymentIntegrationEventService = paymentIntegrationEventService;
public async Task Handle(PayOrderCommand @event)
{
//PAYMENT SUCCESSED
var orderPaymentSuccededIntegrationEvent = new OrderPaymentSuccededIntegrationEvent(@event.OrderId);
_paymentIntegrationEventService.PublishThroughEventBus(orderPaymentSuccededIntegrationEvent);
//PAYMENT FAILED
//var orderPaymentFailedIntegrationEvent = new OrderPaymentFailedIntegrationEvent(@event.OrderId);
//_paymentIntegrationEventService.PublishThroughEventBus(orderPaymentFailedIntegrationEvent);
}
}
}

View File

@ -1,11 +0,0 @@
namespace Payment.API.IntegrationCommands.Commands
{
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
public class PayOrderCommand : IntegrationEvent
{
public int OrderId { get; }
public PayOrderCommand(int orderId) => OrderId = orderId;
}
}

View File

@ -0,0 +1,37 @@
namespace Payment.API.IntegrationEvents.EventHandling
{
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using System.Threading.Tasks;
using Payment.API.IntegrationEvents.Events;
using Microsoft.Extensions.Options;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
public class OrderStatusChangedToStockConfirmedIntegrationEventHandler :
IIntegrationEventHandler<OrderStatusChangedToStockConfirmedIntegrationEvent>
{
private readonly IEventBus _eventBus;
private readonly PaymentSettings _settings;
public OrderStatusChangedToStockConfirmedIntegrationEventHandler(IEventBus eventBus,
IOptionsSnapshot<PaymentSettings> settings)
{
_eventBus = eventBus;
_settings = settings.Value;
}
public async Task Handle(OrderStatusChangedToStockConfirmedIntegrationEvent @event)
{
IntegrationEvent orderPaymentIntegrationEvent;
if(_settings.SuccessPayment)
{
orderPaymentIntegrationEvent = new OrderPaymentSuccededIntegrationEvent(@event.OrderId);
}
else
{
orderPaymentIntegrationEvent = new OrderPaymentFailedIntegrationEvent(@event.OrderId);
}
_eventBus.Publish(orderPaymentIntegrationEvent);
}
}
}

View File

@ -0,0 +1,12 @@
namespace Payment.API.IntegrationEvents.Events
{
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
public class OrderStatusChangedToStockConfirmedIntegrationEvent : IntegrationEvent
{
public int OrderId { get; }
public OrderStatusChangedToStockConfirmedIntegrationEvent(int orderId)
=> OrderId = orderId;
}
}

View File

@ -1,9 +0,0 @@
namespace Payment.API.IntegrationEvents
{
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
public interface IPaymentIntegrationEventService
{
void PublishThroughEventBus(IntegrationEvent evt);
}
}

View File

@ -1,21 +0,0 @@
namespace Payment.API.IntegrationEvents
{
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using System;
public class PaymentIntegrationEventService : IPaymentIntegrationEventService
{
private readonly IEventBus _eventBus;
public PaymentIntegrationEventService(IEventBus eventBus)
{
_eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
}
public void PublishThroughEventBus(IntegrationEvent evt)
{
_eventBus.Publish(evt); ;
}
}
}

View File

@ -0,0 +1,8 @@
namespace Payment.API
{
public class PaymentSettings
{
public bool SuccessPayment { get; set; }
public string EventBusConnection { get; set; }
}
}

View File

@ -7,12 +7,11 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
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;
using Payment.API.IntegrationEvents.Events;
using Payment.API.IntegrationEvents.EventHandling;
namespace Payment.API
{
@ -35,8 +34,7 @@ namespace Payment.API
{
// Add framework services.
services.AddMvc();
services.AddTransient<IPaymentIntegrationEventService, PaymentIntegrationEventService>();
services.Configure<PaymentSettings>(Configuration);
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
{
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
@ -88,13 +86,15 @@ namespace Payment.API
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
services.AddTransient<IIntegrationEventHandler<PayOrderCommand>, PayOrderCommandHandler>();
services.AddTransient<IIntegrationEventHandler<OrderStatusChangedToStockConfirmedIntegrationEvent>,
OrderStatusChangedToStockConfirmedIntegrationEventHandler>();
}
private void ConfigureEventBus(IApplicationBuilder app)
{
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
eventBus.Subscribe<PayOrderCommand, IIntegrationEventHandler<PayOrderCommand>>();
eventBus.Subscribe<OrderStatusChangedToStockConfirmedIntegrationEvent,
IIntegrationEventHandler<OrderStatusChangedToStockConfirmedIntegrationEvent>>();
}
}
}
}

View File

@ -4,5 +4,6 @@
"LogLevel": {
"Default": "Warning"
}
}
},
"SuccessPayment": "true"
}