Re-layout source code to correct namespaces (handlers, commands, etc.)
This commit is contained in:
+11
-9
@@ -1,4 +1,7 @@
|
|||||||
using FinancialApi.Domain;
|
using System.Security.Cryptography;
|
||||||
|
using FinancialApi.Application.Commands;
|
||||||
|
using FinancialApi.Application.Handlers;
|
||||||
|
using FinancialApi.Domain;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using FluentAssertions.Execution;
|
using FluentAssertions.Execution;
|
||||||
|
|
||||||
@@ -12,19 +15,18 @@ public class ApplyFeeHandlerTests
|
|||||||
// Arrange
|
// Arrange
|
||||||
var command = new ApplyFeeCommand(1, 100);
|
var command = new ApplyFeeCommand(1, 100);
|
||||||
var account = new Account(1, 100, "Debit");
|
var account = new Account(1, 100, "Debit");
|
||||||
var destAccount = new Account(99999, 100, "Credit");
|
var revenueAccount = new Account(99999, 0, "Credit");
|
||||||
var pair = new FeePair(account, destAccount, TimeProvider.System.GetUtcNow());
|
var pair = new FeePair(account, revenueAccount, TimeProvider.System.GetUtcNow());
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var intents = ApplyFeeHandler.Handle(command, pair);
|
var intents = ApplyFeeHandler.Handle(command, pair);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
using var _ = new AssertionScope();
|
using var _ = new AssertionScope();
|
||||||
intents.CustomerWrite.Entity.Balance.Should().Be(0);
|
intents.CustomerWrite.Entity.Balance.Should().Be(account.Balance - command.Amount);
|
||||||
intents.RevenueWrite.Entity.Balance.Should().Be(200);
|
intents.RevenueWrite.Entity.Balance.Should().Be(revenueAccount.Balance + command.Amount);
|
||||||
intents.RevenueWrite.Entity.Balance.Should().Be(200);
|
intents.JournalWrite.Entity.Lines.Count.Should().BeGreaterThanOrEqualTo(2);
|
||||||
intents.JournalWrite.Entity.Lines.Count.Should().Be(2);
|
|
||||||
intents.Event.AccountId.Should().Be(account.Id);
|
intents.Event.AccountId.Should().Be(account.Id);
|
||||||
intents.Event.Amount.Should().Be(100);
|
intents.Event.Amount.Should().Be(command.Amount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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 abstract record TransferFundsCommand(int SourceAccountId, int DestinationAccountId, decimal Amount);
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
namespace FinancialApi.Application.Events;
|
||||||
|
|
||||||
|
public record FeeAppliedEvent(int AccountId, decimal Amount);
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
namespace FinancialApi.Application.Events;
|
||||||
|
|
||||||
|
public record FundsTransferredEvent(int SourceAccountId, int DestinationAccountId, decimal Amount);
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
namespace FinancialApi.Application;
|
namespace FinancialApi.Application.Events;
|
||||||
|
|
||||||
public interface IEventTracker
|
public interface IEventTracker
|
||||||
{
|
{
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
namespace FinancialApi.Application.Events;
|
||||||
|
|
||||||
|
public record JournalReversedEvent(Guid OriginalJournalId, Guid ReversalJournalId);
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
using FinancialApi.Domain;
|
||||||
|
|
||||||
|
namespace FinancialApi.Application;
|
||||||
|
|
||||||
|
public record FeePair(Account Source, Account Destination, DateTimeOffset TimeStamp);
|
||||||
+9
-11
@@ -1,16 +1,10 @@
|
|||||||
using System;
|
using FinancialApi.Application.Commands;
|
||||||
using System.Collections.Generic;
|
using FinancialApi.Application.Events;
|
||||||
using System.Threading.Tasks;
|
using FinancialApi.Application.Queries;
|
||||||
using FinancialApi.Domain;
|
using FinancialApi.Domain;
|
||||||
using Wolverine.Persistence;
|
using Wolverine.Persistence;
|
||||||
|
|
||||||
namespace FinancialApi.Application;
|
namespace FinancialApi.Application.Handlers;
|
||||||
|
|
||||||
public record ApplyFeeCommand(int AccountId, decimal Amount);
|
|
||||||
|
|
||||||
public record FeeAppliedEvent(int AccountId, decimal Amount);
|
|
||||||
|
|
||||||
public record FeePair(Account Source, Account Destination, DateTimeOffset TimeStamp);
|
|
||||||
|
|
||||||
public static class ApplyFeeHandler
|
public static class ApplyFeeHandler
|
||||||
{
|
{
|
||||||
@@ -41,11 +35,15 @@ public static class ApplyFeeHandler
|
|||||||
{
|
{
|
||||||
var updatedSource = pair.Source.ApplyPosting(cmd.Amount, EntryType.Debit);
|
var updatedSource = pair.Source.ApplyPosting(cmd.Amount, EntryType.Debit);
|
||||||
var updatedRevenue = pair.Destination.ApplyPosting(cmd.Amount, EntryType.Credit);
|
var updatedRevenue = pair.Destination.ApplyPosting(cmd.Amount, EntryType.Credit);
|
||||||
|
var serviceFee = 0.5m;
|
||||||
|
|
||||||
var lines = new List<JournalLine>
|
var lines = new List<JournalLine>
|
||||||
{
|
{
|
||||||
new(updatedSource.Id, cmd.Amount, EntryType.Debit),
|
new(updatedSource.Id, cmd.Amount, EntryType.Debit),
|
||||||
new(updatedRevenue.Id, cmd.Amount, EntryType.Credit)
|
new(updatedRevenue.Id, cmd.Amount - serviceFee, EntryType.Credit),
|
||||||
|
new(updatedRevenue.Id, serviceFee, EntryType.Credit)
|
||||||
};
|
};
|
||||||
|
|
||||||
var journalEntry = BalancedJournal.Create(Guid.NewGuid(),
|
var journalEntry = BalancedJournal.Create(Guid.NewGuid(),
|
||||||
$"Service Fee Applied: {cmd.Amount} to Account {cmd.AccountId}",
|
$"Service Fee Applied: {cmd.Amount} to Account {cmd.AccountId}",
|
||||||
pair.TimeStamp, lines);
|
pair.TimeStamp, lines);
|
||||||
+2
-1
@@ -1,6 +1,7 @@
|
|||||||
|
using FinancialApi.Application.Events;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace FinancialApi.Application;
|
namespace FinancialApi.Application.Handlers;
|
||||||
|
|
||||||
public static class FinancialEventHandler
|
public static class FinancialEventHandler
|
||||||
{
|
{
|
||||||
+6
-15
@@ -1,26 +1,16 @@
|
|||||||
using System;
|
using FinancialApi.Application.Commands;
|
||||||
using System.Collections.Generic;
|
using FinancialApi.Application.Events;
|
||||||
using System.Linq;
|
using FinancialApi.Application.Queries;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using FinancialApi.Domain;
|
using FinancialApi.Domain;
|
||||||
using Wolverine.Persistence;
|
using Wolverine.Persistence;
|
||||||
|
|
||||||
namespace FinancialApi.Application;
|
namespace FinancialApi.Application.Handlers;
|
||||||
|
|
||||||
public record ReverseJournalCommand(Guid OriginalJournalId, string Reason);
|
|
||||||
|
|
||||||
public record JournalReversedEvent(Guid OriginalJournalId, Guid ReversalJournalId);
|
|
||||||
|
|
||||||
public record ReversalData(
|
public record ReversalData(
|
||||||
JournalEntry OriginalJournalEntry,
|
JournalEntry OriginalJournalEntry,
|
||||||
IReadOnlyList<Account> AffectedAccounts,
|
IReadOnlyList<Account> AffectedAccounts,
|
||||||
DateTimeOffset TimeStamp);
|
DateTimeOffset TimeStamp);
|
||||||
|
|
||||||
public interface IReversalQuery
|
|
||||||
{
|
|
||||||
Task<ReversalData?> GetReversalDataAsync(Guid journalEntryId);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class ReverseJournalHandler
|
public static class ReverseJournalHandler
|
||||||
{
|
{
|
||||||
public static async Task<ReversalData?> LoadAsync(ReverseJournalCommand cmd, IReversalQuery query,
|
public static async Task<ReversalData?> LoadAsync(ReverseJournalCommand cmd, IReversalQuery query,
|
||||||
@@ -33,7 +23,8 @@ public static class ReverseJournalHandler
|
|||||||
public static (
|
public static (
|
||||||
IStorageAction<JournalEntry> JournalWrite,
|
IStorageAction<JournalEntry> JournalWrite,
|
||||||
UnitOfWork<Account> AccountWrites,
|
UnitOfWork<Account> AccountWrites,
|
||||||
JournalReversedEvent Event) Handle(ReverseJournalCommand cmd, ReversalData data)
|
JournalReversedEvent Event)
|
||||||
|
Handle(ReverseJournalCommand cmd, ReversalData data)
|
||||||
{
|
{
|
||||||
var reversalJournal =
|
var reversalJournal =
|
||||||
BalancedJournal.CreateReversal(data.OriginalJournalEntry, $"Reversal: {cmd.Reason}", data.TimeStamp);
|
BalancedJournal.CreateReversal(data.OriginalJournalEntry, $"Reversal: {cmd.Reason}", data.TimeStamp);
|
||||||
+4
-15
@@ -1,21 +1,10 @@
|
|||||||
using System;
|
using FinancialApi.Application.Commands;
|
||||||
using System.Collections.Generic;
|
using FinancialApi.Application.Events;
|
||||||
using System.Threading.Tasks;
|
using FinancialApi.Application.Queries;
|
||||||
using FinancialApi.Domain;
|
using FinancialApi.Domain;
|
||||||
using Wolverine.Persistence;
|
using Wolverine.Persistence;
|
||||||
|
|
||||||
namespace FinancialApi.Application;
|
namespace FinancialApi.Application.Handlers;
|
||||||
|
|
||||||
public interface IAccountQuery
|
|
||||||
{
|
|
||||||
Task<Account?> FindByIdAsync(int id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public record TransferFundsCommand(int SourceAccountId, int DestinationAccountId, decimal Amount);
|
|
||||||
|
|
||||||
public record FundsTransferredEvent(int SourceAccountId, int DestinationAccountId, decimal Amount);
|
|
||||||
|
|
||||||
public record TransferPair(Account Source, Account Destination, DateTimeOffset TimeStamp);
|
|
||||||
|
|
||||||
public static class TransferFundsHandler
|
public static class TransferFundsHandler
|
||||||
{
|
{
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using FinancialApi.Domain;
|
||||||
|
|
||||||
|
namespace FinancialApi.Application.Queries;
|
||||||
|
|
||||||
|
public interface IAccountQuery
|
||||||
|
{
|
||||||
|
Task<Account?> FindByIdAsync(int id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using FinancialApi.Application.Handlers;
|
||||||
|
|
||||||
|
namespace FinancialApi.Application.Queries;
|
||||||
|
|
||||||
|
public interface IReversalQuery
|
||||||
|
{
|
||||||
|
Task<ReversalData?> GetReversalDataAsync(Guid journalEntryId);
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
using FinancialApi.Domain;
|
||||||
|
|
||||||
|
namespace FinancialApi.Application;
|
||||||
|
|
||||||
|
public record TransferPair(Account Source, Account Destination, DateTimeOffset TimeStamp);
|
||||||
@@ -1,6 +1,4 @@
|
|||||||
using System;
|
namespace FinancialApi.Domain;
|
||||||
|
|
||||||
namespace FinancialApi.Domain;
|
|
||||||
|
|
||||||
public record Account(int Id, decimal Balance, string AccountType)
|
public record Account(int Id, decimal Balance, string AccountType)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace FinancialApi.Domain;
|
namespace FinancialApi.Domain;
|
||||||
|
|
||||||
public record BalancedJournal
|
public record BalancedJournal
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace FinancialApi.Domain;
|
namespace FinancialApi.Domain;
|
||||||
|
|
||||||
public record JournalEntry
|
public record JournalEntry
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System.Threading.Tasks;
|
using FinancialApi.Application.Queries;
|
||||||
using FinancialApi.Application;
|
|
||||||
using FinancialApi.Domain;
|
using FinancialApi.Domain;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
using System;
|
using FinancialApi.Application.Handlers;
|
||||||
using System.Linq;
|
using FinancialApi.Application.Queries;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using FinancialApi.Application;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace FinancialApi.Infrastructure;
|
namespace FinancialApi.Infrastructure;
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
using System;
|
using FinancialApi.Application.Events;
|
||||||
using System.Collections.Generic;
|
|
||||||
using FinancialApi.Application;
|
|
||||||
|
|
||||||
namespace FinancialApi;
|
namespace FinancialApi;
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,4 @@
|
|||||||
using System;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNetCore.Diagnostics;
|
using Microsoft.AspNetCore.Diagnostics;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace FinancialApi;
|
namespace FinancialApi;
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
@FinancialApi_HostAddress = https://localhost:7010
|
@FinancialApi_HostAddress = https://localhost:7010
|
||||||
|
|
||||||
GET {{FinancialApi_HostAddress}}/weatherforecast/
|
|
||||||
Accept: application/json
|
|
||||||
|
|
||||||
### Apply Fee to an Account
|
### Apply Fee to an Account
|
||||||
|
|
||||||
POST {{FinancialApi_HostAddress}}/accounts/apply-fee
|
POST {{FinancialApi_HostAddress}}/accounts/apply-fee
|
||||||
@@ -11,7 +8,7 @@ Content-Type: application/json
|
|||||||
|
|
||||||
{
|
{
|
||||||
"accountId": 1,
|
"accountId": 1,
|
||||||
"amount": 500.0
|
"amount": 50.0
|
||||||
}
|
}
|
||||||
|
|
||||||
### Transfer Between Accounts
|
### Transfer Between Accounts
|
||||||
@@ -31,7 +28,7 @@ POST {{FinancialApi_HostAddress}}/journal/reverse
|
|||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
|
|
||||||
{
|
{
|
||||||
"originalJournalId": "1443B0FC-F679-4445-A910-CC333F0925B7",
|
"originalJournalId": "FD2BFD8E-7C85-4CAD-A12B-CCBCD7C12B4A",
|
||||||
"reason": "Incorrect Account Fee"
|
"reason": "Incorrect Account Fee"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
using FinancialApi;
|
using FinancialApi;
|
||||||
using FinancialApi.Application;
|
using FinancialApi.Application.Commands;
|
||||||
|
using FinancialApi.Application.Events;
|
||||||
|
using FinancialApi.Application.Handlers;
|
||||||
|
using FinancialApi.Application.Queries;
|
||||||
using FinancialApi.Infrastructure;
|
using FinancialApi.Infrastructure;
|
||||||
using FinancialApi.ServiceDefaults;
|
using FinancialApi.ServiceDefaults;
|
||||||
using JasperFx.Resources;
|
using JasperFx.Resources;
|
||||||
using Microsoft.AspNetCore.Builder;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
|
||||||
using Wolverine;
|
using Wolverine;
|
||||||
using Wolverine.EntityFrameworkCore;
|
using Wolverine.EntityFrameworkCore;
|
||||||
using Wolverine.Sqlite;
|
using Wolverine.Sqlite;
|
||||||
@@ -85,6 +84,7 @@ app.MapGet("/events", (IEventTracker tracker) =>
|
|||||||
{
|
{
|
||||||
return Results.Ok(store.CapturedEvents);
|
return Results.Ok(store.CapturedEvents);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Results.NotFound();
|
return Results.NotFound();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user