diff --git a/wolverine/a-frame-architecture/FinancialApi.Application/ReverseJournalHandler.cs b/wolverine/a-frame-architecture/FinancialApi.Application/ReverseJournalHandler.cs index de397f2..376cb8c 100644 --- a/wolverine/a-frame-architecture/FinancialApi.Application/ReverseJournalHandler.cs +++ b/wolverine/a-frame-architecture/FinancialApi.Application/ReverseJournalHandler.cs @@ -19,21 +19,16 @@ public static class ReverseJournalHandler public static async Task LoadAsync(ReverseJournalCommand cmd, IReversalQuery query) { var data = await query.GetReversalDataAsync(cmd.OriginalJournalId); - if (data == null) - { - throw new InvalidOperationException($"Journal Entry {cmd.OriginalJournalId} does not exist"); - } - - return data; + return data ?? throw new InvalidOperationException($"Journal Entry {cmd.OriginalJournalId} does not exist"); } public static ( - IStorageAction ReversalWrite, - IStorageAction[] AccountWrites, + IStorageAction JournalWrite, + UnitOfWork AccountWrites, JournalReversedEvent Event) Handle(ReverseJournalCommand cmd, ReversalData data) { var reversalJournal = - BalancedJournal.CreateReversal(data.OriginalJournalEntry, cmd.Reason, DateTimeOffset.UtcNow); + BalancedJournal.CreateReversal(data.OriginalJournalEntry, $"Reversal: {cmd.Reason}", DateTimeOffset.UtcNow); var accountState = data.AffectedAccounts.ToDictionary(a => a.Id); reversalJournal.Entry.Lines.ForEach(l => @@ -41,11 +36,14 @@ public static class ReverseJournalHandler var currentAccount = accountState[l.AccountId]; accountState[l.AccountId] = currentAccount.ApplyPosting(l.Amount, l.Type); }); - - var accountWrites = accountState.Values.Select(a => (IStorageAction)Storage.Update(a)).ToArray(); + var accountUow = new UnitOfWork(); + foreach (var account in accountState.Values) + { + accountUow.Update(account); + } return ( Storage.Insert(reversalJournal.Entry), - accountWrites, + accountUow, new JournalReversedEvent(cmd.OriginalJournalId, reversalJournal.Entry.Id) ); } diff --git a/wolverine/a-frame-architecture/FinancialApi/DomainExceptionHandler.cs b/wolverine/a-frame-architecture/FinancialApi/DomainExceptionHandler.cs index 305b82c..c7bdd97 100644 --- a/wolverine/a-frame-architecture/FinancialApi/DomainExceptionHandler.cs +++ b/wolverine/a-frame-architecture/FinancialApi/DomainExceptionHandler.cs @@ -10,23 +10,23 @@ public class DomainExceptionHandler : IExceptionHandler Exception exception, CancellationToken cancellationToken) { - // Only intercept our specific domain/validation exceptions - if (exception is InvalidOperationException or ArgumentException) + if (exception is not (InvalidOperationException or ArgumentException)) { - var problemDetails = new ProblemDetails - { - Status = StatusCodes.Status400BadRequest, - Title = "Domain Validation Error", - Detail = exception.Message, - Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.1" - }; - - httpContext.Response.StatusCode = problemDetails.Status.Value; - await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken); - - return true; + return false; } - return false; + var problemDetails = new ProblemDetails + { + Status = StatusCodes.Status400BadRequest, + Title = "Domain Validation Error", + Detail = exception.Message, + Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.1" + }; + + httpContext.Response.StatusCode = problemDetails.Status.Value; + await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken); + + return true; + } } \ 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 9f6ca3a..b16c556 100644 --- a/wolverine/a-frame-architecture/FinancialApi/FinancialApi.http +++ b/wolverine/a-frame-architecture/FinancialApi/FinancialApi.http @@ -22,7 +22,7 @@ Content-Type: application/json { "sourceAccountId": 1, "destinationAccountId": 2, - "amount": 3000.00 + "amount": 1000.00 } ### Reverse a Journal Entry @@ -31,6 +31,6 @@ POST {{FinancialApi_HostAddress}}/journal/reverse Content-Type: application/json { - "originalJournalId": "AD4FC011-9979-4364-A0D4-2CCD922ECB5E", - "reason": "Incorrect Account" + "originalJournalId": "3E829F92-E2FA-4FCD-9B7B-E1B5AAC96FF1", + "reason": "Incorrect Account Fee" } diff --git a/wolverine/a-frame-architecture/FinancialApi/Program.cs b/wolverine/a-frame-architecture/FinancialApi/Program.cs index 2faca48..d4f6700 100644 --- a/wolverine/a-frame-architecture/FinancialApi/Program.cs +++ b/wolverine/a-frame-architecture/FinancialApi/Program.cs @@ -58,20 +58,20 @@ app.UseHttpsRedirection(); app.MapPost("/accounts/apply-fee", async (ApplyFeeCommand cmd, IMessageBus bus) => { - await bus.InvokeAsync(cmd); + await bus.InvokeAsync(cmd); return Results.Accepted(); }); app.MapPost("/accounts/transfer", async (TransferFundsCommand cmd, IMessageBus bus) => { - await bus.InvokeAsync(cmd); + await bus.InvokeAsync(cmd); return Results.Accepted(); }); app.MapPost("/journal/reverse", async (ReverseJournalCommand cmd, IMessageBus bus) => { - await bus.InvokeAsync(cmd); + await bus.InvokeAsync(cmd); return Results.Accepted(); }); -app.Run(); +app.Run(); \ No newline at end of file