Some fixes to the queries and context records.

This commit is contained in:
2026-07-30 08:17:18 +02:00
parent acc8423fc6
commit f8b37815b0
15 changed files with 255 additions and 29 deletions
@@ -1,8 +1,6 @@
using System.Security.Cryptography; using FinancialApi.Application.Commands;
using FinancialApi.Application.Commands;
using FinancialApi.Application.Handlers; using FinancialApi.Application.Handlers;
using FinancialApi.Application.Models; using FinancialApi.Application.Models;
using FinancialApi.Domain;
using FinancialApi.Domain.Entities; using FinancialApi.Domain.Entities;
using FluentAssertions; using FluentAssertions;
using FluentAssertions.Execution; using FluentAssertions.Execution;
@@ -2,7 +2,6 @@ using FinancialApi.Application.Commands;
using FinancialApi.Application.Events; using FinancialApi.Application.Events;
using FinancialApi.Application.Interfaces; using FinancialApi.Application.Interfaces;
using FinancialApi.Application.Models; using FinancialApi.Application.Models;
using FinancialApi.Domain;
using FinancialApi.Domain.Aggregates; using FinancialApi.Domain.Aggregates;
using FinancialApi.Domain.Entities; using FinancialApi.Domain.Entities;
using Wolverine.Persistence; using Wolverine.Persistence;
@@ -2,7 +2,6 @@ using FinancialApi.Application.Commands;
using FinancialApi.Application.Events; using FinancialApi.Application.Events;
using FinancialApi.Application.Interfaces; using FinancialApi.Application.Interfaces;
using FinancialApi.Application.Models; using FinancialApi.Application.Models;
using FinancialApi.Domain;
using FinancialApi.Domain.Aggregates; using FinancialApi.Domain.Aggregates;
using FinancialApi.Domain.Entities; using FinancialApi.Domain.Entities;
using Wolverine.Persistence; using Wolverine.Persistence;
@@ -17,8 +16,19 @@ public static class ReverseJournalHandler
TimeProvider timeProvider TimeProvider timeProvider
) )
{ {
var data = await query.GetReversalDataAsync(cmd.OriginalJournalId); var (journalEnty, accounts) = await query.GetReversalDataAsync(cmd.OriginalJournalId);
return data ?? throw new InvalidOperationException($"Journal Entry {cmd.OriginalJournalId} does not exist"); 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<JournalEntry> JournalWrite, UnitOfWork<Account> AccountWrites, JournalReversedEvent public static ( IStorageAction<JournalEntry> JournalWrite, UnitOfWork<Account> AccountWrites, JournalReversedEvent
@@ -2,7 +2,6 @@ using FinancialApi.Application.Commands;
using FinancialApi.Application.Events; using FinancialApi.Application.Events;
using FinancialApi.Application.Interfaces; using FinancialApi.Application.Interfaces;
using FinancialApi.Application.Models; using FinancialApi.Application.Models;
using FinancialApi.Domain;
using FinancialApi.Domain.Aggregates; using FinancialApi.Domain.Aggregates;
using FinancialApi.Domain.Entities; using FinancialApi.Domain.Entities;
using Wolverine.Persistence; using Wolverine.Persistence;
@@ -1,4 +1,3 @@
using FinancialApi.Domain;
using FinancialApi.Domain.Entities; using FinancialApi.Domain.Entities;
namespace FinancialApi.Application.Interfaces; namespace FinancialApi.Application.Interfaces;
@@ -1,10 +1,8 @@
using FinancialApi.Application.Commands; using FinancialApi.Domain.Entities;
using FinancialApi.Application.Handlers;
using FinancialApi.Application.Models;
namespace FinancialApi.Application.Interfaces; namespace FinancialApi.Application.Interfaces;
public interface IReversalQuery public interface IReversalQuery
{ {
Task<JournalReversalContext?> GetReversalDataAsync(Guid journalEntryId); Task<(JournalEntry?, List<Account>?)> GetReversalDataAsync(Guid journalEntryId);
} }
@@ -1,4 +1,3 @@
using FinancialApi.Domain;
using FinancialApi.Domain.Entities; using FinancialApi.Domain.Entities;
namespace FinancialApi.Application.Models; namespace FinancialApi.Application.Models;
@@ -1,4 +1,3 @@
using FinancialApi.Domain;
using FinancialApi.Domain.Entities; using FinancialApi.Domain.Entities;
namespace FinancialApi.Application.Models; namespace FinancialApi.Application.Models;
@@ -1,4 +1,3 @@
using FinancialApi.Domain;
using FinancialApi.Domain.Entities; using FinancialApi.Domain.Entities;
namespace FinancialApi.Application.Models; namespace FinancialApi.Application.Models;
@@ -0,0 +1,221 @@
namespace FinancialApi.Domain;
public interface IFinancialError
{
int ErrorCode { get; }
string Message { get; }
Exception? Exception { get; }
}
public sealed class FinancialError : IFinancialError
{
public FinancialError(int errorCode, string message, Exception? exception = null)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(errorCode);
ErrorCode = errorCode;
Message = message ?? throw new ArgumentNullException(nameof(message));
Exception = exception;
}
public int ErrorCode { get; }
public string Message { get; }
public Exception? Exception { get; }
public override string ToString() => $"Error: {ErrorCode} - {Message}{(Exception == null ? "" : $"\n{Exception}")}";
}
public sealed class FinancialResult<T>
{
private readonly T? _resultValue;
private readonly FinancialError? _resultError;
public FinancialResult(T resultValue)
{
IsError = false;
_resultValue = resultValue;
_resultError = null;
}
public FinancialResult(FinancialError resultError)
{
IsError = true;
_resultValue = default;
_resultError = resultError;
}
public bool IsError { get; }
/// <summary>
/// Error indicator
/// </summary>
public bool IsSuccess => !IsError;
/// <summary>
/// Result Values
/// </summary>
public T? Value => _resultValue;
public FinancialError? Error => _resultError;
public static implicit operator FinancialResult<T>(T resultValue) => new(resultValue);
public static implicit operator FinancialResult<T>(FinancialError resultError) => new(resultError);
public static FinancialResult<T> Success(T successValue)
{
return new FinancialResult<T>(successValue);
}
public static FinancialResult<T> Failure(FinancialError failureValue)
{
return new FinancialResult<T>(failureValue);
}
public TResult Match<TResult>(
Func<T, TResult> success,
Func<FinancialError, TResult> failure,
Func<TResult> nullValue
)
{
return IsError switch
{
false when _resultValue == null => nullValue(),
true => failure(_resultError!),
_ => success(_resultValue)
};
}
public TResult Match<TResult>(Func<T, TResult> success, Func<FinancialError, TResult> failure)
{
return IsError switch
{
true => failure(_resultError!),
_ => success(_resultValue!)
};
}
public TResult? Match<TResult>(Func<T, TResult> success)
{
return IsError switch
{
true => default,
_ => success(_resultValue!)
};
}
public void Match(Action<T> success, Action<FinancialError> failure, Action nullValue)
{
switch (IsError)
{
case false when _resultValue == null:
nullValue.Invoke();
return;
case true:
failure.Invoke(_resultError!);
return;
default:
success(_resultValue);
break;
}
}
public void Match(Action<T> success, Action<FinancialError> failure)
{
switch (IsError)
{
case true:
failure.Invoke(_resultError!);
return;
default:
success(_resultValue!);
break;
}
}
public void Match(Action<T> success)
{
if (IsError)
{
return;
}
success(_resultValue!);
}
public async Task<TResult> MatchAsync<TResult>(
Func<T, Task<TResult>> success,
Func<FinancialError, Task<TResult>> failure,
Func<Task<TResult>> nullValue
)
{
return IsError switch
{
false when _resultValue == null => await nullValue(),
true => await failure(_resultError!),
_ => await success(_resultValue)
};
}
public async Task<TResult> MatchAsync<TResult>(
Func<T, Task<TResult>> success,
Func<FinancialError, Task<TResult>> failure
)
{
return IsError switch
{
true => await failure(_resultError!),
_ => await success(_resultValue!)
};
}
public async Task<TResult?> MatchAsync<TResult>(Func<T, Task<TResult>> success)
{
return IsError switch
{
true => default,
_ => await success(_resultValue!)
};
}
public async Task MatchAsync(Func<T, Task> success, Func<FinancialError, Task> failure, Func<Task> nullValue)
{
switch (IsError)
{
case false when _resultValue == null:
await nullValue.Invoke();
return;
case true:
await failure.Invoke(_resultError!);
return;
default:
await success(_resultValue!);
break;
}
}
public async Task MatchAsync(Func<T, Task> success, Func<FinancialError, Task> failure)
{
switch (IsError)
{
case true:
await failure.Invoke(_resultError!);
return;
default:
await success(_resultValue!);
break;
}
}
public async Task MatchAsync(Func<T, Task> success)
{
if (IsError)
{
return;
}
await success(_resultValue!);
}
}
@@ -1,5 +1,4 @@
using FinancialApi.Domain; using FinancialApi.Domain.Entities;
using FinancialApi.Domain.Entities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace FinancialApi.Infrastructure; namespace FinancialApi.Infrastructure;
@@ -1,5 +1,4 @@
using FinancialApi.Application.Interfaces; using FinancialApi.Application.Interfaces;
using FinancialApi.Domain;
using FinancialApi.Domain.Entities; using FinancialApi.Domain.Entities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -1,14 +1,12 @@
using FinancialApi.Application.Commands;
using FinancialApi.Application.Handlers;
using FinancialApi.Application.Interfaces; using FinancialApi.Application.Interfaces;
using FinancialApi.Application.Models; using FinancialApi.Domain.Entities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace FinancialApi.Infrastructure; namespace FinancialApi.Infrastructure;
public class ReversalQuery(AccountDbContext db, TimeProvider timeProvider) : IReversalQuery public class ReversalQuery(AccountDbContext db) : IReversalQuery
{ {
public async Task<JournalReversalContext?> GetReversalDataAsync(Guid journalEntryId) public async Task<(JournalEntry?, List<Account>?)> GetReversalDataAsync(Guid journalEntryId)
{ {
var entry = await db.JournalEntries.Include(x => x.Lines) var entry = await db.JournalEntries.Include(x => x.Lines)
.AsNoTracking() .AsNoTracking()
@@ -16,7 +14,7 @@ public class ReversalQuery(AccountDbContext db, TimeProvider timeProvider) : IRe
if (entry == null) if (entry == null)
{ {
return null; return (null, null);
} }
var accountIds = entry.Lines.Select(l => l.AccountId) var accountIds = entry.Lines.Select(l => l.AccountId)
@@ -27,6 +25,6 @@ public class ReversalQuery(AccountDbContext db, TimeProvider timeProvider) : IRe
.Where(a => accountIds.Contains(a.Id)) .Where(a => accountIds.Contains(a.Id))
.ToListAsync(); .ToListAsync();
return new JournalReversalContext(entry, accounts, timeProvider.GetUtcNow()); return (entry, accounts);
} }
} }
@@ -1,8 +1,11 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AConfiguredTaskAwaitable_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2026_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fe929cb9673c947ad9269facdac90006adb3400_003F14_003Fd939fe0a_003FConfiguredTaskAwaitable_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AConstructorBindingFactory_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2026_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fda476e261fcf49b394c1074b25b4cdce2b2960_003Fd4_003Fb1aa95c1_003FConstructorBindingFactory_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AConstructorBindingFactory_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2026_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fda476e261fcf49b394c1074b25b4cdce2b2960_003Fd4_003Fb1aa95c1_003FConstructorBindingFactory_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ALateBoundTestFramework_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2026_002E1_003Fresharper_002Dhost_003FSourcesCache_003Fcffa676d9bfc6c7a1c9d0d25ead15b3b0c9e12e3187bf397eaa52260e5fd91_003FLateBoundTestFramework_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ALateBoundTestFramework_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2026_002E1_003Fresharper_002Dhost_003FSourcesCache_003Fcffa676d9bfc6c7a1c9d0d25ead15b3b0c9e12e3187bf397eaa52260e5fd91_003FLateBoundTestFramework_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AMessageContext_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2026_002E2_003Fresharper_002Dhost_003FSourcesCache_003Fb263759e5fefb34bcd589e63aeba791cef6f591ecbc93d1a7e62befa9e8bc6_003FMessageContext_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARelationalLoggerExtensions_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2026_002E2_003Fresharper_002Dhost_003FSourcesCache_003Fb797502679b4aa8376708777b4db8d11e31a798d7e6bead76c15bdfa30834892_003FRelationalLoggerExtensions_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARelationalLoggerExtensions_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2026_002E2_003Fresharper_002Dhost_003FSourcesCache_003Fb797502679b4aa8376708777b4db8d11e31a798d7e6bead76c15bdfa30834892_003FRelationalLoggerExtensions_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AWolverineRuntime_002EHostService_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2026_002E1_003Fresharper_002Dhost_003FSourcesCache_003F97bae8f1ffdd86c14edef353d7a85d3d3773b56c68e3a62e8815d1afaacbe797_003FWolverineRuntime_002EHostService_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AWolverineRuntime_002EHostService_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2026_002E1_003Fresharper_002Dhost_003FSourcesCache_003F97bae8f1ffdd86c14edef353d7a85d3d3773b56c68e3a62e8815d1afaacbe797_003FWolverineRuntime_002EHostService_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=b53aee56_002D76e6_002D4473_002D9a54_002Dc62cf61dc263/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="Test1" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt; <s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=ea7499a6_002Ddeba_002D4139_002Dac76_002D505e3f7f2642/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Solution /&gt; &lt;Solution /&gt;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary> &lt;/SessionState&gt;</s:String>
</wpf:ResourceDictionary>
+7 -1
View File
@@ -1,4 +1,4 @@
[tools] [tools]
dotnet = "10" dotnet = "10"
[tasks.restore] [tasks.restore]
@@ -12,3 +12,9 @@ run = "dotnet build"
[tasks.clean] [tasks.clean]
run = "fd -I \"bin|obj\" -td -X rm -r && dotnet clean" run = "fd -I \"bin|obj\" -td -X rm -r && dotnet clean"
[tasks.migrate]
usage = '''
arg "<name>" help="Migration Name"
'''
run = "dotnet ef migrations add \"${name?}\" --startup-project FinancialApi --project FinancialApi.Infrastructure"