Browse Source

Work in progress

pull/1240/head
espent1004 5 years ago
parent
commit
50bf07c8c0
5 changed files with 121 additions and 29 deletions
  1. +0
    -1
      docker-compose.override.yml
  2. +65
    -0
      src/BuildingBlocks/EventBus/EventBus/Abstractions/AbstractIntegrationEventHandler.cs
  3. +6
    -0
      src/BuildingBlocks/EventBus/EventBus/EventBus.csproj
  4. +18
    -0
      src/BuildingBlocks/EventBus/EventBus/Events/CustomisationEvent.cs
  5. +32
    -28
      src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs

+ 0
- 1
docker-compose.override.yml View File

@ -59,7 +59,6 @@ services:
tenantmanager: tenantmanager:
environment: environment:
- ConnectionString=${ESHOP_AZURE_CATALOG_DB:-Server=sql.data;Database=Microsoft.eShopOnContainers.Services.TenantManagerDb;User Id=sa;Password=Pass@word} - ConnectionString=${ESHOP_AZURE_CATALOG_DB:-Server=sql.data;Database=Microsoft.eShopOnContainers.Services.TenantManagerDb;User Id=sa;Password=Pass@word}
ports: ports:
- "5115:80" - "5115:80"


+ 65
- 0
src/BuildingBlocks/EventBus/EventBus/Abstractions/AbstractIntegrationEventHandler.cs View File

@ -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<IIntegrationEvent>
{
private static String url = @"http://tenantmanager/";
private readonly IEventBus _eventBus;
public async Task<bool> 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;
}
}
}

+ 6
- 0
src/BuildingBlocks/EventBus/EventBus/EventBus.csproj View File

@ -9,4 +9,10 @@
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.Extensions.Logging.Abstractions">
<HintPath>..\..\..\..\..\..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.logging.abstractions\2.2.0\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
</Reference>
</ItemGroup>
</Project> </Project>

+ 18
- 0
src/BuildingBlocks/EventBus/EventBus/Events/CustomisationEvent.cs View File

@ -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; }
}
}

+ 32
- 28
src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs View File

@ -12,7 +12,7 @@ using System;
namespace Ordering.API.Application.IntegrationEvents.EventHandling namespace Ordering.API.Application.IntegrationEvents.EventHandling
{ {
public class UserCheckoutAcceptedIntegrationEventHandler : IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>
public class UserCheckoutAcceptedIntegrationEventHandler : AbstractIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>, IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>
{ {
private readonly IMediator _mediator; private readonly IMediator _mediator;
private readonly ILogger<UserCheckoutAcceptedIntegrationEventHandler> _logger; private readonly ILogger<UserCheckoutAcceptedIntegrationEventHandler> _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); _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, bool>(createOrderCommand, @event.RequestId);
var requestCreateOrder = new IdentifiedCommand<CreateOrderCommand, bool>(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);
}
} }
} }
} }


Loading…
Cancel
Save