Added a transfer funds handler using a-frame and wolverine.

This commit is contained in:
2026-07-14 16:57:03 +02:00
parent 9de9777926
commit 0de38ef2df
7 changed files with 105 additions and 25 deletions
@@ -3,12 +3,8 @@ using Microsoft.EntityFrameworkCore;
namespace FinancialApi.Infrastructure;
public class AccountDbContext : DbContext
public class AccountDbContext(DbContextOptions<AccountDbContext> options) : DbContext(options)
{
public AccountDbContext(DbContextOptions<AccountDbContext> options) : base(options)
{
}
public DbSet<Account> Accounts => Set<Account>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
@@ -0,0 +1,29 @@
using FinancialApi.Application;
using Microsoft.EntityFrameworkCore;
using Wolverine.Attributes;
using Wolverine.Persistence;
namespace FinancialApi.Infrastructure;
public static class ApplyFeeHandler
{
public static Task<Account?> LoadAsync(
ApplyFeeCommand cmd,
AccountDbContext db) => db.Accounts.AsNoTracking().SingleOrDefaultAsync(x => x.Id == cmd.AccountId);
[Transactional]
public static (IStorageAction<Account> UpdatedAccount, FeeAppliedEvent Event) Handle(
ApplyFeeCommand cmd,
Account account)
{
var updatedAccount = account.ApplyFee(cmd.Amount);
var @event = new FeeAppliedEvent(account.Id, cmd.Amount);
return (Storage.Update(updatedAccount), @event);
}
}
public record ApplyFeeCommand(int AccountId, decimal Amount);
public record FeeAppliedEvent(int AccountId, decimal Amount);
@@ -0,0 +1,43 @@
using FinancialApi.Application;
using Microsoft.EntityFrameworkCore;
using Wolverine.Attributes;
using Wolverine.Persistence;
namespace FinancialApi.Infrastructure;
public record TransferFundsCommand(int SourceAccountId, int DestinationAccountId, decimal Amount);
public record FundsTransferredEvent(int SourceAccountId, int DestinationAccountId, decimal Amount);
// A simple container to pass our loaded entities into the pure function
public record TransferPair(Account Source, Account Destination);
public static class TransferFundsHandler
{
public static async Task<TransferPair?> LoadAsync(TransferFundsCommand cmd, AccountDbContext db)
{
var source = await db.Accounts.AsNoTracking().FirstOrDefaultAsync(x => x.Id == cmd.SourceAccountId);
var dest = await db.Accounts.AsNoTracking().FirstOrDefaultAsync(x => x.Id == cmd.DestinationAccountId);
if (source == null || dest == null) return null; // Wolverine drops into a 404/Problem if null
return new TransferPair(source, dest);
}
[Transactional]
public static (
IStorageAction<Account> SourceWrite,
IStorageAction<Account> DestWrite,
FundsTransferredEvent Event
) Handle(TransferFundsCommand cmd, TransferPair pair)
{
var updatedSource = pair.Source.Debit(cmd.Amount);
var updatedDest = pair.Destination.Credit(cmd.Amount);
var sourcePersistence = Storage.Update(updatedSource);
var destPersistence = Storage.Update(updatedDest);
var @event = new FundsTransferredEvent(cmd.SourceAccountId, cmd.DestinationAccountId, cmd.Amount);
return (sourcePersistence, destPersistence, @event);
}
}