diff --git a/wolverine/a-frame-architecture/FinancialApi.Application/FinancialApi.Application.csproj b/wolverine/a-frame-architecture/FinancialApi.Application/FinancialApi.Application.csproj index cc8c37d..94e1287 100644 --- a/wolverine/a-frame-architecture/FinancialApi.Application/FinancialApi.Application.csproj +++ b/wolverine/a-frame-architecture/FinancialApi.Application/FinancialApi.Application.csproj @@ -11,6 +11,9 @@ + + ..\..\..\..\..\..\.nuget\packages\microsoft.extensions.logging.abstractions\10.0.9\lib\net10.0\Microsoft.Extensions.Logging.Abstractions.dll + ..\..\..\..\..\..\.nuget\packages\wolverinefx\6.18.0\lib\net10.0\Wolverine.dll diff --git a/wolverine/a-frame-architecture/FinancialApi.Application/FinancialEventHandler.cs b/wolverine/a-frame-architecture/FinancialApi.Application/FinancialEventHandler.cs new file mode 100644 index 0000000..ed11aa3 --- /dev/null +++ b/wolverine/a-frame-architecture/FinancialApi.Application/FinancialEventHandler.cs @@ -0,0 +1,42 @@ +using Microsoft.Extensions.Logging; + +namespace FinancialApi.Application; + +public static class FinancialEventHandler +{ + public static void Handle( + FeeAppliedEvent @event, + IEventTracker store, + ILogger logger) + { + var message = $"FeeApplied: ${@event.Amount} moved from Account {@event.AccountId} to Account 99999."; + + logger.LogInformation("BACKGROUND EVENT FIRED: {Message}", message); + + // Save to our UI bucket + store.Add(message); + } + + public static void Handle( + FundsTransferredEvent @event, + IEventTracker store, + ILogger logger) + { + var message = $"FundsTransferred: ${@event.Amount} moved from Account {@event.SourceAccountId} to Account {@event.DestinationAccountId}."; + + logger.LogInformation("BACKGROUND EVENT FIRED: {Message}", message); + + // Save to our UI bucket + store.Add(message); + } + + public static void Handle( + JournalReversedEvent @event, + IEventTracker store, + ILogger logger) + { + var message = $"JournalReversed: Original Entry {@event.OriginalJournalId} was reversed by Entry {@event.ReversalJournalId}."; + logger.LogInformation("BACKGROUND EVENT FIRED: {Message}", message); + store.Add(message); + } +} \ No newline at end of file diff --git a/wolverine/a-frame-architecture/FinancialApi.Application/IEventTracker.cs b/wolverine/a-frame-architecture/FinancialApi.Application/IEventTracker.cs new file mode 100644 index 0000000..14bdab6 --- /dev/null +++ b/wolverine/a-frame-architecture/FinancialApi.Application/IEventTracker.cs @@ -0,0 +1,6 @@ +namespace FinancialApi.Application; + +public interface IEventTracker +{ + void Add(string eventMessage); +} \ No newline at end of file diff --git a/wolverine/a-frame-architecture/FinancialApi/DemoEventStore.cs b/wolverine/a-frame-architecture/FinancialApi/DemoEventStore.cs new file mode 100644 index 0000000..772ba53 --- /dev/null +++ b/wolverine/a-frame-architecture/FinancialApi/DemoEventStore.cs @@ -0,0 +1,13 @@ +using FinancialApi.Application; + +namespace FinancialApi; + +public class DemoEventStore : IEventTracker +{ + public List CapturedEvents { get; } = new(); + + public void Add(string eventMessage) + { + CapturedEvents.Add($"[{DateTimeOffset.UtcNow:HH:mm:ss}] {eventMessage}"); + } +} \ No newline at end of file diff --git a/wolverine/a-frame-architecture/FinancialApi/FinancialApi.http b/wolverine/a-frame-architecture/FinancialApi/FinancialApi.http index b16c556..840a3a5 100644 --- a/wolverine/a-frame-architecture/FinancialApi/FinancialApi.http +++ b/wolverine/a-frame-architecture/FinancialApi/FinancialApi.http @@ -31,6 +31,11 @@ POST {{FinancialApi_HostAddress}}/journal/reverse Content-Type: application/json { - "originalJournalId": "3E829F92-E2FA-4FCD-9B7B-E1B5AAC96FF1", + "originalJournalId": "1443B0FC-F679-4445-A910-CC333F0925B7", "reason": "Incorrect Account Fee" } + +### Events + +GET {{FinancialApi_HostAddress}}/events +Content-Type: application/json \ No newline at end of file diff --git a/wolverine/a-frame-architecture/FinancialApi/Program.cs b/wolverine/a-frame-architecture/FinancialApi/Program.cs index d4f6700..a4aee3a 100644 --- a/wolverine/a-frame-architecture/FinancialApi/Program.cs +++ b/wolverine/a-frame-architecture/FinancialApi/Program.cs @@ -22,6 +22,7 @@ builder.Services.AddDbContext(options => builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddSingleton(); // Add services to the container. // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi @@ -31,6 +32,7 @@ builder.AddServiceDefaults(); builder.Host.UseWolverine(opts => { + opts.Discovery.IncludeAssembly(typeof(DemoEventStore).Assembly); opts.Discovery.IncludeAssembly(typeof(ApplyFeeHandler).Assembly); opts.PersistMessagesWithSqlite(connectionString); opts.UseEntityFrameworkCoreTransactions(); @@ -74,4 +76,13 @@ app.MapPost("/journal/reverse", async (ReverseJournalCommand cmd, IMessageBus bu return Results.Accepted(); }); +app.MapGet("/events", (IEventTracker tracker) => +{ + if (tracker is DemoEventStore store) + { + return Results.Ok(store.CapturedEvents); + } + return Results.NotFound(); +}); + app.Run(); \ No newline at end of file