Added a transfer funds handler using a-frame and wolverine.
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
namespace FinancialApi.Application;
|
||||
|
||||
public record Account(int Id, decimal Balance)
|
||||
{
|
||||
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 };
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
namespace FinancialApi.Application;
|
||||
|
||||
public record Account(int Id, decimal Balance)
|
||||
{
|
||||
public Account ApplyFee(decimal amount)
|
||||
{
|
||||
if (amount <= 0)
|
||||
throw new ArgumentException("Fee must be positive.");
|
||||
|
||||
if (Balance - amount < 0)
|
||||
throw new InvalidOperationException("Insufficient funds.");
|
||||
|
||||
// Returns a brand new record with the updated balance
|
||||
return this with { Balance = Balance - amount };
|
||||
}
|
||||
}
|
||||
+1
-5
@@ -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)
|
||||
+1
-2
@@ -1,10 +1,9 @@
|
||||
using FinancialApi.Application;
|
||||
using FinancialApi.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Wolverine.Attributes;
|
||||
using Wolverine.Persistence;
|
||||
|
||||
namespace FinancialApi;
|
||||
namespace FinancialApi.Infrastructure;
|
||||
|
||||
public static class ApplyFeeHandler
|
||||
{
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
GET {{FinancialApi_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
### Apply Fee to an Account
|
||||
|
||||
POST {{FinancialApi_HostAddress}}/accounts/apply-fee
|
||||
Accept: application/json
|
||||
@@ -14,4 +14,13 @@ Content-Type: application/json
|
||||
"amount": 5.0
|
||||
}
|
||||
|
||||
###
|
||||
### Transfer Between Accounts
|
||||
|
||||
POST {{FinancialApi_HostAddress}}/accounts/transfer
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"sourceAccountId": 1,
|
||||
"destinationAccountId": 2,
|
||||
"amount": 30.00
|
||||
}
|
||||
|
||||
@@ -53,4 +53,10 @@ app.MapPost("/accounts/apply-fee", async (ApplyFeeCommand cmd, IMessageBus bus)
|
||||
return Results.Accepted();
|
||||
});
|
||||
|
||||
app.MapPost("/accounts/transfer", async (TransferFundsCommand cmd, IMessageBus bus) =>
|
||||
{
|
||||
await bus.InvokeAsync(cmd);
|
||||
return Results.Accepted();
|
||||
});
|
||||
|
||||
app.Run();
|
||||
|
||||
Reference in New Issue
Block a user