diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 41faa3580..8c59298ff 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -59,7 +59,6 @@ services: tenantmanager: environment: - ConnectionString=${ESHOP_AZURE_CATALOG_DB:-Server=sql.data;Database=Microsoft.eShopOnContainers.Services.TenantManagerDb;User Id=sa;Password=Pass@word} - ports: - "5115:80" diff --git a/src/BuildingBlocks/EventBus/EventBus/Abstractions/AbstractIntegrationEventHandler.cs b/src/BuildingBlocks/EventBus/EventBus/Abstractions/AbstractIntegrationEventHandler.cs new file mode 100644 index 000000000..c67e082cf --- /dev/null +++ b/src/BuildingBlocks/EventBus/EventBus/Abstractions/AbstractIntegrationEventHandler.cs @@ -0,0 +1,65 @@ +using System.Threading.Tasks; +using System; +using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; +using System.Net; +using System.IO; +using System.Net.Http; +using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; +using Microsoft.Extensions.Logging; + +namespace Ordering.API.Application.IntegrationEvents.EventHandling +{ + public abstract class AbstractIntegrationEventHandler + { + private static String url = @"http://tenantmanager/"; + private readonly IEventBus _eventBus; + + public async Task CheckIfCustomised(IntegrationEvent @event) + { + Boolean result = Get(@event); + if (result) + { + CustomisationEvent customisationEvent = new CustomisationEvent(1, @event); + try + { + //_logger.LogInformation("----- Publishing integration event: {IntegrationEventId} from {AppName} - ({@IntegrationEvent})", eventMessage.Id, Program.AppName, eventMessage); + + _eventBus.Publish(customisationEvent); + } + catch (Exception ex) + { + //_logger.LogError(ex, "ERROR Publishing integration event: {IntegrationEventId} from {AppName}", eventMessage.Id, Program.AppName); + + throw; + } + } + + return result; + } + + private Boolean Get(IntegrationEvent @event) + { + //TODO return true/false + Console.WriteLine("Making API Call..."); + using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate })) + { + client.BaseAddress = new Uri(url); + try + { + HttpResponseMessage response = client.GetAsync("api/tenants").Result; + response.EnsureSuccessStatusCode(); + string result = response.Content.ReadAsStringAsync().Result; + Console.WriteLine("Result: " + result); + + } + catch(Exception e) + { + Console.WriteLine(e); + } + + } + return false; + } + } + +} diff --git a/src/BuildingBlocks/EventBus/EventBus/EventBus.csproj b/src/BuildingBlocks/EventBus/EventBus/EventBus.csproj index 9704f6ff5..9dc11b510 100644 --- a/src/BuildingBlocks/EventBus/EventBus/EventBus.csproj +++ b/src/BuildingBlocks/EventBus/EventBus/EventBus.csproj @@ -9,4 +9,10 @@ + + + ..\..\..\..\..\..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.logging.abstractions\2.2.0\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll + + + \ No newline at end of file diff --git a/src/BuildingBlocks/EventBus/EventBus/Events/CustomisationEvent.cs b/src/BuildingBlocks/EventBus/EventBus/Events/CustomisationEvent.cs new file mode 100644 index 000000000..38b1564a5 --- /dev/null +++ b/src/BuildingBlocks/EventBus/EventBus/Events/CustomisationEvent.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events +{ + public class CustomisationEvent : IntegrationEvent + { + public CustomisationEvent(int tenantId, IntegrationEvent @event) + { + TenantId = tenantId; + this.@event = @event; + } + + public int TenantId { get; set; } + public IntegrationEvent @event { get; set; } + } +} diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs index a1452b23c..5782930f3 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs @@ -12,7 +12,7 @@ using System; namespace Ordering.API.Application.IntegrationEvents.EventHandling { - public class UserCheckoutAcceptedIntegrationEventHandler : IIntegrationEventHandler + public class UserCheckoutAcceptedIntegrationEventHandler : AbstractIntegrationEventHandler, IIntegrationEventHandler { private readonly IMediator _mediator; private readonly ILogger _logger; @@ -40,41 +40,45 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling { _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event); - var result = false; - - if (@event.RequestId != Guid.Empty) + var customised = await CheckIfCustomised(@event); + if (!customised) { - using (LogContext.PushProperty("IdentifiedCommandId", @event.RequestId)) + var result = false; + + if (@event.RequestId != Guid.Empty) { - var createOrderCommand = new CreateOrderCommand(@event.Basket.Items, @event.UserId, @event.UserName, @event.City, @event.Street, - @event.State, @event.Country, @event.ZipCode, - @event.CardNumber, @event.CardHolderName, @event.CardExpiration, - @event.CardSecurityNumber, @event.CardTypeId); + using (LogContext.PushProperty("IdentifiedCommandId", @event.RequestId)) + { + var createOrderCommand = new CreateOrderCommand(@event.Basket.Items, @event.UserId, @event.UserName, @event.City, @event.Street, + @event.State, @event.Country, @event.ZipCode, + @event.CardNumber, @event.CardHolderName, @event.CardExpiration, + @event.CardSecurityNumber, @event.CardTypeId); - var requestCreateOrder = new IdentifiedCommand(createOrderCommand, @event.RequestId); + var requestCreateOrder = new IdentifiedCommand(createOrderCommand, @event.RequestId); - _logger.LogInformation( - "----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})", - requestCreateOrder.GetGenericTypeName(), - nameof(requestCreateOrder.Id), - requestCreateOrder.Id, - requestCreateOrder); + _logger.LogInformation( + "----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})", + requestCreateOrder.GetGenericTypeName(), + nameof(requestCreateOrder.Id), + requestCreateOrder.Id, + requestCreateOrder); - result = await _mediator.Send(requestCreateOrder); + result = await _mediator.Send(requestCreateOrder); - if (result) - { - _logger.LogInformation("----- CreateOrderCommand suceeded - RequestId: {RequestId}", @event.RequestId); - } - else - { - _logger.LogWarning("CreateOrderCommand failed - RequestId: {RequestId}", @event.RequestId); + if (result) + { + _logger.LogInformation("----- CreateOrderCommand suceeded - RequestId: {RequestId}", @event.RequestId); + } + else + { + _logger.LogWarning("CreateOrderCommand failed - RequestId: {RequestId}", @event.RequestId); + } } } - } - else - { - _logger.LogWarning("Invalid IntegrationEvent - RequestId is missing - {@IntegrationEvent}", @event); + else + { + _logger.LogWarning("Invalid IntegrationEvent - RequestId is missing - {@IntegrationEvent}", @event); + } } } }