Added a-frame-architecture usiung wolverine sample.
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
namespace FinancialApi.Application.Commands;
|
||||
|
||||
public record ApplyFeeCommand(int AccountId, decimal Amount);
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
namespace FinancialApi.Application.Commands;
|
||||
|
||||
public record ReverseJournalCommand(Guid OriginalJournalId, string Reason);
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
namespace FinancialApi.Application.Commands;
|
||||
|
||||
public record TransferFundsCommand(int SourceAccountId, int DestinationAccountId, decimal Amount);
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\FinancialApi.Domain\FinancialApi.Domain.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.Extensions.Logging.Abstractions">
|
||||
<HintPath>..\..\..\..\..\..\.nuget\packages\microsoft.extensions.logging.abstractions\10.0.9\lib\net10.0\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Wolverine">
|
||||
<HintPath>..\..\..\..\..\..\.nuget\packages\wolverinefx\6.18.0\lib\net10.0\Wolverine.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,51 @@
|
||||
using FinancialApi.Application.Commands;
|
||||
using FinancialApi.Application.Interfaces;
|
||||
using FinancialApi.Application.Models;
|
||||
using FinancialApi.Domain.Aggregates;
|
||||
using FinancialApi.Domain.Entities;
|
||||
using FinancialApi.Domain.Events;
|
||||
using Wolverine.Persistence;
|
||||
|
||||
namespace FinancialApi.Application.Handlers;
|
||||
|
||||
public static class ApplyFeeHandler
|
||||
{
|
||||
public static async Task<FeeContext> LoadAsync(ApplyFeeCommand cmd, IAccountQuery query, TimeProvider timeProvider)
|
||||
{
|
||||
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 new FeeContext(source, dest, timeProvider.GetUtcNow());
|
||||
}
|
||||
|
||||
public static ( IStorageAction<Account> CustomerWrite, IStorageAction<Account> RevenueWrite,
|
||||
IStorageAction<BalancedJournalEntry> JournalWrite, FeeAppliedEvent Event ) Handle(
|
||||
ApplyFeeCommand cmd,
|
||||
FeeContext context
|
||||
)
|
||||
{
|
||||
var updatedSource = context.Source.ApplyPosting(cmd.Amount, EntryType.Debit);
|
||||
var updatedRevenue = context.Destination.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}",
|
||||
context.TimeStamp,
|
||||
lines
|
||||
);
|
||||
|
||||
var @event = new FeeAppliedEvent(cmd.AccountId, cmd.Amount);
|
||||
|
||||
return (Storage.Update(updatedSource), Storage.Update(updatedRevenue), Storage.Insert(journalEntry.Entry),
|
||||
@event);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
using FinancialApi.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FinancialApi.Application.Handlers;
|
||||
|
||||
public static class FinancialEventHandler
|
||||
{
|
||||
public static void Handle(FeeAppliedEvent @event, IEventTracker store, ILogger logger)
|
||||
{
|
||||
var message = $"FeeApplied: ${@event.Amount} moved from Account {@event.AccountId} to Account 99999.";
|
||||
logger.LogInformation("BACKGROUND EVENT FIRED: {Message}", message);
|
||||
store.Add(message);
|
||||
}
|
||||
|
||||
public static void Handle(FundsTransferredEvent @event, IEventTracker store, ILogger logger)
|
||||
{
|
||||
var message =
|
||||
$"FundsTransferred: ${@event.Amount} moved from Account {@event.SourceAccountId} to Account {@event.DestinationAccountId}.";
|
||||
logger.LogInformation("BACKGROUND EVENT FIRED: {Message}", message);
|
||||
store.Add(message);
|
||||
}
|
||||
|
||||
public static void Handle(JournalReversedEvent @event, IEventTracker store, ILogger logger)
|
||||
{
|
||||
var message =
|
||||
$"JournalReversed: Original Entry {@event.OriginalJournalId} was reversed by Entry {@event.ReversalJournalId}.";
|
||||
logger.LogInformation("BACKGROUND EVENT FIRED: {Message}", message);
|
||||
store.Add(message);
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
using FinancialApi.Application.Commands;
|
||||
using FinancialApi.Application.Interfaces;
|
||||
using FinancialApi.Application.Models;
|
||||
using FinancialApi.Domain.Aggregates;
|
||||
using FinancialApi.Domain.Entities;
|
||||
using FinancialApi.Domain.Events;
|
||||
using Wolverine.Persistence;
|
||||
|
||||
namespace FinancialApi.Application.Handlers;
|
||||
|
||||
public static class ReverseJournalHandler
|
||||
{
|
||||
public static async Task<JournalReversalContext?> LoadAsync(
|
||||
ReverseJournalCommand cmd,
|
||||
IReversalQuery query,
|
||||
TimeProvider timeProvider
|
||||
)
|
||||
{
|
||||
var (journalEnty, accounts) = await query.GetReversalDataAsync(cmd.OriginalJournalId);
|
||||
if (journalEnty == null)
|
||||
{
|
||||
throw new InvalidOperationException("Journal not found");
|
||||
}
|
||||
|
||||
if (accounts == null)
|
||||
{
|
||||
throw new InvalidOperationException("Journal accounts not found");
|
||||
}
|
||||
|
||||
return new JournalReversalContext(journalEnty, accounts, timeProvider.GetUtcNow()) ??
|
||||
throw new InvalidOperationException($"Journal Entry {cmd.OriginalJournalId} does not exist");
|
||||
}
|
||||
|
||||
public static (IStorageAction<BalancedJournalEntry> JournalWrite, UnitOfWork<Account> AccountWrites, JournalReversedEvent
|
||||
Event) Handle(ReverseJournalCommand cmd, JournalReversalContext context)
|
||||
{
|
||||
var reversalJournal = BalancedJournal.CreateReversal(
|
||||
context.OriginalBalancedJournalEntry,
|
||||
$"Reversal: {cmd.Reason}",
|
||||
context.TimeStamp
|
||||
);
|
||||
var accountState = context.AffectedAccounts.ToDictionary(a => a.Id);
|
||||
|
||||
reversalJournal.Entry.Lines.ForEach(l =>
|
||||
{
|
||||
var currentAccount = accountState[l.AccountId];
|
||||
accountState[l.AccountId] = currentAccount.ApplyPosting(l.Amount, l.Type);
|
||||
}
|
||||
);
|
||||
var accountUow = new UnitOfWork<Account>();
|
||||
foreach (var account in accountState.Values)
|
||||
{
|
||||
accountUow.Update(account);
|
||||
}
|
||||
|
||||
return (Storage.Insert(reversalJournal.Entry), accountUow,
|
||||
new JournalReversedEvent(cmd.OriginalJournalId, reversalJournal.Entry.Id));
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
using FinancialApi.Application.Commands;
|
||||
using FinancialApi.Application.Interfaces;
|
||||
using FinancialApi.Application.Models;
|
||||
using FinancialApi.Domain.Aggregates;
|
||||
using FinancialApi.Domain.Entities;
|
||||
using FinancialApi.Domain.Events;
|
||||
using Wolverine.Persistence;
|
||||
|
||||
namespace FinancialApi.Application.Handlers;
|
||||
|
||||
public static class TransferFundsHandler
|
||||
{
|
||||
public static async Task<TransferContext?> LoadAsync(
|
||||
TransferFundsCommand cmd,
|
||||
IAccountQuery query,
|
||||
TimeProvider timeProvider
|
||||
)
|
||||
{
|
||||
var source = await query.FindByIdAsync(cmd.SourceAccountId);
|
||||
var dest = await query.FindByIdAsync(cmd.DestinationAccountId);
|
||||
|
||||
if (source == null || dest == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new TransferContext(source, dest, timeProvider.GetUtcNow());
|
||||
}
|
||||
|
||||
public static (IStorageAction<Account> SourceWrite, IStorageAction<Account> DestWrite, IStorageAction<BalancedJournalEntry>
|
||||
JournalWrite, FundsTransferredEvent Event) Handle(TransferFundsCommand cmd, TransferContext context)
|
||||
{
|
||||
var updatedSource = context.Source.ApplyPosting(cmd.Amount, EntryType.Debit);
|
||||
var updatedDest = context.Destination.ApplyPosting(cmd.Amount, EntryType.Credit);
|
||||
|
||||
var lines = new List<JournalLine>
|
||||
{
|
||||
new(context.Source.Id, cmd.Amount, EntryType.Debit),
|
||||
new(context.Destination.Id, cmd.Amount, EntryType.Credit)
|
||||
};
|
||||
|
||||
var journalEntry = BalancedJournal.Create(
|
||||
Guid.NewGuid(),
|
||||
$"Transfer: {cmd.Amount} from {cmd.SourceAccountId} to {cmd.DestinationAccountId}",
|
||||
context.TimeStamp,
|
||||
lines
|
||||
);
|
||||
|
||||
var @event = new FundsTransferredEvent(cmd.SourceAccountId, cmd.DestinationAccountId, cmd.Amount);
|
||||
|
||||
return (Storage.Update(updatedSource), Storage.Update(updatedDest), Storage.Insert(journalEntry.Entry), @event);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using FinancialApi.Domain.Entities;
|
||||
|
||||
namespace FinancialApi.Application.Interfaces;
|
||||
|
||||
public interface IAccountQuery
|
||||
{
|
||||
Task<Account?> FindByIdAsync(int id);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using FinancialApi.Domain.Entities;
|
||||
|
||||
namespace FinancialApi.Application.Interfaces;
|
||||
|
||||
public interface IReversalQuery
|
||||
{
|
||||
Task<(BalancedJournalEntry?, List<Account>?)> GetReversalDataAsync(Guid journalEntryId);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
using FinancialApi.Domain.Entities;
|
||||
|
||||
namespace FinancialApi.Application.Models;
|
||||
|
||||
public record FeeContext(Account Source, Account Destination, DateTimeOffset TimeStamp);
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
using FinancialApi.Domain.Entities;
|
||||
|
||||
namespace FinancialApi.Application.Models;
|
||||
|
||||
public record JournalReversalContext(
|
||||
BalancedJournalEntry OriginalBalancedJournalEntry,
|
||||
IReadOnlyList<Account> AffectedAccounts,
|
||||
DateTimeOffset TimeStamp
|
||||
);
|
||||
@@ -0,0 +1,5 @@
|
||||
using FinancialApi.Domain.Entities;
|
||||
|
||||
namespace FinancialApi.Application.Models;
|
||||
|
||||
public record TransferContext(Account Source, Account Destination, DateTimeOffset TimeStamp);
|
||||
Reference in New Issue
Block a user