Add Domain project and shifted handlers to application.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using FinancialApi.Application;
|
||||
using FinancialApi.Domain;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FinancialApi.Infrastructure;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
using FinancialApi.Application;
|
||||
using FinancialApi.Domain;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FinancialApi.Infrastructure;
|
||||
|
||||
public class AccountQuery(AccountDbContext db) : IAccountQuery
|
||||
{
|
||||
public Task<Account?> FindByIdAsync(int id)
|
||||
{
|
||||
return db.Accounts.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id);
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
using FinancialApi.Application;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Wolverine.Attributes;
|
||||
using Wolverine.Persistence;
|
||||
|
||||
namespace FinancialApi.Infrastructure;
|
||||
|
||||
public record ApplyFeeCommand(int AccountId, decimal Amount);
|
||||
|
||||
public record FeePair(Account CustomerAccount, Account RevenueAccount);
|
||||
|
||||
public record FeeAppliedEvent(int AccountId, decimal Amount);
|
||||
|
||||
public static class ApplyFeeHandler
|
||||
{
|
||||
public static async Task<FeePair?> LoadAsync(
|
||||
ApplyFeeCommand cmd,
|
||||
AccountDbContext db)
|
||||
{
|
||||
var customer = await db.Accounts.AsNoTracking().SingleOrDefaultAsync(x => x.Id == cmd.AccountId);
|
||||
var revenue = await db.Accounts.AsNoTracking().SingleOrDefaultAsync(x => x.Id == 99999);
|
||||
return (customer == null || revenue == null) ? null : new FeePair(customer, revenue);
|
||||
}
|
||||
|
||||
[Transactional]
|
||||
public static (
|
||||
IStorageAction<Account> CustomerWrite,
|
||||
IStorageAction<Account> RevenueWrite,
|
||||
IStorageAction<JournalEntry> JournalWrite,
|
||||
FeeAppliedEvent Event
|
||||
)
|
||||
Handle(
|
||||
ApplyFeeCommand cmd,
|
||||
FeePair pair
|
||||
)
|
||||
{
|
||||
var updatedCustomer = pair.CustomerAccount.ApplyPosting(cmd.Amount, EntryType.Debit);
|
||||
var updatedRevenue = pair.RevenueAccount.ApplyPosting(cmd.Amount, EntryType.Credit);
|
||||
var lines = new List<JournalLine>
|
||||
{
|
||||
new(pair.CustomerAccount.Id, cmd.Amount, EntryType.Debit),
|
||||
new(9999, 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(updatedCustomer),
|
||||
Storage.Update(updatedRevenue),
|
||||
Storage.Insert(journalEntry.Entry),
|
||||
@event
|
||||
);
|
||||
}
|
||||
}
|
||||
+1
@@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\FinancialApi.Application\FinancialApi.Application.csproj" />
|
||||
<ProjectReference Include="..\FinancialApi.Domain\FinancialApi.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using FinancialApi.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace FinancialApi.Infrastructure.Migrations
|
||||
{
|
||||
[DbContext(typeof(AccountDbContext))]
|
||||
[Migration("20260715100428_UpdateAccountBalances")]
|
||||
partial class UpdateAccountBalances
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "10.0.9");
|
||||
|
||||
modelBuilder.Entity("FinancialApi.Application.Account", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("AccountType")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<decimal>("Balance")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Accounts");
|
||||
|
||||
b.HasData(
|
||||
new
|
||||
{
|
||||
Id = 1,
|
||||
AccountType = "Credit",
|
||||
Balance = 10000000.00m
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 2,
|
||||
AccountType = "Liability",
|
||||
Balance = 50000000.00m
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 99999,
|
||||
AccountType = "Revenue",
|
||||
Balance = 0.00m
|
||||
});
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FinancialApi.Application.JournalEntry", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<DateTimeOffset>("CreatedAt")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("JournalEntries");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FinancialApi.Application.JournalEntry", b =>
|
||||
{
|
||||
b.OwnsMany("FinancialApi.Application.JournalLine", "Lines", b1 =>
|
||||
{
|
||||
b1.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b1.Property<int>("AccountId")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b1.Property<decimal>("Amount")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b1.Property<Guid>("JournalEntryId")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b1.Property<string>("Type")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b1.HasKey("Id");
|
||||
|
||||
b1.HasIndex("JournalEntryId");
|
||||
|
||||
b1.ToTable("JournalLine");
|
||||
|
||||
b1.WithOwner()
|
||||
.HasForeignKey("JournalEntryId");
|
||||
});
|
||||
|
||||
b.Navigation("Lines");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
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,
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user