From 2ade7a87e8ba2a3455175855bb7d571533d071c8 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Mon, 15 May 2017 19:15:33 +0200 Subject: [PATCH] Add Integration Events and Pay Order Command msg handler --- docker-compose.vs.debug.yml | 2 +- docker-compose.yml | 2 +- .../PayOrderCommandMsgHandler.cs | 19 ++++++++++++ .../Commands/PayOrderCommandMsg.cs | 11 +++++++ .../OrderPaymentFailedIntegrationEvent.cs | 11 +++++++ .../OrderPaymentSuccededIntegrationEvent.cs | 11 +++++++ .../Payment/Payment.API/Payment.API.csproj | 5 ++++ src/Services/Payment/Payment.API/Startup.cs | 30 +++++++++++++++++++ 8 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 src/Services/Payment/Payment.API/IntegrationCommands/CommandHandlers/PayOrderCommandMsgHandler.cs create mode 100644 src/Services/Payment/Payment.API/IntegrationCommands/Commands/PayOrderCommandMsg.cs create mode 100644 src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent.cs create mode 100644 src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs diff --git a/docker-compose.vs.debug.yml b/docker-compose.vs.debug.yml index eaa0b6f35..f6187dee2 100644 --- a/docker-compose.vs.debug.yml +++ b/docker-compose.vs.debug.yml @@ -107,7 +107,7 @@ services: - "com.microsoft.visualstudio.targetoperatingsystem=linux" payment.api: - image: payment.api:dev + image: eshop/payment.api:dev build: args: source: ${DOCKER_BUILD_SOURCE} diff --git a/docker-compose.yml b/docker-compose.yml index cc134a005..75177c5db 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -86,7 +86,7 @@ services: dockerfile: Dockerfile payment.api: - image: payment.api + image: eshop/payment.api build: context: ./src/Services/Payment/Payment.API dockerfile: Dockerfile diff --git a/src/Services/Payment/Payment.API/IntegrationCommands/CommandHandlers/PayOrderCommandMsgHandler.cs b/src/Services/Payment/Payment.API/IntegrationCommands/CommandHandlers/PayOrderCommandMsgHandler.cs new file mode 100644 index 000000000..0a41c133c --- /dev/null +++ b/src/Services/Payment/Payment.API/IntegrationCommands/CommandHandlers/PayOrderCommandMsgHandler.cs @@ -0,0 +1,19 @@ +namespace Payment.API.IntegrationCommands.CommandHandlers +{ + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; + using Payment.API.IntegrationCommands.Commands; + using System.Threading.Tasks; + using System; + + public class PayOrderCommandMsgHandler : IIntegrationEventHandler + { + public PayOrderCommandMsgHandler() + { + } + + public async Task Handle(PayOrderCommandMsg @event) + { + throw new NotImplementedException(); + } + } +} diff --git a/src/Services/Payment/Payment.API/IntegrationCommands/Commands/PayOrderCommandMsg.cs b/src/Services/Payment/Payment.API/IntegrationCommands/Commands/PayOrderCommandMsg.cs new file mode 100644 index 000000000..360f40606 --- /dev/null +++ b/src/Services/Payment/Payment.API/IntegrationCommands/Commands/PayOrderCommandMsg.cs @@ -0,0 +1,11 @@ +namespace Payment.API.IntegrationCommands.Commands +{ + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; + + public class PayOrderCommandMsg : IntegrationEvent + { + public int OrderId { get; } + + public PayOrderCommandMsg(int orderId) => OrderId = orderId; + } +} \ No newline at end of file diff --git a/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent.cs b/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent.cs new file mode 100644 index 000000000..d51c518c4 --- /dev/null +++ b/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent.cs @@ -0,0 +1,11 @@ +namespace Payment.API.IntegrationEvents.Events +{ + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; + + public class OrderPaymentFailedIntegrationEvent : IntegrationEvent + { + public int OrderId { get; } + + public OrderPaymentFailedIntegrationEvent(int orderId) => OrderId = orderId; + } +} \ No newline at end of file diff --git a/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs b/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs new file mode 100644 index 000000000..d672ff9d4 --- /dev/null +++ b/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs @@ -0,0 +1,11 @@ +namespace Payment.API.IntegrationEvents.Events +{ + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; + + public class OrderPaymentSuccededIntegrationEvent : IntegrationEvent + { + public int OrderId { get; } + + public OrderPaymentSuccededIntegrationEvent(int orderId) => OrderId = orderId; + } +} \ No newline at end of file diff --git a/src/Services/Payment/Payment.API/Payment.API.csproj b/src/Services/Payment/Payment.API/Payment.API.csproj index 147cd2e3c..7f741fa53 100644 --- a/src/Services/Payment/Payment.API/Payment.API.csproj +++ b/src/Services/Payment/Payment.API/Payment.API.csproj @@ -25,5 +25,10 @@ + + + + + diff --git a/src/Services/Payment/Payment.API/Startup.cs b/src/Services/Payment/Payment.API/Startup.cs index c5160a81d..ac970d82d 100644 --- a/src/Services/Payment/Payment.API/Startup.cs +++ b/src/Services/Payment/Payment.API/Startup.cs @@ -6,6 +6,11 @@ using Microsoft.Extensions.Configuration; 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; namespace Payment.API { @@ -29,6 +34,21 @@ namespace Payment.API // Add framework services. services.AddMvc(); + services.AddSingleton(sp => + { + var logger = sp.GetRequiredService>(); + + var factory = new ConnectionFactory() + { + HostName = Configuration["EventBusConnection"] + }; + + return new DefaultRabbitMQPersistentConnection(factory, logger); + }); + + services.AddSingleton(); + services.AddSingleton(); + services.AddSwaggerGen(); services.ConfigureSwaggerGen(options => { @@ -47,6 +67,8 @@ namespace Payment.API return new AutofacServiceProvider(container.Build()); } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { @@ -57,6 +79,14 @@ namespace Payment.API app.UseSwagger() .UseSwaggerUi(); + + ConfigureEventBus(app); + } + + private void ConfigureEventBus(IApplicationBuilder app) + { + var eventBus = app.ApplicationServices.GetRequiredService(); + eventBus.Subscribe>(); } } }