Add Domain project and shifted handlers to application.

This commit is contained in:
2026-07-15 16:10:00 +02:00
parent 656f545281
commit 0376b58a33
16 changed files with 229 additions and 52 deletions
@@ -1,70 +0,0 @@
namespace FinancialApi.Application;
public record Account(int Id, decimal Balance, string AccountType)
{
public Account ApplyPosting(decimal amount, EntryType entryType)
{
if (amount <= 0)
{
throw new ArgumentException("Credit amount must be positive.");
}
var newBalance = Balance;
if (AccountType == "Revenue")
{
newBalance = entryType == EntryType.Credit ? newBalance + amount : newBalance - amount;
}
else
{
newBalance = entryType == EntryType.Debit ? newBalance - amount : newBalance + amount;
}
if (newBalance < 0 && AccountType == "Liability")
{
throw new InvalidOperationException("Insufficient funds.");
}
return this with { Balance = newBalance };
}
public Account ApplyFee(decimal amount)
{
if (amount <= 0)
{
throw new ArgumentException("Fee must be positive.");
}
if (Balance - amount < 0)
{
throw new InvalidOperationException("Insufficient funds.");
}
return this with { Balance = Balance - amount };
}
public Account Debit(decimal amount)
{
if (amount <= 0)
{
throw new ArgumentException("Debit amount must be positive.");
}
if (Balance - amount < 0)
{
throw new InvalidOperationException("Insufficient funds.");
}
return this with { Balance = Balance - amount };
}
public Account Credit(decimal amount)
{
if (amount <= 0)
{
throw new ArgumentException("Credit amount must be positive.");
}
return this with { Balance = Balance + amount };
}
}
@@ -0,0 +1,58 @@
using FinancialApi.Domain;
using Wolverine.Attributes;
using Wolverine.Persistence;
namespace FinancialApi.Application;
public record ApplyFeeCommand(int AccountId, decimal Amount);
public record FeeAppliedEvent(int AccountId, decimal Amount);
public static class ApplyFeeHandler
{
public static async Task<(Account, Account)> LoadAsync(
ApplyFeeCommand cmd,
IAccountQuery query)
{
var source = await query.FindByIdAsync(cmd.AccountId);
var dest = await query.FindByIdAsync(99999);
if (source == null || dest == null)
{
throw new InvalidOperationException($"Cannot process transfer. Account(s) not found.");
}
return (source, dest);
}
public static (
IStorageAction<Account> CustomerWrite,
IStorageAction<Account> RevenueWrite,
IStorageAction<JournalEntry> JournalWrite,
FeeAppliedEvent Event
)
Handle(
ApplyFeeCommand cmd,
Account source,
Account dest
)
{
var updatedSource = source.ApplyPosting(cmd.Amount, EntryType.Debit);
var updatedRevenue = dest.ApplyPosting(cmd.Amount, EntryType.Credit);
var lines = new List<JournalLine>
{
new(updatedSource.Id, cmd.Amount, EntryType.Debit),
new(updatedRevenue.Id, cmd.Amount, EntryType.Credit)
};
var journalEntry = BalancedJournal.Create(Guid.NewGuid(),
$"Service Fee Applied: {cmd.Amount} to Account {cmd.AccountId}",
DateTimeOffset.UtcNow, lines);
var @event = new FeeAppliedEvent(cmd.AccountId, cmd.Amount);
return (
Storage.Update(updatedSource),
Storage.Update(updatedRevenue),
Storage.Insert(journalEntry.Entry),
@event
);
}
}
@@ -1,23 +0,0 @@
namespace FinancialApi.Application;
public record BalancedJournal
{
public JournalEntry Entry { get; }
private BalancedJournal(JournalEntry entry) => Entry = entry;
public static BalancedJournal Create(Guid id, string description, DateTimeOffset createdAt, List<JournalLine> lines)
{
var debits = lines.Where(l => l.Type == EntryType.Debit).Sum(l => l.Amount);
var credits = lines.Where(l => l.Type == EntryType.Credit).Sum(l => l.Amount);
if (debits != credits)
{
throw new InvalidOperationException(
$"GAAP Violation: Total Debits ({debits}) must equal Total Credits ({credits})!"
);
}
return new(new JournalEntry(id, description, createdAt, lines));
}
}
@@ -1,7 +0,0 @@
namespace FinancialApi.Application;
public enum EntryType
{
Debit,
Credit
}
@@ -6,4 +6,14 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\FinancialApi.Domain\FinancialApi.Domain.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Wolverine">
<HintPath>..\..\..\..\..\..\.nuget\packages\wolverinefx\6.18.0\lib\net10.0\Wolverine.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
@@ -1,21 +0,0 @@
namespace FinancialApi.Application;
public record JournalEntry
{
public Guid Id { get; init; }
public string Description { get; init; }
public DateTimeOffset CreatedAt { get; init; }
public List<JournalLine> Lines { get; init; } = [];
private JournalEntry()
{
}
internal JournalEntry(Guid id, string description, DateTimeOffset createdAt, List<JournalLine> lines)
{
Lines = lines;
Id = id;
Description = description;
CreatedAt = createdAt;
}
}
@@ -1,3 +0,0 @@
namespace FinancialApi.Application;
public record JournalLine(int AccountId, decimal Amount, EntryType Type);
@@ -0,0 +1,60 @@
using FinancialApi.Domain;
using Wolverine.Attributes;
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);
public static class TransferFundsHandler
{
public static async Task<TransferPair?> LoadAsync(TransferFundsCommand cmd, IAccountQuery query)
{
var source = await query.FindByIdAsync(cmd.SourceAccountId);
var dest = await query.FindByIdAsync(cmd.DestinationAccountId);
if (source == null || dest == null) return null;
return new TransferPair(source, dest);
}
public static (
IStorageAction<Account> SourceWrite,
IStorageAction<Account> DestWrite,
IStorageAction<JournalEntry> JournalWrite,
FundsTransferredEvent Event
) Handle(TransferFundsCommand cmd, TransferPair pair)
{
var updatedSource = pair.Source.Debit(cmd.Amount);
var updatedDest = pair.Destination.Credit(cmd.Amount);
var lines = new List<JournalLine>
{
new (pair.Source.Id, cmd.Amount, EntryType.Debit),
new (pair.Destination.Id, cmd.Amount, EntryType.Credit)
};
var journalEntry =
BalancedJournal.Create(Guid.NewGuid(),
$"Transfer: {cmd.Amount} from {cmd.SourceAccountId} to {cmd.DestinationAccountId}",
DateTimeOffset.UtcNow, lines);
var @event = new FundsTransferredEvent(cmd.SourceAccountId, cmd.DestinationAccountId, cmd.Amount);
return (
Storage.Update(updatedSource),
Storage.Update(updatedDest),
Storage.Insert(journalEntry.Entry),
@event
);
}
}