Adjust location of classes further.

This commit is contained in:
2026-07-17 17:10:28 +02:00
parent 69cb05f389
commit 29d179a23a
21 changed files with 82 additions and 56 deletions
@@ -1,7 +1,9 @@
using System.Security.Cryptography; using System.Security.Cryptography;
using FinancialApi.Application.Commands; using FinancialApi.Application.Commands;
using FinancialApi.Application.Handlers; using FinancialApi.Application.Handlers;
using FinancialApi.Application.Models;
using FinancialApi.Domain; using FinancialApi.Domain;
using FinancialApi.Domain.Entities;
using FluentAssertions; using FluentAssertions;
using FluentAssertions.Execution; using FluentAssertions.Execution;
@@ -16,7 +18,7 @@ public class ApplyFeeHandlerTests
var command = new ApplyFeeCommand(1, 100); var command = new ApplyFeeCommand(1, 100);
var account = new Account(1, 100, "Debit"); var account = new Account(1, 100, "Debit");
var revenueAccount = new Account(99999, 0, "Credit"); var revenueAccount = new Account(99999, 0, "Credit");
var pair = new FeePair(account, revenueAccount, TimeProvider.System.GetUtcNow()); var pair = new FeeContext(account, revenueAccount, TimeProvider.System.GetUtcNow());
// Act // Act
var intents = ApplyFeeHandler.Handle(command, pair); var intents = ApplyFeeHandler.Handle(command, pair);
@@ -1,5 +0,0 @@
using FinancialApi.Domain;
namespace FinancialApi.Application;
public record FeePair(Account Source, Account Destination, DateTimeOffset TimeStamp);
@@ -1,14 +1,17 @@
using FinancialApi.Application.Commands; using FinancialApi.Application.Commands;
using FinancialApi.Application.Events; using FinancialApi.Application.Events;
using FinancialApi.Application.Queries; using FinancialApi.Application.Interfaces;
using FinancialApi.Application.Models;
using FinancialApi.Domain; using FinancialApi.Domain;
using FinancialApi.Domain.Aggregates;
using FinancialApi.Domain.Entities;
using Wolverine.Persistence; using Wolverine.Persistence;
namespace FinancialApi.Application.Handlers; namespace FinancialApi.Application.Handlers;
public static class ApplyFeeHandler public static class ApplyFeeHandler
{ {
public static async Task<FeePair> LoadAsync( public static async Task<FeeContext> LoadAsync(
ApplyFeeCommand cmd, ApplyFeeCommand cmd,
IAccountQuery query, TimeProvider timeProvider) IAccountQuery query, TimeProvider timeProvider)
{ {
@@ -19,7 +22,7 @@ public static class ApplyFeeHandler
throw new InvalidOperationException($"Cannot process transfer. Account(s) not found."); throw new InvalidOperationException($"Cannot process transfer. Account(s) not found.");
} }
return new FeePair(source, dest, timeProvider.GetUtcNow()); return new FeeContext(source, dest, timeProvider.GetUtcNow());
} }
public static ( public static (
@@ -30,11 +33,11 @@ public static class ApplyFeeHandler
) )
Handle( Handle(
ApplyFeeCommand cmd, ApplyFeeCommand cmd,
FeePair pair FeeContext context
) )
{ {
var updatedSource = pair.Source.ApplyPosting(cmd.Amount, EntryType.Debit); var updatedSource = context.Source.ApplyPosting(cmd.Amount, EntryType.Debit);
var updatedRevenue = pair.Destination.ApplyPosting(cmd.Amount, EntryType.Credit); var updatedRevenue = context.Destination.ApplyPosting(cmd.Amount, EntryType.Credit);
var serviceFee = 0.5m; var serviceFee = 0.5m;
var lines = new List<JournalLine> var lines = new List<JournalLine>
@@ -46,7 +49,7 @@ public static class ApplyFeeHandler
var journalEntry = BalancedJournal.Create(Guid.NewGuid(), var journalEntry = BalancedJournal.Create(Guid.NewGuid(),
$"Service Fee Applied: {cmd.Amount} to Account {cmd.AccountId}", $"Service Fee Applied: {cmd.Amount} to Account {cmd.AccountId}",
pair.TimeStamp, lines); context.TimeStamp, lines);
var @event = new FeeAppliedEvent(cmd.AccountId, cmd.Amount); var @event = new FeeAppliedEvent(cmd.AccountId, cmd.Amount);
@@ -1,19 +1,17 @@
using FinancialApi.Application.Commands; using FinancialApi.Application.Commands;
using FinancialApi.Application.Events; using FinancialApi.Application.Events;
using FinancialApi.Application.Queries; using FinancialApi.Application.Interfaces;
using FinancialApi.Application.Models;
using FinancialApi.Domain; using FinancialApi.Domain;
using FinancialApi.Domain.Aggregates;
using FinancialApi.Domain.Entities;
using Wolverine.Persistence; using Wolverine.Persistence;
namespace FinancialApi.Application.Handlers; namespace FinancialApi.Application.Handlers;
public record ReversalData(
JournalEntry OriginalJournalEntry,
IReadOnlyList<Account> AffectedAccounts,
DateTimeOffset TimeStamp);
public static class ReverseJournalHandler public static class ReverseJournalHandler
{ {
public static async Task<ReversalData?> LoadAsync(ReverseJournalCommand cmd, IReversalQuery query, public static async Task<JournalReversalContext?> LoadAsync(ReverseJournalCommand cmd, IReversalQuery query,
TimeProvider timeProvider) TimeProvider timeProvider)
{ {
var data = await query.GetReversalDataAsync(cmd.OriginalJournalId); var data = await query.GetReversalDataAsync(cmd.OriginalJournalId);
@@ -24,11 +22,11 @@ public static class ReverseJournalHandler
IStorageAction<JournalEntry> JournalWrite, IStorageAction<JournalEntry> JournalWrite,
UnitOfWork<Account> AccountWrites, UnitOfWork<Account> AccountWrites,
JournalReversedEvent Event) JournalReversedEvent Event)
Handle(ReverseJournalCommand cmd, ReversalData data) Handle(ReverseJournalCommand cmd, JournalReversalContext context)
{ {
var reversalJournal = var reversalJournal =
BalancedJournal.CreateReversal(data.OriginalJournalEntry, $"Reversal: {cmd.Reason}", data.TimeStamp); BalancedJournal.CreateReversal(context.OriginalJournalEntry, $"Reversal: {cmd.Reason}", context.TimeStamp);
var accountState = data.AffectedAccounts.ToDictionary(a => a.Id); var accountState = context.AffectedAccounts.ToDictionary(a => a.Id);
reversalJournal.Entry.Lines.ForEach(l => reversalJournal.Entry.Lines.ForEach(l =>
{ {
@@ -1,14 +1,17 @@
using FinancialApi.Application.Commands; using FinancialApi.Application.Commands;
using FinancialApi.Application.Events; using FinancialApi.Application.Events;
using FinancialApi.Application.Queries; using FinancialApi.Application.Interfaces;
using FinancialApi.Application.Models;
using FinancialApi.Domain; using FinancialApi.Domain;
using FinancialApi.Domain.Aggregates;
using FinancialApi.Domain.Entities;
using Wolverine.Persistence; using Wolverine.Persistence;
namespace FinancialApi.Application.Handlers; namespace FinancialApi.Application.Handlers;
public static class TransferFundsHandler public static class TransferFundsHandler
{ {
public static async Task<TransferPair?> LoadAsync(TransferFundsCommand cmd, IAccountQuery query, public static async Task<TransferContext?> LoadAsync(TransferFundsCommand cmd, IAccountQuery query,
TimeProvider timeProvider) TimeProvider timeProvider)
{ {
var source = await query.FindByIdAsync(cmd.SourceAccountId); var source = await query.FindByIdAsync(cmd.SourceAccountId);
@@ -16,7 +19,7 @@ public static class TransferFundsHandler
if (source == null || dest == null) return null; if (source == null || dest == null) return null;
return new TransferPair(source, dest, timeProvider.GetUtcNow()); return new TransferContext(source, dest, timeProvider.GetUtcNow());
} }
public static ( public static (
@@ -24,21 +27,21 @@ public static class TransferFundsHandler
IStorageAction<Account> DestWrite, IStorageAction<Account> DestWrite,
IStorageAction<JournalEntry> JournalWrite, IStorageAction<JournalEntry> JournalWrite,
FundsTransferredEvent Event FundsTransferredEvent Event
) Handle(TransferFundsCommand cmd, TransferPair pair) ) Handle(TransferFundsCommand cmd, TransferContext context)
{ {
var updatedSource = pair.Source.Debit(cmd.Amount); var updatedSource = context.Source.Debit(cmd.Amount);
var updatedDest = pair.Destination.Credit(cmd.Amount); var updatedDest = context.Destination.Credit(cmd.Amount);
var lines = new List<JournalLine> var lines = new List<JournalLine>
{ {
new(pair.Source.Id, cmd.Amount, EntryType.Debit), new(context.Source.Id, cmd.Amount, EntryType.Debit),
new(pair.Destination.Id, cmd.Amount, EntryType.Credit) new(context.Destination.Id, cmd.Amount, EntryType.Credit)
}; };
var journalEntry = var journalEntry =
BalancedJournal.Create(Guid.NewGuid(), BalancedJournal.Create(Guid.NewGuid(),
$"Transfer: {cmd.Amount} from {cmd.SourceAccountId} to {cmd.DestinationAccountId}", $"Transfer: {cmd.Amount} from {cmd.SourceAccountId} to {cmd.DestinationAccountId}",
pair.TimeStamp, lines); context.TimeStamp, lines);
var @event = new FundsTransferredEvent(cmd.SourceAccountId, cmd.DestinationAccountId, cmd.Amount); var @event = new FundsTransferredEvent(cmd.SourceAccountId, cmd.DestinationAccountId, cmd.Amount);
@@ -1,6 +1,7 @@
using FinancialApi.Domain; using FinancialApi.Domain;
using FinancialApi.Domain.Entities;
namespace FinancialApi.Application.Queries; namespace FinancialApi.Application.Interfaces;
public interface IAccountQuery public interface IAccountQuery
{ {
@@ -0,0 +1,10 @@
using FinancialApi.Application.Commands;
using FinancialApi.Application.Handlers;
using FinancialApi.Application.Models;
namespace FinancialApi.Application.Interfaces;
public interface IReversalQuery
{
Task<JournalReversalContext?> GetReversalDataAsync(Guid journalEntryId);
}
@@ -0,0 +1,6 @@
using FinancialApi.Domain;
using FinancialApi.Domain.Entities;
namespace FinancialApi.Application.Models;
public record FeeContext(Account Source, Account Destination, DateTimeOffset TimeStamp);
@@ -0,0 +1,9 @@
using FinancialApi.Domain;
using FinancialApi.Domain.Entities;
namespace FinancialApi.Application.Models;
public record JournalReversalContext(
JournalEntry OriginalJournalEntry,
IReadOnlyList<Account> AffectedAccounts,
DateTimeOffset TimeStamp);
@@ -0,0 +1,6 @@
using FinancialApi.Domain;
using FinancialApi.Domain.Entities;
namespace FinancialApi.Application.Models;
public record TransferContext(Account Source, Account Destination, DateTimeOffset TimeStamp);
@@ -1,8 +0,0 @@
using FinancialApi.Application.Handlers;
namespace FinancialApi.Application.Queries;
public interface IReversalQuery
{
Task<ReversalData?> GetReversalDataAsync(Guid journalEntryId);
}
@@ -1,5 +0,0 @@
using FinancialApi.Domain;
namespace FinancialApi.Application;
public record TransferPair(Account Source, Account Destination, DateTimeOffset TimeStamp);
@@ -1,4 +1,6 @@
namespace FinancialApi.Domain; using FinancialApi.Domain.Entities;
namespace FinancialApi.Domain.Aggregates;
public record BalancedJournal public record BalancedJournal
{ {
@@ -1,4 +1,4 @@
namespace FinancialApi.Domain; namespace FinancialApi.Domain.Entities;
public record Account(int Id, decimal Balance, string AccountType) public record Account(int Id, decimal Balance, string AccountType)
{ {
@@ -1,4 +1,4 @@
namespace FinancialApi.Domain; namespace FinancialApi.Domain.Entities;
public enum EntryType public enum EntryType
{ {
@@ -1,4 +1,4 @@
namespace FinancialApi.Domain; namespace FinancialApi.Domain.Entities;
public record JournalEntry public record JournalEntry
{ {
@@ -1,3 +1,3 @@
namespace FinancialApi.Domain; namespace FinancialApi.Domain.Entities;
public record JournalLine(int AccountId, decimal Amount, EntryType Type); public record JournalLine(int AccountId, decimal Amount, EntryType Type);
@@ -1,4 +1,5 @@
using FinancialApi.Domain; using FinancialApi.Domain;
using FinancialApi.Domain.Entities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace FinancialApi.Infrastructure; namespace FinancialApi.Infrastructure;
@@ -1,5 +1,6 @@
using FinancialApi.Application.Queries; using FinancialApi.Application.Interfaces;
using FinancialApi.Domain; using FinancialApi.Domain;
using FinancialApi.Domain.Entities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace FinancialApi.Infrastructure; namespace FinancialApi.Infrastructure;
@@ -1,12 +1,14 @@
using FinancialApi.Application.Commands;
using FinancialApi.Application.Handlers; using FinancialApi.Application.Handlers;
using FinancialApi.Application.Queries; using FinancialApi.Application.Interfaces;
using FinancialApi.Application.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace FinancialApi.Infrastructure; namespace FinancialApi.Infrastructure;
public class ReversalQuery(AccountDbContext db, TimeProvider timeProvider) : IReversalQuery public class ReversalQuery(AccountDbContext db, TimeProvider timeProvider) : IReversalQuery
{ {
public async Task<ReversalData?> GetReversalDataAsync(Guid journalEntryId) public async Task<JournalReversalContext?> GetReversalDataAsync(Guid journalEntryId)
{ {
var entry = await db.JournalEntries var entry = await db.JournalEntries
.Include(x => x.Lines) .Include(x => x.Lines)
@@ -25,6 +27,6 @@ public class ReversalQuery(AccountDbContext db, TimeProvider timeProvider) : IRe
.Where(a => accountIds.Contains(a.Id)) .Where(a => accountIds.Contains(a.Id))
.ToListAsync(); .ToListAsync();
return new ReversalData(entry, accounts, timeProvider.GetUtcNow()); return new JournalReversalContext(entry, accounts, timeProvider.GetUtcNow());
} }
} }
@@ -2,7 +2,7 @@ using FinancialApi;
using FinancialApi.Application.Commands; using FinancialApi.Application.Commands;
using FinancialApi.Application.Events; using FinancialApi.Application.Events;
using FinancialApi.Application.Handlers; using FinancialApi.Application.Handlers;
using FinancialApi.Application.Queries; using FinancialApi.Application.Interfaces;
using FinancialApi.Infrastructure; using FinancialApi.Infrastructure;
using FinancialApi.ServiceDefaults; using FinancialApi.ServiceDefaults;
using JasperFx.Resources; using JasperFx.Resources;