Re-layout source code to correct namespaces (handlers, commands, etc.)

This commit is contained in:
2026-07-17 16:57:55 +02:00
parent 09a89a34c4
commit 69cb05f389
25 changed files with 89 additions and 85 deletions
@@ -1,4 +1,7 @@
using FinancialApi.Domain;
using System.Security.Cryptography;
using FinancialApi.Application.Commands;
using FinancialApi.Application.Handlers;
using FinancialApi.Domain;
using FluentAssertions;
using FluentAssertions.Execution;
@@ -12,19 +15,18 @@ public class ApplyFeeHandlerTests
// Arrange
var command = new ApplyFeeCommand(1, 100);
var account = new Account(1, 100, "Debit");
var destAccount = new Account(99999, 100, "Credit");
var pair = new FeePair(account, destAccount, TimeProvider.System.GetUtcNow());
var revenueAccount = new Account(99999, 0, "Credit");
var pair = new FeePair(account, revenueAccount, TimeProvider.System.GetUtcNow());
// Act
var intents = ApplyFeeHandler.Handle(command, pair);
// Assert
using var _ = new AssertionScope();
intents.CustomerWrite.Entity.Balance.Should().Be(0);
intents.RevenueWrite.Entity.Balance.Should().Be(200);
intents.RevenueWrite.Entity.Balance.Should().Be(200);
intents.JournalWrite.Entity.Lines.Count.Should().Be(2);
intents.CustomerWrite.Entity.Balance.Should().Be(account.Balance - command.Amount);
intents.RevenueWrite.Entity.Balance.Should().Be(revenueAccount.Balance + command.Amount);
intents.JournalWrite.Entity.Lines.Count.Should().BeGreaterThanOrEqualTo(2);
intents.Event.AccountId.Should().Be(account.Id);
intents.Event.Amount.Should().Be(100);
intents.Event.Amount.Should().Be(command.Amount);
}
}
@@ -0,0 +1,3 @@
namespace FinancialApi.Application.Commands;
public record ApplyFeeCommand(int AccountId, decimal Amount);
@@ -0,0 +1,3 @@
namespace FinancialApi.Application.Commands;
public record ReverseJournalCommand(Guid OriginalJournalId, string Reason);
@@ -0,0 +1,3 @@
namespace FinancialApi.Application.Commands;
public abstract record TransferFundsCommand(int SourceAccountId, int DestinationAccountId, decimal Amount);
@@ -0,0 +1,3 @@
namespace FinancialApi.Application.Events;
public record FeeAppliedEvent(int AccountId, decimal Amount);
@@ -0,0 +1,3 @@
namespace FinancialApi.Application.Events;
public record FundsTransferredEvent(int SourceAccountId, int DestinationAccountId, decimal Amount);
@@ -1,4 +1,4 @@
namespace FinancialApi.Application;
namespace FinancialApi.Application.Events;
public interface IEventTracker
{
@@ -0,0 +1,3 @@
namespace FinancialApi.Application.Events;
public record JournalReversedEvent(Guid OriginalJournalId, Guid ReversalJournalId);
@@ -0,0 +1,5 @@
using FinancialApi.Domain;
namespace FinancialApi.Application;
public record FeePair(Account Source, Account Destination, DateTimeOffset TimeStamp);
@@ -1,16 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FinancialApi.Application.Commands;
using FinancialApi.Application.Events;
using FinancialApi.Application.Queries;
using FinancialApi.Domain;
using Wolverine.Persistence;
namespace FinancialApi.Application;
public record ApplyFeeCommand(int AccountId, decimal Amount);
public record FeeAppliedEvent(int AccountId, decimal Amount);
public record FeePair(Account Source, Account Destination, DateTimeOffset TimeStamp);
namespace FinancialApi.Application.Handlers;
public static class ApplyFeeHandler
{
@@ -41,11 +35,15 @@ public static class ApplyFeeHandler
{
var updatedSource = pair.Source.ApplyPosting(cmd.Amount, EntryType.Debit);
var updatedRevenue = pair.Destination.ApplyPosting(cmd.Amount, EntryType.Credit);
var serviceFee = 0.5m;
var lines = new List<JournalLine>
{
new(updatedSource.Id, cmd.Amount, EntryType.Debit),
new(updatedRevenue.Id, cmd.Amount, EntryType.Credit)
new(updatedRevenue.Id, cmd.Amount - serviceFee, EntryType.Credit),
new(updatedRevenue.Id, serviceFee, EntryType.Credit)
};
var journalEntry = BalancedJournal.Create(Guid.NewGuid(),
$"Service Fee Applied: {cmd.Amount} to Account {cmd.AccountId}",
pair.TimeStamp, lines);
@@ -1,6 +1,7 @@
using FinancialApi.Application.Events;
using Microsoft.Extensions.Logging;
namespace FinancialApi.Application;
namespace FinancialApi.Application.Handlers;
public static class FinancialEventHandler
{
@@ -1,26 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FinancialApi.Application.Commands;
using FinancialApi.Application.Events;
using FinancialApi.Application.Queries;
using FinancialApi.Domain;
using Wolverine.Persistence;
namespace FinancialApi.Application;
public record ReverseJournalCommand(Guid OriginalJournalId, string Reason);
public record JournalReversedEvent(Guid OriginalJournalId, Guid ReversalJournalId);
namespace FinancialApi.Application.Handlers;
public record ReversalData(
JournalEntry OriginalJournalEntry,
IReadOnlyList<Account> AffectedAccounts,
DateTimeOffset TimeStamp);
public interface IReversalQuery
{
Task<ReversalData?> GetReversalDataAsync(Guid journalEntryId);
}
public static class ReverseJournalHandler
{
public static async Task<ReversalData?> LoadAsync(ReverseJournalCommand cmd, IReversalQuery query,
@@ -33,7 +23,8 @@ public static class ReverseJournalHandler
public static (
IStorageAction<JournalEntry> JournalWrite,
UnitOfWork<Account> AccountWrites,
JournalReversedEvent Event) Handle(ReverseJournalCommand cmd, ReversalData data)
JournalReversedEvent Event)
Handle(ReverseJournalCommand cmd, ReversalData data)
{
var reversalJournal =
BalancedJournal.CreateReversal(data.OriginalJournalEntry, $"Reversal: {cmd.Reason}", data.TimeStamp);
@@ -1,21 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FinancialApi.Application.Commands;
using FinancialApi.Application.Events;
using FinancialApi.Application.Queries;
using FinancialApi.Domain;
using Wolverine.Persistence;
namespace FinancialApi.Application;
public interface IAccountQuery
{
Task<Account?> FindByIdAsync(int id);
}
public record TransferFundsCommand(int SourceAccountId, int DestinationAccountId, decimal Amount);
public record FundsTransferredEvent(int SourceAccountId, int DestinationAccountId, decimal Amount);
public record TransferPair(Account Source, Account Destination, DateTimeOffset TimeStamp);
namespace FinancialApi.Application.Handlers;
public static class TransferFundsHandler
{
@@ -0,0 +1,8 @@
using FinancialApi.Domain;
namespace FinancialApi.Application.Queries;
public interface IAccountQuery
{
Task<Account?> FindByIdAsync(int id);
}
@@ -0,0 +1,8 @@
using FinancialApi.Application.Handlers;
namespace FinancialApi.Application.Queries;
public interface IReversalQuery
{
Task<ReversalData?> GetReversalDataAsync(Guid journalEntryId);
}
@@ -0,0 +1,5 @@
using FinancialApi.Domain;
namespace FinancialApi.Application;
public record TransferPair(Account Source, Account Destination, DateTimeOffset TimeStamp);
@@ -1,6 +1,4 @@
using System;
namespace FinancialApi.Domain;
namespace FinancialApi.Domain;
public record Account(int Id, decimal Balance, string AccountType)
{
@@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace FinancialApi.Domain;
public record BalancedJournal
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
namespace FinancialApi.Domain;
public record JournalEntry
@@ -1,5 +1,4 @@
using System.Threading.Tasks;
using FinancialApi.Application;
using FinancialApi.Application.Queries;
using FinancialApi.Domain;
using Microsoft.EntityFrameworkCore;
@@ -1,7 +1,5 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using FinancialApi.Application;
using FinancialApi.Application.Handlers;
using FinancialApi.Application.Queries;
using Microsoft.EntityFrameworkCore;
namespace FinancialApi.Infrastructure;
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using FinancialApi.Application;
using FinancialApi.Application.Events;
namespace FinancialApi;
@@ -1,8 +1,4 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace FinancialApi;
@@ -1,8 +1,5 @@
@FinancialApi_HostAddress = https://localhost:7010
GET {{FinancialApi_HostAddress}}/weatherforecast/
Accept: application/json
### Apply Fee to an Account
POST {{FinancialApi_HostAddress}}/accounts/apply-fee
@@ -11,7 +8,7 @@ Content-Type: application/json
{
"accountId": 1,
"amount": 500.0
"amount": 50.0
}
### Transfer Between Accounts
@@ -31,7 +28,7 @@ POST {{FinancialApi_HostAddress}}/journal/reverse
Content-Type: application/json
{
"originalJournalId": "1443B0FC-F679-4445-A910-CC333F0925B7",
"originalJournalId": "FD2BFD8E-7C85-4CAD-A12B-CCBCD7C12B4A",
"reason": "Incorrect Account Fee"
}
@@ -1,13 +1,12 @@
using FinancialApi;
using FinancialApi.Application;
using FinancialApi.Application.Commands;
using FinancialApi.Application.Events;
using FinancialApi.Application.Handlers;
using FinancialApi.Application.Queries;
using FinancialApi.Infrastructure;
using FinancialApi.ServiceDefaults;
using JasperFx.Resources;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Wolverine;
using Wolverine.EntityFrameworkCore;
using Wolverine.Sqlite;
@@ -85,6 +84,7 @@ app.MapGet("/events", (IEventTracker tracker) =>
{
return Results.Ok(store.CapturedEvents);
}
return Results.NotFound();
});