Re-layout source code to correct namespaces (handlers, commands, etc.)
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
using FinancialApi.Application.Commands;
|
||||
using FinancialApi.Application.Events;
|
||||
using FinancialApi.Application.Queries;
|
||||
using FinancialApi.Domain;
|
||||
using Wolverine.Persistence;
|
||||
|
||||
namespace FinancialApi.Application.Handlers;
|
||||
|
||||
public static class ApplyFeeHandler
|
||||
{
|
||||
public static async Task<FeePair> 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 FeePair(source, dest, timeProvider.GetUtcNow());
|
||||
}
|
||||
|
||||
public static (
|
||||
IStorageAction<Account> CustomerWrite,
|
||||
IStorageAction<Account> RevenueWrite,
|
||||
IStorageAction<JournalEntry> JournalWrite,
|
||||
FeeAppliedEvent Event
|
||||
)
|
||||
Handle(
|
||||
ApplyFeeCommand cmd,
|
||||
FeePair pair
|
||||
)
|
||||
{
|
||||
var updatedSource = pair.Source.ApplyPosting(cmd.Amount, EntryType.Debit);
|
||||
var updatedRevenue = pair.Destination.ApplyPosting(cmd.Amount, EntryType.Credit);
|
||||
var serviceFee = 0.5m;
|
||||
|
||||
var lines = new List<JournalLine>
|
||||
{
|
||||
new(updatedSource.Id, cmd.Amount, EntryType.Debit),
|
||||
new(updatedRevenue.Id, cmd.Amount - serviceFee, EntryType.Credit),
|
||||
new(updatedRevenue.Id, serviceFee, EntryType.Credit)
|
||||
};
|
||||
|
||||
var journalEntry = BalancedJournal.Create(Guid.NewGuid(),
|
||||
$"Service Fee Applied: {cmd.Amount} to Account {cmd.AccountId}",
|
||||
pair.TimeStamp, lines);
|
||||
|
||||
var @event = new FeeAppliedEvent(cmd.AccountId, cmd.Amount);
|
||||
|
||||
return (
|
||||
Storage.Update(updatedSource),
|
||||
Storage.Update(updatedRevenue),
|
||||
Storage.Insert(journalEntry.Entry),
|
||||
@event
|
||||
);
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
using FinancialApi.Application.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);
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
using FinancialApi.Application.Commands;
|
||||
using FinancialApi.Application.Events;
|
||||
using FinancialApi.Application.Queries;
|
||||
using FinancialApi.Domain;
|
||||
using Wolverine.Persistence;
|
||||
|
||||
namespace FinancialApi.Application.Handlers;
|
||||
|
||||
public record ReversalData(
|
||||
JournalEntry OriginalJournalEntry,
|
||||
IReadOnlyList<Account> AffectedAccounts,
|
||||
DateTimeOffset TimeStamp);
|
||||
|
||||
public static class ReverseJournalHandler
|
||||
{
|
||||
public static async Task<ReversalData?> LoadAsync(ReverseJournalCommand cmd, IReversalQuery query,
|
||||
TimeProvider timeProvider)
|
||||
{
|
||||
var data = await query.GetReversalDataAsync(cmd.OriginalJournalId);
|
||||
return data ?? throw new InvalidOperationException($"Journal Entry {cmd.OriginalJournalId} does not exist");
|
||||
}
|
||||
|
||||
public static (
|
||||
IStorageAction<JournalEntry> JournalWrite,
|
||||
UnitOfWork<Account> AccountWrites,
|
||||
JournalReversedEvent Event)
|
||||
Handle(ReverseJournalCommand cmd, ReversalData data)
|
||||
{
|
||||
var reversalJournal =
|
||||
BalancedJournal.CreateReversal(data.OriginalJournalEntry, $"Reversal: {cmd.Reason}", data.TimeStamp);
|
||||
var accountState = data.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)
|
||||
);
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
using FinancialApi.Application.Commands;
|
||||
using FinancialApi.Application.Events;
|
||||
using FinancialApi.Application.Queries;
|
||||
using FinancialApi.Domain;
|
||||
using Wolverine.Persistence;
|
||||
|
||||
namespace FinancialApi.Application.Handlers;
|
||||
|
||||
public static class TransferFundsHandler
|
||||
{
|
||||
public static async Task<TransferPair?> 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 TransferPair(source, dest, timeProvider.GetUtcNow());
|
||||
}
|
||||
|
||||
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}",
|
||||
pair.TimeStamp, 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