feat(my-bets): personal bet journal with CLV tracking

Adds a manual bet-tracking journal that turns the analyzer into an actual
bet tracker. Users record wagers; the journal auto-grades them when event
results land and computes per-bet Closing-Line-Value against the latest
pre-match snapshot — the strongest long-run indicator of betting skill.

Domain:
- PlacedBet entity (reuses Bet vocabulary for Scope/Type/Side/Value/Rate)
  with stake, placed-at, outcome, and notes. Derived GrossReturn / NetProfit.
- BetOutcome enum (Pending / Won / Lost / Void).
- BetOutcomeResolver: pure function grading any Match-scope bet against an
  EventResult. Handles 1X2, draws, handicap (incl. push), and totals.
  Period-scope bets stay manual since EventResult only carries full-time.

Application:
- IPlacedBetRepository abstraction.
- ClosingLineValueCalculator: pure CLV math (implied-probability delta) +
  snapshot-matching predicate by Scope/Type/Side/Value.
- BetJournalReport + BetJournalStats records.
- Four use cases: Record / ResolvePending / BuildReport / Delete.
- New ISnapshotRepository.GetLatestPreMatchAsync pushes the closing-line
  pick into a single SQLite query rather than materialising the 30-day
  window in memory per event.
- ROI turnover excludes Void stakes — pushes are not real turnover and
  including them would dilute the user's edge.

Infrastructure:
- PlacedBetEntity / Configuration / Repository / Mapping helpers.
- 20260516 migration adding the PlacedBets table with EventCode and
  Outcome indices. Intentionally NO foreign key to Events — the journal
  is user data and must survive snapshot-retention pruning. Covered by an
  explicit round-trip test.

UI:
- Pages/MyBets/Journal.razor: hero header, 4-card KPI strip (ROI / strike
  rate / avg CLV / net profit, tinted by tone), inline add-bet form with
  the same invariants as the Bet record, drill-down table with per-row
  outcome pills, CLV percentage-points column, P&L, notes underline, and
  inline-confirm delete. RU + EN i18n.
- Nav entry under Analysis.

Tests: +55 across Domain / Application / Infrastructure (resolver math
including handicap push and total push boundaries, PlacedBet invariants
and derived properties, CLV math + null-handling, four use cases under
NSubstitute, EF round-trip including survives-event-deletion). All 379
tests pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-16 17:45:42 +03:00
parent 292223174c
commit 1ad896b07e
36 changed files with 3315 additions and 0 deletions
@@ -0,0 +1,32 @@
using Marathon.Application.Storage;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
namespace Marathon.Application.Abstractions;
/// <summary>
/// Repository for <see cref="PlacedBet"/> domain entities — the user-tracked
/// betting journal.
/// </summary>
public interface IPlacedBetRepository : IRepository<Guid, PlacedBet>
{
/// <summary>
/// Bets matching <paramref name="outcome"/>. Used by the resolver use case
/// to scan only <see cref="BetOutcome.Pending"/> rows on each pass.
/// </summary>
Task<IReadOnlyList<PlacedBet>> ListByOutcomeAsync(BetOutcome outcome, CancellationToken ct = default);
/// <summary>
/// Bets whose <see cref="PlacedBet.PlacedAt"/> falls within
/// <paramref name="range"/>. Used by the journal page when the user filters
/// by date.
/// </summary>
Task<IReadOnlyList<PlacedBet>> ListByDateRangeAsync(DateRange range, CancellationToken ct = default);
/// <summary>
/// Every bet recorded against <paramref name="eventId"/>. Used by the event
/// detail page to show "you have N bets on this match".
/// </summary>
Task<IReadOnlyList<PlacedBet>> ListByEventAsync(EventId eventId, CancellationToken ct = default);
}
@@ -36,4 +36,19 @@ public interface ISnapshotRepository
Task AddAsync(OddsSnapshot entity, CancellationToken ct = default);
Task SaveChangesAsync(CancellationToken ct = default);
/// <summary>
/// Returns the latest pre-match snapshot for <paramref name="eventId"/> whose
/// <see cref="OddsSnapshot.CapturedAt"/> is at or before
/// <paramref name="atOrBefore"/>, or <c>null</c> if none exists. Used by the
/// bet-journal use case as the "closing line" reference for CLV.
/// </summary>
/// <remarks>
/// Pushes the ORDER BY + LIMIT 1 down to SQLite so we do not materialise
/// every snapshot in the 30-day pre-match window just to pick one.
/// </remarks>
Task<OddsSnapshot?> GetLatestPreMatchAsync(
EventId eventId,
DateTimeOffset atOrBefore,
CancellationToken ct = default);
}
@@ -32,6 +32,11 @@ public static class ApplicationModule
services.AddScoped<DetectAnomaliesUseCase>();
services.AddScoped<EvaluateAnomalyOutcomesUseCase>();
services.AddScoped<RecordPlacedBetUseCase>();
services.AddScoped<ResolvePendingBetsUseCase>();
services.AddScoped<BuildBetJournalReportUseCase>();
services.AddScoped<DeletePlacedBetUseCase>();
return services;
}
}
@@ -0,0 +1,92 @@
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
namespace Marathon.Application.Betting;
/// <summary>
/// Aggregate report on the user's bet-tracking journal — totals, P&amp;L, and
/// per-bet CLV. Consumed by the Journal page; built by
/// <see cref="UseCases.BuildBetJournalReportUseCase"/>.
/// </summary>
/// <param name="Stats">Roll-up of stake / profit / hit rate / CLV across all bets in scope.</param>
/// <param name="Bets">
/// Every bet paired with its computed CLV (null when no closing snapshot was
/// available). Ordered most-recent <see cref="PlacedBet.PlacedAt"/> first.
/// </param>
public sealed record BetJournalReport(
BetJournalStats Stats,
IReadOnlyList<BetJournalRow> Bets);
/// <summary>
/// One row in the journal — a domain <see cref="PlacedBet"/> plus the CLV
/// computed against the closing pre-match snapshot.
/// </summary>
/// <param name="Bet">The domain bet exactly as persisted.</param>
/// <param name="ClvProbabilityDelta">
/// Closing-line value as an implied-probability delta in roughly [-1, 1].
/// Positive means the user took a better price than the closing line; null
/// when no matching bet existed in the closing snapshot.
/// </param>
public sealed record BetJournalRow(
PlacedBet Bet,
decimal? ClvProbabilityDelta);
/// <summary>
/// Aggregate statistics across a set of <see cref="PlacedBet"/>.
/// All money values share the user's currency — the domain does not encode one.
/// </summary>
/// <param name="TotalBets">Every bet in scope, regardless of outcome.</param>
/// <param name="PendingCount">Bets still awaiting settlement.</param>
/// <param name="WonCount">Settled wins.</param>
/// <param name="LostCount">Settled losses.</param>
/// <param name="VoidCount">Settled pushes / void grades.</param>
/// <param name="TotalStaked">
/// Turnover that contributes to ROI: sum of <see cref="PlacedBet.Stake"/> across
/// <b>Won and Lost</b> bets only. Void (push) and Pending bets are excluded — a
/// returned stake is not real turnover and counting it would dilute ROI.
/// </param>
/// <param name="TotalReturned">
/// Sum of <see cref="PlacedBet.GrossReturn"/> across the same Won + Lost subset
/// that feeds <see cref="TotalStaked"/>.
/// </param>
/// <param name="NetProfit"><c>TotalReturned TotalStaked</c>.</param>
/// <param name="RoiPercent">
/// <c>NetProfit / TotalStaked × 100</c>. Null when no bets have resolved yet.
/// </param>
/// <param name="StrikeRatePercent">
/// <c>WonCount / (WonCount + LostCount) × 100</c> — excludes voids and pendings.
/// Null when no settled win/loss exists yet.
/// </param>
/// <param name="AverageClvProbabilityDelta">
/// Mean CLV across bets where CLV was computable. Null when no comparable
/// closing snapshot was available for any bet.
/// </param>
public sealed record BetJournalStats(
int TotalBets,
int PendingCount,
int WonCount,
int LostCount,
int VoidCount,
decimal TotalStaked,
decimal TotalReturned,
decimal NetProfit,
decimal? RoiPercent,
decimal? StrikeRatePercent,
decimal? AverageClvProbabilityDelta)
{
/// <summary>Convenience: WonCount + LostCount + VoidCount.</summary>
public int ResolvedCount => WonCount + LostCount + VoidCount;
public static BetJournalStats Empty { get; } = new(
TotalBets: 0,
PendingCount: 0,
WonCount: 0,
LostCount: 0,
VoidCount: 0,
TotalStaked: 0m,
TotalReturned: 0m,
NetProfit: 0m,
RoiPercent: null,
StrikeRatePercent: null,
AverageClvProbabilityDelta: null);
}
@@ -0,0 +1,85 @@
using Marathon.Domain.Entities;
using Marathon.Domain.ValueObjects;
namespace Marathon.Application.Betting;
/// <summary>
/// Pure helper that computes Closing Line Value (CLV) for a placed bet.
/// </summary>
/// <remarks>
/// <para>
/// CLV measures how much better (or worse) the rate the user took was compared
/// with the bookmaker's last pre-match price on the same selection. It is the
/// single best long-run indicator of betting skill — positive CLV correlates
/// with positive expected value regardless of any individual bet's outcome.
/// </para>
/// <para>
/// Formula (implied-probability delta):
/// <list type="bullet">
/// <item>Taken implied probability: <c>p_t = 1 / takenRate</c></item>
/// <item>Closing implied probability: <c>p_c = 1 / closeRate</c></item>
/// <item><c>CLV = p_c p_t</c></item>
/// </list>
/// Positive CLV means the closing price implied higher probability for the
/// selection than the price the user took — i.e. the line moved in the user's
/// favour after they placed the bet.
/// </para>
/// <para>
/// Returns <c>null</c> when no matching bet (same Scope / Type / Side / Value)
/// can be found in the closing snapshot — typically because the market closed
/// before the bookmaker exposed a comparable line, or the snapshot store has
/// gaps. UI consumers must distinguish "no data" from "0% CLV".
/// </para>
/// </remarks>
public static class ClosingLineValueCalculator
{
/// <summary>
/// Computes CLV (implied-probability delta) given the rate the user took
/// and the rate present in the closing pre-match snapshot for the same
/// selection. Both must be positive — invariants on <see cref="OddsRate"/>
/// already guarantee this for inputs sourced from the domain.
/// </summary>
public static decimal Compute(decimal takenRate, decimal closingRate)
{
if (takenRate <= 0m)
throw new ArgumentOutOfRangeException(nameof(takenRate), takenRate, "Must be positive.");
if (closingRate <= 0m)
throw new ArgumentOutOfRangeException(nameof(closingRate), closingRate, "Must be positive.");
var takenProb = 1m / takenRate;
var closingProb = 1m / closingRate;
// Round to 6 decimals — beyond that is noise from the round-trip.
return Math.Round(closingProb - takenProb, 6);
}
/// <summary>
/// Convenience overload: finds the matching <see cref="Bet"/> in
/// <paramref name="closingSnapshot"/> by Scope / Type / Side / Value, then
/// computes CLV against <paramref name="takenRate"/>. Returns <c>null</c>
/// when no comparable bet is present.
/// </summary>
public static decimal? TryCompute(
decimal takenRate,
Bet placedSelection,
OddsSnapshot? closingSnapshot)
{
ArgumentNullException.ThrowIfNull(placedSelection);
if (closingSnapshot is null) return null;
var match = closingSnapshot.Bets.FirstOrDefault(b =>
b.Scope.Equals(placedSelection.Scope) &&
b.Type == placedSelection.Type &&
b.Side == placedSelection.Side &&
NullableValuesEqual(b.Value, placedSelection.Value));
return match is null ? null : Compute(takenRate, match.Rate.Value);
}
private static bool NullableValuesEqual(OddsValue? a, OddsValue? b)
{
if (a is null && b is null) return true;
if (a is null || b is null) return false;
return a.Value == b.Value;
}
}
@@ -0,0 +1,172 @@
using Marathon.Application.Abstractions;
using Marathon.Application.Betting;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Microsoft.Extensions.Logging;
using DomainEventId = Marathon.Domain.ValueObjects.EventId;
namespace Marathon.Application.UseCases;
/// <summary>
/// Builds a <see cref="BetJournalReport"/>: every persisted bet paired with its
/// Closing-Line-Value, plus aggregate <see cref="BetJournalStats"/>.
/// </summary>
/// <remarks>
/// <para>
/// Closing-line lookup: for each distinct event in the journal, this use case
/// queries pre-match snapshots within a window that ends at the event's
/// <see cref="Event.ScheduledAt"/> and picks the latest snapshot whose
/// <see cref="OddsSnapshot.CapturedAt"/> is still before kickoff. That snapshot
/// is the "close" for CLV purposes.
/// </para>
/// <para>
/// If the snapshot store has nothing within the lookback window, the bet
/// receives a null CLV. Stats then exclude it from the average.
/// </para>
/// </remarks>
public sealed class BuildBetJournalReportUseCase
{
private readonly IPlacedBetRepository _bets;
private readonly IEventRepository _events;
private readonly ISnapshotRepository _snapshots;
private readonly ILogger<BuildBetJournalReportUseCase> _logger;
public BuildBetJournalReportUseCase(
IPlacedBetRepository bets,
IEventRepository events,
ISnapshotRepository snapshots,
ILogger<BuildBetJournalReportUseCase> logger)
{
_bets = bets ?? throw new ArgumentNullException(nameof(bets));
_events = events ?? throw new ArgumentNullException(nameof(events));
_snapshots = snapshots ?? throw new ArgumentNullException(nameof(snapshots));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<BetJournalReport> ExecuteAsync(CancellationToken ct = default)
{
var bets = await _bets.ListAsync(ct).ConfigureAwait(false);
if (bets.Count == 0)
{
_logger.LogInformation("BuildBetJournalReportUseCase: no bets — empty report");
return new BetJournalReport(BetJournalStats.Empty, Array.Empty<BetJournalRow>());
}
var distinctEventIds = bets.Select(b => b.EventId).Distinct().ToList();
// Resolve closing snapshot per event using a single-row repo call —
// pushes the ORDER BY / LIMIT 1 down to SQLite rather than materialising
// every snapshot in a 30-day window.
var closingByEvent = new Dictionary<DomainEventId, OddsSnapshot?>(distinctEventIds.Count);
foreach (var eventId in distinctEventIds)
{
ct.ThrowIfCancellationRequested();
var ev = await _events.GetAsync(eventId, ct).ConfigureAwait(false);
if (ev is null)
{
closingByEvent[eventId] = null;
continue;
}
var closing = await _snapshots
.GetLatestPreMatchAsync(eventId, ev.ScheduledAt, ct)
.ConfigureAwait(false);
closingByEvent[eventId] = closing;
}
var rows = new List<BetJournalRow>(bets.Count);
foreach (var bet in bets)
{
ct.ThrowIfCancellationRequested();
closingByEvent.TryGetValue(bet.EventId, out var closing);
var clv = ClosingLineValueCalculator.TryCompute(
takenRate: bet.Selection.Rate.Value,
placedSelection: bet.Selection,
closingSnapshot: closing);
rows.Add(new BetJournalRow(bet, clv));
}
rows.Sort((a, b) => b.Bet.PlacedAt.CompareTo(a.Bet.PlacedAt));
var stats = ComputeStats(rows);
_logger.LogInformation(
"BuildBetJournalReportUseCase: report built — {Total} bets, {Resolved} resolved, ROI={Roi:0.##}%",
stats.TotalBets, stats.ResolvedCount, stats.RoiPercent ?? 0m);
return new BetJournalReport(stats, rows);
}
private static BetJournalStats ComputeStats(IReadOnlyList<BetJournalRow> rows)
{
if (rows.Count == 0) return BetJournalStats.Empty;
var pending = 0;
var won = 0;
var lost = 0;
var voided = 0;
// Industry-standard ROI excludes pushes from turnover — staking on a Void
// bet returns the stake and is functionally a no-op, so counting it as
// turnover dilutes the ROI denominator and understates the user's edge.
// Only Won + Lost contribute to TotalStaked / TotalReturned.
var totalStaked = 0m;
var totalReturned = 0m;
decimal clvSum = 0m;
var clvCount = 0;
foreach (var row in rows)
{
switch (row.Bet.Outcome)
{
case BetOutcome.Pending: pending++; break;
case BetOutcome.Won: won++; break;
case BetOutcome.Lost: lost++; break;
case BetOutcome.Void: voided++; break;
}
if (row.Bet.Outcome is BetOutcome.Won or BetOutcome.Lost)
{
totalStaked += row.Bet.Stake;
totalReturned += row.Bet.GrossReturn ?? 0m;
}
if (row.ClvProbabilityDelta is { } clv)
{
clvSum += clv;
clvCount++;
}
}
var netProfit = totalReturned - totalStaked;
var winLoss = won + lost;
decimal? roi = totalStaked > 0m
? Math.Round((netProfit / totalStaked) * 100m, 2)
: null;
decimal? strikeRate = winLoss > 0
? Math.Round(((decimal)won / winLoss) * 100m, 2)
: null;
// CLV inputs are already 6-decimal-rounded by ClosingLineValueCalculator;
// round the mean only at the display boundary to avoid compounding bias.
decimal? avgClv = clvCount > 0
? clvSum / clvCount
: null;
return new BetJournalStats(
TotalBets: rows.Count,
PendingCount: pending,
WonCount: won,
LostCount: lost,
VoidCount: voided,
TotalStaked: totalStaked,
TotalReturned: totalReturned,
NetProfit: netProfit,
RoiPercent: roi,
StrikeRatePercent: strikeRate,
AverageClvProbabilityDelta: avgClv);
}
}
@@ -0,0 +1,29 @@
using Marathon.Application.Abstractions;
using Microsoft.Extensions.Logging;
namespace Marathon.Application.UseCases;
/// <summary>
/// Removes a <see cref="Marathon.Domain.Entities.PlacedBet"/> from the journal
/// by its identifier. Silent no-op when the id does not exist.
/// </summary>
public sealed class DeletePlacedBetUseCase
{
private readonly IPlacedBetRepository _bets;
private readonly ILogger<DeletePlacedBetUseCase> _logger;
public DeletePlacedBetUseCase(
IPlacedBetRepository bets,
ILogger<DeletePlacedBetUseCase> logger)
{
_bets = bets ?? throw new ArgumentNullException(nameof(bets));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task ExecuteAsync(Guid betId, CancellationToken ct = default)
{
await _bets.DeleteAsync(betId, ct).ConfigureAwait(false);
await _bets.SaveChangesAsync(ct).ConfigureAwait(false);
_logger.LogInformation("DeletePlacedBetUseCase: removed bet {BetId}", betId);
}
}
@@ -0,0 +1,90 @@
using Marathon.Application.Abstractions;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Microsoft.Extensions.Logging;
using DomainEventId = Marathon.Domain.ValueObjects.EventId;
namespace Marathon.Application.UseCases;
/// <summary>
/// Records a new <see cref="PlacedBet"/> entered manually via the Journal UI.
/// </summary>
/// <remarks>
/// <para>
/// The use case validates that the referenced event exists, then persists the
/// bet. If the event already has a final result the bet is graded on the spot
/// via <see cref="Marathon.Domain.Betting.BetOutcomeResolver"/> — saves the
/// user a round-trip to the resolver page when entering historical wagers.
/// </para>
/// </remarks>
public sealed class RecordPlacedBetUseCase
{
private readonly IPlacedBetRepository _bets;
private readonly IEventRepository _events;
private readonly IResultRepository _results;
private readonly ILogger<RecordPlacedBetUseCase> _logger;
public RecordPlacedBetUseCase(
IPlacedBetRepository bets,
IEventRepository events,
IResultRepository results,
ILogger<RecordPlacedBetUseCase> logger)
{
_bets = bets ?? throw new ArgumentNullException(nameof(bets));
_events = events ?? throw new ArgumentNullException(nameof(events));
_results = results ?? throw new ArgumentNullException(nameof(results));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
/// <summary>
/// Persists <paramref name="bet"/>. Returns the bet as stored — if the
/// event already has a result, the returned instance reflects the graded
/// <see cref="BetOutcome"/>.
/// </summary>
/// <exception cref="InvalidOperationException">
/// The bet references an unknown event. The journal does not allow free-form
/// event codes — wagers must be on events the scraper has captured so the
/// CLV calculator can compare against the closing snapshot.
/// </exception>
public async Task<PlacedBet> ExecuteAsync(PlacedBet bet, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(bet);
// Confirm the event exists in the local store.
var ev = await _events.GetAsync(bet.EventId, ct).ConfigureAwait(false);
if (ev is null)
{
throw new InvalidOperationException(
$"Cannot record a bet on unknown event '{bet.EventId.Value}'. " +
"The event must already be present in the scrape store.");
}
var toPersist = bet;
// Auto-grade if a result is already available.
if (bet.Outcome == BetOutcome.Pending)
{
var result = await _results.GetAsync(bet.EventId, ct).ConfigureAwait(false);
if (result is not null)
{
var graded = Marathon.Domain.Betting.BetOutcomeResolver.Resolve(bet.Selection, result);
if (graded is not null)
{
toPersist = bet.WithOutcome(graded.Value);
_logger.LogInformation(
"RecordPlacedBetUseCase: bet {BetId} on event {EventId} auto-graded as {Outcome}",
toPersist.Id, ((DomainEventId)toPersist.EventId).Value, graded.Value);
}
}
}
await _bets.AddAsync(toPersist, ct).ConfigureAwait(false);
await _bets.SaveChangesAsync(ct).ConfigureAwait(false);
_logger.LogInformation(
"RecordPlacedBetUseCase: persisted bet {BetId} on event {EventId} stake={Stake} rate={Rate}",
toPersist.Id, ((DomainEventId)toPersist.EventId).Value, toPersist.Stake, toPersist.Selection.Rate.Value);
return toPersist;
}
}
@@ -0,0 +1,84 @@
using Marathon.Application.Abstractions;
using Marathon.Domain.Betting;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Microsoft.Extensions.Logging;
using DomainEventId = Marathon.Domain.ValueObjects.EventId;
namespace Marathon.Application.UseCases;
/// <summary>
/// Sweeps the journal for <see cref="BetOutcome.Pending"/> bets whose events
/// have been graded, and updates them in bulk via
/// <see cref="BetOutcomeResolver"/>.
/// </summary>
/// <remarks>
/// Called on demand from the Journal page's "Resolve pending" button. The
/// design is idempotent — bets that cannot be auto-graded (period-scope, or
/// no result yet) are left untouched and surface again on the next pass.
/// </remarks>
public sealed class ResolvePendingBetsUseCase
{
private readonly IPlacedBetRepository _bets;
private readonly IResultRepository _results;
private readonly ILogger<ResolvePendingBetsUseCase> _logger;
public ResolvePendingBetsUseCase(
IPlacedBetRepository bets,
IResultRepository results,
ILogger<ResolvePendingBetsUseCase> logger)
{
_bets = bets ?? throw new ArgumentNullException(nameof(bets));
_results = results ?? throw new ArgumentNullException(nameof(results));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
/// <summary>
/// Returns the number of bets that were transitioned out of Pending in this pass.
/// </summary>
public async Task<int> ExecuteAsync(CancellationToken ct = default)
{
var pending = await _bets.ListByOutcomeAsync(BetOutcome.Pending, ct).ConfigureAwait(false);
if (pending.Count == 0)
{
_logger.LogInformation("ResolvePendingBetsUseCase: no pending bets");
return 0;
}
// Cache results per event so we do not re-query for each bet on the same event.
var resultCache = new Dictionary<DomainEventId, EventResult?>();
var resolvedCount = 0;
foreach (var bet in pending)
{
ct.ThrowIfCancellationRequested();
if (!resultCache.TryGetValue(bet.EventId, out var result))
{
result = await _results.GetAsync(bet.EventId, ct).ConfigureAwait(false);
resultCache[bet.EventId] = result;
}
if (result is null) continue;
var graded = BetOutcomeResolver.Resolve(bet.Selection, result);
if (graded is null) continue;
var updated = bet.WithOutcome(graded.Value);
await _bets.UpdateAsync(updated, ct).ConfigureAwait(false);
resolvedCount++;
}
// Save before logging — if the batch fails, an exception bubbles out and
// the success-count log is never emitted; we never report a graded count
// that was rolled back.
if (resolvedCount > 0)
await _bets.SaveChangesAsync(ct).ConfigureAwait(false);
_logger.LogInformation(
"ResolvePendingBetsUseCase: graded {Resolved} of {Pending} pending bets",
resolvedCount, pending.Count);
return resolvedCount;
}
}
@@ -0,0 +1,95 @@
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
namespace Marathon.Domain.Betting;
/// <summary>
/// Pure function that grades a <see cref="Bet"/> selection against a final
/// <see cref="EventResult"/>. Used by the bet-journal resolver to auto-settle
/// pending wagers the moment a result lands.
/// </summary>
/// <remarks>
/// <para>
/// Grading rules:
/// <list type="bullet">
/// <item><c>Win</c> (Side1/Side2): selection wins iff <c>WinnerSide</c> matches the side.</item>
/// <item><c>Draw</c>: wins iff <c>WinnerSide == Draw</c>.</item>
/// <item><c>WinFora</c> with handicap <c>h</c> on side S: adjusted S-score
/// = <c>S.Score + h</c>. Wins when adjusted > opponent, voids on tie, loses otherwise.</item>
/// <item><c>Total</c> with threshold <c>t</c>: combined = <c>Side1Score + Side2Score</c>.
/// <c>More</c> wins when combined > t, voids on equal, loses when less.
/// <c>Less</c> is the mirror image.</item>
/// </list>
/// </para>
/// <para>
/// Returns <c>null</c> when the bet cannot be graded against this result —
/// today only period-scope selections, because <see cref="EventResult"/> stores
/// the full-time score only. Callers must leave such bets in
/// <see cref="BetOutcome.Pending"/> for manual settlement.
/// </para>
/// </remarks>
public static class BetOutcomeResolver
{
/// <summary>
/// Grades <paramref name="selection"/> against <paramref name="result"/>.
/// Returns the resulting <see cref="BetOutcome"/> or <c>null</c> if the
/// bet shape cannot be auto-resolved from the available result data.
/// </summary>
public static BetOutcome? Resolve(Bet selection, EventResult result)
{
ArgumentNullException.ThrowIfNull(selection);
ArgumentNullException.ThrowIfNull(result);
// Period-scope bets need per-period scores which EventResult does not
// carry today — leave for manual grading.
if (selection.Scope is not MatchScope)
return null;
return selection.Type switch
{
BetType.Win => ResolveWin(selection.Side, result),
BetType.Draw => ResolveDraw(result),
BetType.WinFora => ResolveFora(selection.Side, selection.Value!.Value, result),
BetType.Total => ResolveTotal(selection.Side, selection.Value!.Value, result),
_ => null,
};
}
private static BetOutcome ResolveWin(Side side, EventResult result) =>
result.WinnerSide == side ? BetOutcome.Won : BetOutcome.Lost;
private static BetOutcome ResolveDraw(EventResult result) =>
result.WinnerSide == Side.Draw ? BetOutcome.Won : BetOutcome.Lost;
private static BetOutcome ResolveFora(Side side, decimal handicap, EventResult result)
{
// Adjusted score for the side that took the handicap.
var (own, opponent) = side == Side.Side1
? (result.Side1Score, result.Side2Score)
: (result.Side2Score, result.Side1Score);
var adjusted = own + handicap;
if (adjusted > opponent) return BetOutcome.Won;
if (adjusted == opponent) return BetOutcome.Void;
return BetOutcome.Lost;
}
private static BetOutcome ResolveTotal(Side side, decimal threshold, EventResult result)
{
var total = (decimal)(result.Side1Score + result.Side2Score);
// More wins when total > threshold; Less wins when total < threshold.
// Equality is a push (Void) for both sides.
if (total == threshold) return BetOutcome.Void;
var totalIsOver = total > threshold;
return side switch
{
Side.More => totalIsOver ? BetOutcome.Won : BetOutcome.Lost,
Side.Less => totalIsOver ? BetOutcome.Lost : BetOutcome.Won,
_ => BetOutcome.Lost, // Defensive — Bet invariant rejects other sides for Total.
};
}
}
+89
View File
@@ -0,0 +1,89 @@
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
namespace Marathon.Domain.Entities;
/// <summary>
/// A wager the user manually recorded as having placed (with this or another
/// bookmaker). Reuses the <see cref="Bet"/> vocabulary so the journal can mirror
/// scraped markets directly — same Scope / Type / Side / Value / Rate invariants
/// apply to <see cref="Selection"/>.
/// </summary>
/// <param name="Id">Stable identifier — Guid so duplicates can be detected by the UI.</param>
/// <param name="EventId">Event the wager is on.</param>
/// <param name="Selection">
/// The market + rate the user took. <c>Selection.Rate</c> is the "taken rate"
/// used for ROI and CLV calculations.
/// </param>
/// <param name="Stake">
/// Money risked, in the user's currency. The domain does not encode currency —
/// stake values are compared as raw decimals.
/// </param>
/// <param name="PlacedAt">When the bet was recorded. Stored as Moscow time.</param>
/// <param name="Outcome">Current settlement state — see <see cref="BetOutcome"/>.</param>
/// <param name="Notes">Optional free text — strategy tag, source, etc.</param>
public sealed record PlacedBet(
Guid Id,
EventId EventId,
Bet Selection,
decimal Stake,
DateTimeOffset PlacedAt,
BetOutcome Outcome,
string? Notes)
{
public Guid Id { get; } = Id == Guid.Empty
? throw new ArgumentException("PlacedBet Id must not be an empty GUID.", nameof(Id))
: Id;
public EventId EventId { get; } = EventId ?? throw new ArgumentNullException(nameof(EventId));
public Bet Selection { get; } = Selection ?? throw new ArgumentNullException(nameof(Selection));
public decimal Stake { get; } = Stake > 0m
? Stake
: throw new ArgumentOutOfRangeException(nameof(Stake), Stake,
"Stake must be positive.");
public DateTimeOffset PlacedAt { get; } = PlacedAt.Offset == MoscowTime.Offset
? PlacedAt
: throw new ArgumentException(
$"PlacedAt must be in Europe/Moscow time (UTC+03:00). " +
$"Received offset: {PlacedAt.Offset:hh\\:mm}.",
nameof(PlacedAt));
public BetOutcome Outcome { get; } = Outcome;
public string? Notes { get; } = string.IsNullOrWhiteSpace(Notes) ? null : Notes;
/// <summary>
/// Gross return on this bet for the current outcome — the amount the
/// bookmaker pays back to the user (stake + winnings).
/// <list type="bullet">
/// <item><see cref="BetOutcome.Won"/>: <c>Stake × Rate</c></item>
/// <item><see cref="BetOutcome.Void"/>: <c>Stake</c> (push — stake returned)</item>
/// <item><see cref="BetOutcome.Lost"/>: <c>0</c></item>
/// <item><see cref="BetOutcome.Pending"/>: <c>null</c> (unknown)</item>
/// </list>
/// </summary>
public decimal? GrossReturn => Outcome switch
{
BetOutcome.Won => Stake * Selection.Rate.Value,
BetOutcome.Void => Stake,
BetOutcome.Lost => 0m,
_ => null,
};
/// <summary>
/// Net profit for the current outcome — <see cref="GrossReturn"/> minus
/// <see cref="Stake"/>. Negative for losses. Null while pending.
/// </summary>
public decimal? NetProfit => GrossReturn is null ? null : GrossReturn.Value - Stake;
/// <summary>
/// Returns a copy with a new <see cref="Outcome"/> — used by the resolver
/// use case after grading the event. Constructs explicitly because the
/// manual validating <c>get</c>-only properties prevent <c>with</c>.
/// </summary>
public PlacedBet WithOutcome(BetOutcome outcome) =>
new(Id, EventId, Selection, Stake, PlacedAt, outcome, Notes);
}
+24
View File
@@ -0,0 +1,24 @@
namespace Marathon.Domain.Enums;
/// <summary>
/// Settlement status of a user-tracked <see cref="Marathon.Domain.Entities.PlacedBet"/>.
/// </summary>
public enum BetOutcome
{
/// <summary>
/// The event has not been graded yet, or the bet has not been auto-resolved
/// yet. Default state for a freshly recorded bet.
/// </summary>
Pending,
/// <summary>The selection won — stake returned plus winnings.</summary>
Won,
/// <summary>The selection lost — stake is forfeit.</summary>
Lost,
/// <summary>
/// Handicap/total push or event abandoned — stake returned, no profit/loss.
/// </summary>
Void,
}
@@ -0,0 +1,57 @@
using Marathon.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Marathon.Infrastructure.Migrations;
/// <inheritdoc />
[DbContext(typeof(MarathonDbContext))]
[Migration("20260516000000_AddPlacedBets")]
public partial class AddPlacedBets : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "PlacedBets",
columns: table => new
{
Id = table.Column<string>(type: "TEXT", nullable: false),
EventCode = table.Column<string>(type: "TEXT", nullable: false),
Scope = table.Column<int>(type: "INTEGER", nullable: false),
PeriodNumber = table.Column<int>(type: "INTEGER", nullable: true),
Type = table.Column<int>(type: "INTEGER", nullable: false),
Side = table.Column<int>(type: "INTEGER", nullable: false),
Value = table.Column<decimal>(type: "TEXT", nullable: true),
Rate = table.Column<decimal>(type: "TEXT", nullable: false),
Stake = table.Column<decimal>(type: "TEXT", nullable: false),
PlacedAt = table.Column<string>(type: "TEXT", nullable: false),
Outcome = table.Column<int>(type: "INTEGER", nullable: false),
Notes = table.Column<string>(type: "TEXT", nullable: true),
},
constraints: table =>
{
table.PrimaryKey("PK_PlacedBets", x => x.Id);
// No foreign key to Events — the journal is user data and must
// survive snapshot retention pruning the source event row.
});
migrationBuilder.CreateIndex(
name: "IX_PlacedBets_EventCode",
table: "PlacedBets",
column: "EventCode");
migrationBuilder.CreateIndex(
name: "IX_PlacedBets_Outcome",
table: "PlacedBets",
column: "Outcome");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(name: "PlacedBets");
}
}
@@ -104,6 +104,26 @@ partial class MarathonDbContextModelSnapshot : ModelSnapshot
b.ToTable("Sports");
});
modelBuilder.Entity("Marathon.Infrastructure.Persistence.Entities.PlacedBetEntity", b =>
{
b.Property<string>("Id").HasColumnType("TEXT");
b.Property<string>("EventCode").IsRequired().HasColumnType("TEXT");
b.Property<int>("Scope").HasColumnType("INTEGER");
b.Property<int?>("PeriodNumber").HasColumnType("INTEGER");
b.Property<int>("Type").HasColumnType("INTEGER");
b.Property<int>("Side").HasColumnType("INTEGER");
b.Property<decimal?>("Value").HasColumnType("TEXT");
b.Property<decimal>("Rate").HasColumnType("TEXT");
b.Property<decimal>("Stake").HasColumnType("TEXT");
b.Property<string>("PlacedAt").IsRequired().HasColumnType("TEXT");
b.Property<int>("Outcome").HasColumnType("INTEGER");
b.Property<string>("Notes").HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("EventCode").HasDatabaseName("IX_PlacedBets_EventCode");
b.HasIndex("Outcome").HasDatabaseName("IX_PlacedBets_Outcome");
b.ToTable("PlacedBets");
});
modelBuilder.Entity("Marathon.Infrastructure.Persistence.Entities.AnomalyEntity", b =>
{
b.HasOne("Marathon.Infrastructure.Persistence.Entities.EventEntity", "Event")
@@ -0,0 +1,35 @@
using Marathon.Infrastructure.Persistence.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Marathon.Infrastructure.Persistence.Configurations;
internal sealed class PlacedBetConfiguration : IEntityTypeConfiguration<PlacedBetEntity>
{
public void Configure(EntityTypeBuilder<PlacedBetEntity> builder)
{
builder.ToTable("PlacedBets");
builder.HasKey(b => b.Id);
builder.Property(b => b.Id).HasColumnType("TEXT").IsRequired();
builder.Property(b => b.EventCode).HasColumnType("TEXT").IsRequired();
builder.Property(b => b.Scope).HasColumnType("INTEGER").IsRequired();
builder.Property(b => b.PeriodNumber).HasColumnType("INTEGER");
builder.Property(b => b.Type).HasColumnType("INTEGER").IsRequired();
builder.Property(b => b.Side).HasColumnType("INTEGER").IsRequired();
builder.Property(b => b.Value).HasColumnType("TEXT");
builder.Property(b => b.Rate).HasColumnType("TEXT").IsRequired();
builder.Property(b => b.Stake).HasColumnType("TEXT").IsRequired();
builder.Property(b => b.PlacedAt).HasColumnType("TEXT").IsRequired();
builder.Property(b => b.Outcome).HasColumnType("INTEGER").IsRequired();
builder.Property(b => b.Notes).HasColumnType("TEXT");
// EventCode is intentionally NOT a foreign key — the journal is the
// user's data and must survive snapshot retention pruning the source
// event row. Existence is checked once at insert time by the use case.
builder.HasIndex(b => b.EventCode).HasDatabaseName("IX_PlacedBets_EventCode");
builder.HasIndex(b => b.Outcome).HasDatabaseName("IX_PlacedBets_Outcome");
}
}
@@ -0,0 +1,47 @@
namespace Marathon.Infrastructure.Persistence.Entities;
/// <summary>
/// EF Core persistence entity for a user-tracked <see cref="Marathon.Domain.Entities.PlacedBet"/>.
/// Flattens the embedded <c>Bet</c> selection (Scope / Type / Side / Value / Rate)
/// into columns so SQLite can index by event and outcome cheaply.
/// </summary>
public sealed class PlacedBetEntity
{
/// <summary>GUID primary key stored as TEXT.</summary>
public string Id { get; set; } = default!;
/// <summary>Foreign key to <see cref="EventEntity.EventCode"/>.</summary>
public string EventCode { get; set; } = default!;
// ─── Embedded Bet selection ──────────────────────────────────────────────
/// <summary>Scope discriminator: 0 = Match, 1 = Period.</summary>
public int Scope { get; set; }
/// <summary>Period number when <see cref="Scope"/> = 1; null otherwise.</summary>
public int? PeriodNumber { get; set; }
/// <summary>BetType as int (Win / Draw / WinFora / Total).</summary>
public int Type { get; set; }
/// <summary>Side as int (Side1 / Side2 / Draw / Less / More).</summary>
public int Side { get; set; }
/// <summary>Handicap or total threshold; null for Win / Draw markets.</summary>
public decimal? Value { get; set; }
/// <summary>Decimal odds the user took.</summary>
public decimal Rate { get; set; }
// ─── Wager fields ────────────────────────────────────────────────────────
/// <summary>Stake in the user's currency.</summary>
public decimal Stake { get; set; }
/// <summary>ISO 8601 timestamp when the bet was recorded (Moscow time).</summary>
public string PlacedAt { get; set; } = default!;
/// <summary>BetOutcome as int (Pending / Won / Lost / Void).</summary>
public int Outcome { get; set; }
/// <summary>Optional free-text note from the user.</summary>
public string? Notes { get; set; }
}
@@ -158,6 +158,51 @@ internal static class Mapping
NameRu: entity.NameRu,
NameEn: entity.NameEn);
// ─── PlacedBet ────────────────────────────────────────────────────────────
public static PlacedBetEntity ToEntity(PlacedBet domain) =>
new()
{
Id = domain.Id.ToString(),
EventCode = domain.EventId.Value,
Scope = domain.Selection.Scope is MatchScope ? ScopeMatch : ScopePeriod,
PeriodNumber = domain.Selection.Scope is PeriodScope ps ? ps.Number : null,
Type = (int)domain.Selection.Type,
Side = (int)domain.Selection.Side,
Value = domain.Selection.Value?.Value,
Rate = domain.Selection.Rate.Value,
Stake = domain.Stake,
PlacedAt = domain.PlacedAt.ToString("O"),
Outcome = (int)domain.Outcome,
Notes = domain.Notes,
};
public static PlacedBet ToDomain(PlacedBetEntity entity)
{
var scope = entity.Scope switch
{
ScopeMatch => (BetScope)MatchScope.Instance,
ScopePeriod => new PeriodScope(entity.PeriodNumber!.Value),
_ => throw new InvalidOperationException(
$"Unknown BetScope discriminator: {entity.Scope}"),
};
var value = entity.Value.HasValue ? new OddsValue(entity.Value.Value) : null;
var rate = new OddsRate(entity.Rate);
var type = (BetType)entity.Type;
var side = (Side)entity.Side;
var selection = new Bet(scope, type, side, value, rate);
return new PlacedBet(
Id: Guid.Parse(entity.Id),
EventId: new EventId(entity.EventCode),
Selection: selection,
Stake: entity.Stake,
PlacedAt: DateTimeOffset.Parse(entity.PlacedAt, CultureInfo.InvariantCulture, RoundtripStyles),
Outcome: (BetOutcome)entity.Outcome,
Notes: entity.Notes);
}
// ─── League ───────────────────────────────────────────────────────────────
public static LeagueEntity ToEntity(League domain) =>
@@ -18,6 +18,7 @@ public sealed class MarathonDbContext : DbContext
public DbSet<AnomalyEntity> Anomalies => Set<AnomalyEntity>();
public DbSet<SportEntity> Sports => Set<SportEntity>();
public DbSet<LeagueEntity> Leagues => Set<LeagueEntity>();
public DbSet<PlacedBetEntity> PlacedBets => Set<PlacedBetEntity>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
@@ -53,6 +53,7 @@ public static class PersistenceModule
services.AddScoped<ISnapshotRepository, SnapshotRepository>();
services.AddScoped<IResultRepository, ResultRepository>();
services.AddScoped<IAnomalyRepository, AnomalyRepository>();
services.AddScoped<IPlacedBetRepository, PlacedBetRepository>();
services.AddScoped<IExcelExporter, ExcelExporter>();
return services;
@@ -0,0 +1,87 @@
using Marathon.Application.Abstractions;
using Marathon.Application.Storage;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
using Microsoft.EntityFrameworkCore;
namespace Marathon.Infrastructure.Persistence.Repositories;
internal sealed class PlacedBetRepository : IPlacedBetRepository
{
private readonly MarathonDbContext _db;
public PlacedBetRepository(MarathonDbContext db) => _db = db;
public async Task<PlacedBet?> GetAsync(Guid key, CancellationToken ct = default)
{
var idStr = key.ToString();
// AsNoTracking so callers can re-map and UpdateAsync without tripping
// EF's "another instance with the same key is already tracked" guard.
var entity = await _db.PlacedBets.AsNoTracking()
.FirstOrDefaultAsync(b => b.Id == idStr, ct);
return entity is null ? null : Mapping.ToDomain(entity);
}
public async Task<IReadOnlyList<PlacedBet>> ListAsync(CancellationToken ct = default)
{
var entities = await _db.PlacedBets.AsNoTracking().ToListAsync(ct);
return entities.Select(Mapping.ToDomain).ToList().AsReadOnly();
}
public async Task<IReadOnlyList<PlacedBet>> ListByOutcomeAsync(BetOutcome outcome, CancellationToken ct = default)
{
var outcomeInt = (int)outcome;
var entities = await _db.PlacedBets.AsNoTracking()
.Where(b => b.Outcome == outcomeInt)
.ToListAsync(ct);
return entities.Select(Mapping.ToDomain).ToList().AsReadOnly();
}
public async Task<IReadOnlyList<PlacedBet>> ListByDateRangeAsync(DateRange range, CancellationToken ct = default)
{
// PlacedAt is stored as ISO 8601 TEXT — same lexical-equals-chronological ordering
// trick used in EventRepository.ListByDateRangeAsync.
var fromStr = range.From.ToString("O");
var toStr = range.To.ToString("O");
var entities = await _db.PlacedBets.AsNoTracking()
.Where(b => b.PlacedAt.CompareTo(fromStr) >= 0
&& b.PlacedAt.CompareTo(toStr) <= 0)
.ToListAsync(ct);
return entities.Select(Mapping.ToDomain).ToList().AsReadOnly();
}
public async Task<IReadOnlyList<PlacedBet>> ListByEventAsync(EventId eventId, CancellationToken ct = default)
{
var entities = await _db.PlacedBets.AsNoTracking()
.Where(b => b.EventCode == eventId.Value)
.ToListAsync(ct);
return entities.Select(Mapping.ToDomain).ToList().AsReadOnly();
}
public async Task AddAsync(PlacedBet entity, CancellationToken ct = default)
{
var efEntity = Mapping.ToEntity(entity);
await _db.PlacedBets.AddAsync(efEntity, ct);
}
public Task UpdateAsync(PlacedBet entity, CancellationToken ct = default)
{
var efEntity = Mapping.ToEntity(entity);
_db.PlacedBets.Update(efEntity);
return Task.CompletedTask;
}
public async Task DeleteAsync(Guid key, CancellationToken ct = default)
{
var idStr = key.ToString();
var entity = await _db.PlacedBets.FirstOrDefaultAsync(b => b.Id == idStr, ct);
if (entity is not null)
_db.PlacedBets.Remove(entity);
}
public async Task SaveChangesAsync(CancellationToken ct = default) =>
await _db.SaveChangesAsync(ct);
}
@@ -83,4 +83,26 @@ internal sealed class SnapshotRepository : ISnapshotRepository
public async Task SaveChangesAsync(CancellationToken ct = default) =>
await _db.SaveChangesAsync(ct);
public async Task<OddsSnapshot?> GetLatestPreMatchAsync(
EventId eventId,
DateTimeOffset atOrBefore,
CancellationToken ct = default)
{
// OddsSource enum: PreMatch == 0. Inlined as an int constant to keep the
// expression EF-translatable (the IL would otherwise carry a cast).
const int preMatchSource = (int)Marathon.Domain.Enums.OddsSource.PreMatch;
var toStr = atOrBefore.ToString("O");
var entity = await _db.Snapshots.AsNoTracking()
.Include(s => s.Bets)
.Where(s => s.EventCode == eventId.Value
&& s.Source == preMatchSource
&& s.CapturedAt.CompareTo(toStr) <= 0)
.OrderByDescending(s => s.CapturedAt)
.FirstOrDefaultAsync(ct);
return entity is null ? null : Mapping.ToDomain(entity);
}
}
+4
View File
@@ -43,6 +43,10 @@
<MudIcon Icon="@Icons.Material.Outlined.Insights" Size="Size.Small" />
<span>@L["Nav.Insights"]</span>
</NavLink>
<NavLink class="m-nav__link" href="my-bets">
<MudIcon Icon="@Icons.Material.Outlined.Receipt" Size="Size.Small" />
<span>@L["Nav.MyBets"]</span>
</NavLink>
<div class="m-nav__group" style="margin-top: var(--m-space-5);">@L["Nav.Section.System"]</div>
<NavLink class="m-nav__link" href="settings">
+931
View File
@@ -0,0 +1,931 @@
@*
Journal — the user's personal bet tracker.
Loads a precomputed BetJournalVm and exposes it as the editorial-quant
ledger that mirrors Insights / AnomalyFeed: a hero header in the accent
tone (positive product surface, not anomaly-red), a KPI strip, a compact
record-a-bet form, and a list of every wager with P&L, CLV, and outcome.
*@
@page "/my-bets"
@using Marathon.Application.Betting
@implements IDisposable
@inject IStringLocalizer<SharedResource> L
@inject IBetJournalService Service
@inject ISnackbar Snackbar
@inject ILogger<Journal> Logger
<PageTitle>@L["App.Title"] · @L["Nav.MyBets"]</PageTitle>
<section class="m-shell">
<header class="m-rise m-rise-1 m-journal__header" data-test="journal-header">
<div class="m-journal__header-text">
<span class="m-kicker">@L["Journal.Kicker"]</span>
<h1 class="m-display" style="font-size: clamp(2rem, 4vw, 3rem);">@L["Journal.Title"]</h1>
<p style="color: var(--m-c-ink-soft); max-width: 64ch;">@L["Journal.Lede"]</p>
</div>
<div class="m-journal__header-actions">
<button type="button"
class="m-chip m-journal__chip"
@onclick="ResolvePendingAsync"
disabled="@(_loading || _resolving)"
data-test="journal-resolve">
<span class="m-journal__chip-glyph @(_resolving ? "is-spinning" : null)" aria-hidden="true">✓</span>
<span>@L["Journal.Action.Resolve"]</span>
</button>
<button type="button"
class="m-chip m-journal__chip"
@onclick="LoadAsync"
disabled="@_loading"
data-test="journal-refresh">
<span class="m-journal__chip-glyph @(_loading ? "is-spinning" : null)" aria-hidden="true">↻</span>
<span>@L["Journal.Action.Refresh"]</span>
</button>
</div>
</header>
@if (_loading && _vm is null)
{
<div class="m-list-empty m-rise m-rise-2" data-test="journal-loading">
<MudProgressCircular Indeterminate="true" Size="Size.Small" />
<span class="m-mono">@L["Common.Loading"]</span>
</div>
}
else if (_errored && _vm is null)
{
<div class="m-list-empty m-rise m-rise-2" data-test="journal-error">
<span class="m-kicker" style="border-color: var(--m-c-anomaly); color: var(--m-c-anomaly);">
@L["Common.Empty"]
</span>
<p style="color: var(--m-c-ink-soft); margin-top: var(--m-space-3); max-width: 50ch;">
@L["Journal.Empty.None"]
</p>
</div>
}
else if (_vm is { } vm)
{
@* ---------- KPI strip ---------- *@
<div class="m-journal__kpis m-rise m-rise-2" data-test="journal-kpis">
<article class="m-journal__kpi m-journal__kpi--@SignedTone(vm.Stats.RoiPercent)" data-test="journal-kpi-roi">
<span class="m-journal__kpi-label">@L["Journal.Stat.Roi"]</span>
<span class="m-journal__kpi-value">@FormatSignedPercent(vm.Stats.RoiPercent)</span>
<span class="m-journal__kpi-hint">@L["Journal.Stat.Roi.Hint"]</span>
</article>
<article class="m-journal__kpi" data-test="journal-kpi-strike">
<span class="m-journal__kpi-label">@L["Journal.Stat.StrikeRate"]</span>
<span class="m-journal__kpi-value">@FormatPercent(vm.Stats.StrikeRatePercent)</span>
<span class="m-journal__kpi-hint">@L["Journal.Stat.StrikeRate.Hint"]</span>
</article>
<article class="m-journal__kpi m-journal__kpi--@ClvTone(vm.Stats.AverageClvProbabilityDelta)" data-test="journal-kpi-clv">
<span class="m-journal__kpi-label">@L["Journal.Stat.AvgClv"]</span>
<span class="m-journal__kpi-value">@FormatClvPoints(vm.Stats.AverageClvProbabilityDelta)</span>
<span class="m-journal__kpi-hint">@L["Journal.Stat.AvgClv.Hint"]</span>
</article>
<article class="m-journal__kpi m-journal__kpi--@SignedTone(vm.Stats.NetProfit)" data-test="journal-kpi-profit">
<span class="m-journal__kpi-label">@L["Journal.Stat.NetProfit"]</span>
<span class="m-journal__kpi-value">@FormatSignedDecimal(vm.Stats.NetProfit, vm.Stats.ResolvedCount)</span>
<span class="m-journal__kpi-hint">@L["Journal.Stat.NetProfit.Hint"]</span>
</article>
</div>
<div class="m-journal__counts m-rise m-rise-2 m-mono" data-test="journal-counts">
<span><span class="m-journal__counts-label">@L["Journal.Stat.TotalBets"]</span> <strong>@vm.Stats.TotalBets</strong></span>
<span aria-hidden="true">·</span>
<span><span class="m-journal__counts-label">@L["Journal.Stat.Pending"]</span> <strong>@vm.Stats.PendingCount</strong></span>
<span aria-hidden="true">·</span>
<span><span class="m-journal__counts-label">@L["Journal.Stat.Won"]</span> <strong style="color: var(--m-c-positive);">@vm.Stats.WonCount</strong></span>
<span aria-hidden="true">·</span>
<span><span class="m-journal__counts-label">@L["Journal.Stat.Lost"]</span> <strong style="color: var(--m-c-anomaly);">@vm.Stats.LostCount</strong></span>
<span aria-hidden="true">·</span>
<span><span class="m-journal__counts-label">@L["Journal.Stat.Void"]</span> <strong>@vm.Stats.VoidCount</strong></span>
</div>
<hr class="m-rule--double" />
@* ---------- Record-a-bet form ---------- *@
<section class="m-journal__section m-rise m-rise-3" data-test="journal-add">
<header class="m-journal__section-head">
<span class="m-kicker">@L["Journal.Section.Add"]</span>
</header>
<article class="m-card m-card--accented m-journal__form-card">
<div class="m-journal__form-grid">
<div class="m-journal__form-field m-journal__form-field--wide">
<label class="m-journal__form-label" for="journal-event-id">@L["Journal.Field.EventId"]</label>
<MudTextField id="journal-event-id"
T="string"
@bind-Value="_form.EventId"
Variant="Variant.Outlined"
Placeholder="26000000"
data-test="journal-add-event-id" />
<span class="m-journal__form-hint">@L["Journal.Field.EventId.Hint"]</span>
</div>
<div class="m-journal__form-field">
<label class="m-journal__form-label">@L["Journal.Field.Type"]</label>
<MudSelect T="BetType"
Value="_form.Type"
ValueChanged="OnTypeChanged"
Variant="Variant.Outlined"
data-test="journal-add-type">
@foreach (var betType in _betTypes)
{
<MudSelectItem T="BetType" Value="@betType">@BetTypeLabel(betType)</MudSelectItem>
}
</MudSelect>
</div>
<div class="m-journal__form-field">
<label class="m-journal__form-label">@L["Journal.Field.Side"]</label>
<MudSelect T="Side"
@bind-Value="_form.Side"
Variant="Variant.Outlined"
data-test="journal-add-side">
@foreach (var side in SidesFor(_form.Type))
{
<MudSelectItem T="Side" Value="@side">@SideLabel(side)</MudSelectItem>
}
</MudSelect>
</div>
@if (_form.Type is BetType.WinFora or BetType.Total)
{
<div class="m-journal__form-field">
<label class="m-journal__form-label">@L["Journal.Field.Value"]</label>
<MudNumericField T="decimal?"
@bind-Value="_form.Value"
Variant="Variant.Outlined"
Step="0.5m"
data-test="journal-add-value" />
<span class="m-journal__form-hint">@L["Journal.Field.Value.Hint"]</span>
</div>
}
<div class="m-journal__form-field">
<label class="m-journal__form-label">@L["Journal.Field.Rate"]</label>
<MudNumericField T="decimal"
@bind-Value="_form.Rate"
Min="1.01m"
Step="0.01m"
Variant="Variant.Outlined"
data-test="journal-add-rate" />
</div>
<div class="m-journal__form-field">
<label class="m-journal__form-label">@L["Journal.Field.Stake"]</label>
<MudNumericField T="decimal"
@bind-Value="_form.Stake"
Min="0.01m"
Step="1m"
Variant="Variant.Outlined"
data-test="journal-add-stake" />
</div>
<div class="m-journal__form-field m-journal__form-field--wide">
<label class="m-journal__form-label">@L["Journal.Field.Notes"]</label>
<MudTextField T="string"
@bind-Value="_form.Notes"
Variant="Variant.Outlined"
Lines="2"
Placeholder="@L["Journal.Field.Notes.Placeholder"]"
data-test="journal-add-notes" />
</div>
</div>
@if (!string.IsNullOrEmpty(_formError))
{
<p class="m-journal__form-error" data-test="journal-add-error">@_formError</p>
}
<div class="m-journal__form-actions">
<button type="button"
class="m-chip m-journal__submit"
@onclick="SubmitAsync"
disabled="@_submitting"
data-test="journal-add-submit">
<span class="m-journal__chip-glyph @(_submitting ? "is-spinning" : null)" aria-hidden="true">+</span>
<span>@L["Journal.Action.Submit"]</span>
</button>
</div>
</article>
</section>
<hr class="m-rule--double" />
@* ---------- Bets list ---------- *@
<section class="m-journal__section m-rise m-rise-4" data-test="journal-list">
<header class="m-journal__section-head">
<span class="m-kicker">@L["Journal.Section.List"]</span>
<span class="m-journal__section-count m-mono">@vm.Bets.Count</span>
</header>
@if (vm.Bets.Count == 0)
{
<div class="m-list-empty" data-test="journal-empty">
<span class="m-kicker" style="border-color: var(--m-c-ink-soft); color: var(--m-c-ink-soft);">
@L["Common.Empty"]
</span>
<p style="color: var(--m-c-ink-soft); margin-top: var(--m-space-3); max-width: 56ch;">
@L["Journal.Empty.None"]
</p>
</div>
}
else
{
<div class="m-journal__table-wrap">
<table class="m-journal__table" data-test="journal-table">
<thead>
<tr>
<th scope="col">@L["Journal.Column.PlacedAt"]</th>
<th scope="col">@L["Journal.Column.Match"]</th>
<th scope="col">@L["Journal.Column.Selection"]</th>
<th scope="col" style="text-align: right;">@L["Journal.Column.Stake"]</th>
<th scope="col" style="text-align: right;">@L["Journal.Column.Rate"]</th>
<th scope="col" style="text-align: right;">@L["Journal.Column.Profit"]</th>
<th scope="col" style="text-align: right;">@L["Journal.Column.Clv"]</th>
<th scope="col">@L["Journal.Column.Outcome"]</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
@foreach (var bet in vm.Bets)
{
var row = bet;
<tr class="m-journal__row m-journal__row--@OutcomeCss(row.Bet.Outcome)"
data-test="journal-row"
data-bet-id="@row.Id">
<td class="m-mono">@row.Bet.PlacedAt.ToLocalTime().ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture)</td>
<td style="font-weight: 500;">@row.EventTitle</td>
<td>
<div class="m-journal__selection">@SelectionLabel(row.Bet.Selection)</div>
@if (!string.IsNullOrWhiteSpace(row.Bet.Notes))
{
<div class="m-journal__notes" data-test="journal-row-notes">@row.Bet.Notes</div>
}
</td>
<td class="m-mono" style="text-align: right;">@row.Bet.Stake.ToString("0.00", CultureInfo.InvariantCulture)</td>
<td class="m-mono" style="text-align: right;">@row.Bet.Selection.Rate.Value.ToString("0.00", CultureInfo.InvariantCulture)</td>
<td class="m-mono m-journal__pl m-journal__pl--@ProfitTone(row.Bet.NetProfit)" style="text-align: right;">
@FormatProfit(row.Bet.NetProfit)
</td>
<td class="m-mono m-journal__clv m-journal__clv--@ClvTone(row.ClvProbabilityDelta)" style="text-align: right;">
@FormatClvPoints(row.ClvProbabilityDelta)
</td>
<td>
<span class="m-journal__verdict m-journal__verdict--@OutcomeCss(row.Bet.Outcome)">
@OutcomeLabel(row.Bet.Outcome)
</span>
</td>
<td class="m-journal__row-actions">
@if (_pendingDeleteId == row.Id)
{
<span class="m-journal__confirm" data-test="@($"journal-delete-confirm-{row.Id}")">
<span class="m-journal__confirm-msg">@L["Journal.Confirm.Delete"]</span>
<button type="button"
class="m-chip m-journal__chip m-journal__chip--danger"
@onclick="@(() => ConfirmDeleteAsync(row.Id))"
data-test="@($"journal-delete-confirm-yes-{row.Id}")">
@L["Journal.Action.Confirm"]
</button>
<button type="button"
class="m-chip m-journal__chip"
@onclick="CancelDelete"
data-test="@($"journal-delete-confirm-no-{row.Id}")">
@L["Journal.Action.Cancel"]
</button>
</span>
}
else
{
<button type="button"
class="m-chip m-journal__chip m-journal__chip--ghost"
@onclick="@(() => RequestDelete(row.Id))"
data-test="@($"journal-delete-{row.Id}")"
aria-label="@L["Journal.Action.Delete"]">
<span aria-hidden="true">×</span>
<span>@L["Journal.Action.Delete"]</span>
</button>
}
</td>
</tr>
}
</tbody>
</table>
</div>
}
</section>
}
</section>
<style>
/* ---- Header ---- */
.m-journal__header {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
gap: var(--m-space-5);
align-items: end;
}
@@media (max-width: 720px) {
.m-journal__header { grid-template-columns: 1fr; }
.m-journal__header-actions { justify-self: start; }
}
.m-journal__header-text { display: grid; gap: var(--m-space-3); max-width: 880px; }
.m-journal__header-actions { display: flex; gap: var(--m-space-3); flex-wrap: wrap; }
.m-journal__chip {
gap: var(--m-space-2);
padding: 6px 12px;
font-family: var(--m-font-mono);
font-size: 0.75rem;
text-transform: uppercase;
letter-spacing: 0.14em;
}
.m-journal__chip:disabled { opacity: 0.6; cursor: progress; }
.m-journal__chip--ghost {
color: var(--m-c-ink-soft);
background: transparent;
}
.m-journal__chip--ghost:hover {
color: var(--m-c-anomaly);
border-color: var(--m-c-anomaly);
}
.m-journal__chip--danger {
color: var(--m-c-anomaly);
border-color: var(--m-c-anomaly);
}
.m-journal__chip-glyph {
display: inline-block;
font-size: 0.875rem;
line-height: 1;
transition: transform 200ms ease;
}
.m-journal__chip:hover .m-journal__chip-glyph { transform: rotate(45deg); }
.m-journal__chip-glyph.is-spinning { animation: m-journal-spin 1.1s linear infinite; }
@@keyframes m-journal-spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
@@media (prefers-reduced-motion: reduce) {
.m-journal__chip-glyph.is-spinning { animation: none; }
.m-journal__chip:hover .m-journal__chip-glyph { transform: none; }
}
/* ---- KPI strip ---- */
.m-journal__kpis {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: var(--m-space-4);
}
.m-journal__kpi {
background: var(--m-c-paper);
border: 1px solid var(--m-c-rule);
border-left: 3px solid var(--m-c-rule);
padding: var(--m-space-4) var(--m-space-5);
display: flex;
flex-direction: column;
gap: var(--m-space-2);
position: relative;
}
.m-journal__kpi--positive { border-left-color: var(--m-c-positive); }
.m-journal__kpi--neutral { border-left-color: var(--m-c-accent); }
.m-journal__kpi--negative { border-left-color: var(--m-c-anomaly); }
.m-journal__kpi-label {
font-family: var(--m-font-mono);
font-size: 0.6875rem;
letter-spacing: 0.16em;
text-transform: uppercase;
color: var(--m-c-ink-soft);
}
.m-journal__kpi-value {
font-family: var(--m-font-mono);
font-feature-settings: var(--m-num-feature);
font-size: clamp(2rem, 3.5vw, 2.625rem);
font-weight: 500;
line-height: 1;
letter-spacing: -0.02em;
color: var(--m-c-ink);
}
.m-journal__kpi--positive .m-journal__kpi-value { color: var(--m-c-positive); }
.m-journal__kpi--negative .m-journal__kpi-value { color: var(--m-c-anomaly); }
.m-journal__kpi-hint {
font-size: 0.8125rem;
color: var(--m-c-ink-soft);
}
/* ---- Mono count strip ---- */
.m-journal__counts {
display: flex;
gap: var(--m-space-3);
flex-wrap: wrap;
align-items: baseline;
padding: var(--m-space-2) 0;
font-size: 0.8125rem;
color: var(--m-c-ink-soft);
font-feature-settings: var(--m-num-feature);
}
.m-journal__counts strong {
color: var(--m-c-ink);
font-weight: 600;
}
.m-journal__counts-label {
text-transform: uppercase;
letter-spacing: 0.12em;
font-size: 0.6875rem;
}
/* ---- Section headers ---- */
.m-journal__section { display: grid; gap: var(--m-space-4); }
.m-journal__section-head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: var(--m-space-3);
}
.m-journal__section-count {
font-size: 0.6875rem;
letter-spacing: 0.16em;
text-transform: uppercase;
color: var(--m-c-ink-soft);
}
/* ---- Form ---- */
.m-journal__form-card {
display: grid;
gap: var(--m-space-4);
padding: var(--m-space-5);
}
.m-journal__form-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: var(--m-space-4);
}
.m-journal__form-field { display: grid; gap: var(--m-space-2); }
.m-journal__form-field--wide { grid-column: span 2; }
@@media (max-width: 720px) {
.m-journal__form-field--wide { grid-column: span 1; }
}
.m-journal__form-label {
font-family: var(--m-font-mono);
font-size: 0.6875rem;
letter-spacing: 0.14em;
text-transform: uppercase;
color: var(--m-c-ink-soft);
}
.m-journal__form-hint {
font-size: 0.75rem;
color: var(--m-c-ink-soft);
}
.m-journal__form-error {
margin: 0;
padding: var(--m-space-3) var(--m-space-4);
border: 1px solid var(--m-c-anomaly);
border-left-width: 3px;
background: rgba(220, 38, 38, 0.06);
color: var(--m-c-anomaly);
font-family: var(--m-font-mono);
font-size: 0.8125rem;
line-height: 1.5;
}
[data-theme="dark"] .m-journal__form-error {
background: rgba(248, 113, 113, 0.10);
}
.m-journal__form-actions {
display: flex;
justify-content: flex-end;
gap: var(--m-space-3);
}
.m-journal__submit {
border-color: var(--m-c-accent);
color: var(--m-c-accent);
}
.m-journal__submit:not(:disabled):hover {
background: var(--m-c-accent);
color: var(--m-c-paper);
}
/* ---- Table ---- */
.m-journal__table-wrap {
background: var(--m-c-paper);
border: 1px solid var(--m-c-rule);
overflow-x: auto;
}
.m-journal__table {
width: 100%;
border-collapse: collapse;
font-family: var(--m-font-body);
}
.m-journal__table thead th {
font-family: var(--m-font-mono);
font-size: 0.6875rem;
letter-spacing: 0.14em;
text-transform: uppercase;
text-align: left;
padding: var(--m-space-3) var(--m-space-3);
border-bottom: 1px solid var(--m-c-rule);
color: var(--m-c-ink-soft);
background: var(--m-c-paper-2);
white-space: nowrap;
}
.m-journal__table tbody td {
padding: var(--m-space-3) var(--m-space-3);
border-bottom: 1px solid var(--m-c-rule);
vertical-align: middle;
font-size: 0.9375rem;
}
.m-journal__table tbody tr:last-child td { border-bottom: 0; }
.m-journal__row { transition: background 120ms ease; }
.m-journal__row:hover { background: var(--m-c-paper-2); }
.m-journal__row--won { box-shadow: inset 2px 0 0 0 var(--m-c-positive); }
.m-journal__row--lost { box-shadow: inset 2px 0 0 0 var(--m-c-anomaly); }
.m-journal__row--pending { box-shadow: inset 2px 0 0 0 var(--m-c-rule); }
.m-journal__row--void { box-shadow: inset 2px 0 0 0 var(--m-c-ink-soft); }
@@media (prefers-reduced-motion: reduce) {
.m-journal__row { transition: none; }
}
.m-journal__pl, .m-journal__clv { font-feature-settings: var(--m-num-feature); font-weight: 600; }
.m-journal__pl--positive, .m-journal__clv--positive { color: var(--m-c-positive); }
.m-journal__pl--negative, .m-journal__clv--negative { color: var(--m-c-anomaly); }
.m-journal__pl--neutral, .m-journal__clv--neutral { color: var(--m-c-ink-soft); }
.m-journal__selection { font-weight: 500; }
.m-journal__notes {
margin-top: 2px;
font-size: 0.75rem;
color: var(--m-c-ink-soft);
font-style: italic;
max-width: 36ch;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.m-journal__verdict {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 2px 8px;
font-family: var(--m-font-mono);
font-size: 0.6875rem;
letter-spacing: 0.14em;
text-transform: uppercase;
border: 1px solid currentColor;
border-radius: var(--m-radius-xs);
background: rgba(0, 0, 0, 0);
}
.m-journal__verdict--won {
color: var(--m-c-positive);
background: rgba(21, 128, 61, 0.10);
}
.m-journal__verdict--lost {
color: var(--m-c-anomaly);
background: rgba(220, 38, 38, 0.10);
}
.m-journal__verdict--pending {
color: var(--m-c-ink-soft);
background: transparent;
}
.m-journal__verdict--void {
color: var(--m-c-ink-soft);
background: var(--m-c-paper-2);
}
[data-theme="dark"] .m-journal__verdict--won {
color: var(--m-c-positive);
background: rgba(34, 197, 94, 0.15);
}
[data-theme="dark"] .m-journal__verdict--lost {
color: var(--m-c-anomaly);
background: rgba(248, 113, 113, 0.15);
}
.m-journal__row-actions {
text-align: right;
white-space: nowrap;
}
.m-journal__confirm {
display: inline-flex;
align-items: center;
gap: var(--m-space-2);
flex-wrap: wrap;
justify-content: flex-end;
}
.m-journal__confirm-msg {
font-family: var(--m-font-mono);
font-size: 0.6875rem;
letter-spacing: 0.12em;
text-transform: uppercase;
color: var(--m-c-anomaly);
}
/* ---- Empty-state block ---- */
.m-list-empty {
display: grid;
place-content: center;
gap: var(--m-space-3);
padding: var(--m-space-7);
text-align: center;
background: var(--m-c-paper);
border: 1px solid var(--m-c-rule);
}
</style>
@code {
private static readonly BetType[] _betTypes =
{ BetType.Win, BetType.Draw, BetType.WinFora, BetType.Total };
private BetJournalVm? _vm;
private bool _loading = true;
private bool _errored;
private bool _submitting;
private bool _resolving;
private string? _formError;
private Guid? _pendingDeleteId;
private AddBetForm _form = new();
private CancellationTokenSource? _loadCts;
protected override async Task OnInitializedAsync()
{
await LoadAsync();
}
private async Task LoadAsync()
{
_loadCts?.Cancel();
_loadCts = new CancellationTokenSource();
var ct = _loadCts.Token;
_loading = true;
_errored = false;
StateHasChanged();
try
{
var report = await Service.GetReportAsync(ct);
if (ct.IsCancellationRequested) return;
_vm = report;
_pendingDeleteId = null;
}
catch (OperationCanceledException) { /* superseded */ }
catch (Exception ex)
{
Logger.LogError(ex, "Failed to load bet journal report.");
_errored = true;
_vm = null;
}
finally
{
_loading = false;
StateHasChanged();
}
}
private async Task ResolvePendingAsync()
{
if (_resolving) return;
_resolving = true;
StateHasChanged();
var ct = _loadCts?.Token ?? CancellationToken.None;
try
{
var graded = await Service.ResolvePendingAsync(ct);
var msg = graded == 0
? L["Journal.Resolve.None"].Value
: string.Format(CultureInfo.CurrentCulture, L["Journal.Resolve.Done"].Value, graded);
Snackbar.Add(msg, graded == 0 ? Severity.Info : Severity.Success);
}
catch (OperationCanceledException) { /* superseded */ }
catch (Exception ex)
{
Logger.LogError(ex, "Failed to resolve pending bets.");
Snackbar.Add(L["Journal.Error.Generic"].Value, Severity.Error);
}
finally
{
_resolving = false;
await LoadAsync();
}
}
private async Task SubmitAsync()
{
if (_submitting) return;
_formError = null;
if (!_form.IsValid(out var err))
{
_formError = err;
StateHasChanged();
return;
}
_submitting = true;
StateHasChanged();
var ct = _loadCts?.Token ?? CancellationToken.None;
try
{
await Service.AddAsync(_form, ct);
_form = new AddBetForm();
_formError = null;
await LoadAsync();
}
catch (ArgumentException ex)
{
_formError = ex.Message;
}
catch (InvalidOperationException ex)
{
_formError = ex.Message;
}
catch (Exception ex)
{
Logger.LogError(ex, "Failed to record bet.");
_formError = L["Journal.Error.Generic"].Value;
}
finally
{
_submitting = false;
StateHasChanged();
}
}
private void OnTypeChanged(BetType next)
{
_form.Type = next;
var valid = SidesFor(next);
if (!valid.Contains(_form.Side))
{
_form.Side = valid[0];
}
if (next is not (BetType.WinFora or BetType.Total))
{
_form.Value = null;
}
}
private void RequestDelete(Guid id)
{
_pendingDeleteId = id;
}
private void CancelDelete()
{
_pendingDeleteId = null;
}
private async Task ConfirmDeleteAsync(Guid id)
{
var ct = _loadCts?.Token ?? CancellationToken.None;
try
{
await Service.DeleteAsync(id, ct);
}
catch (OperationCanceledException) { /* superseded */ }
catch (Exception ex)
{
Logger.LogError(ex, "Failed to delete bet {BetId}.", id);
Snackbar.Add(L["Journal.Error.Generic"].Value, Severity.Error);
}
finally
{
_pendingDeleteId = null;
await LoadAsync();
}
}
// ---- Formatting / labels ------------------------------------------------
private static IReadOnlyList<Side> SidesFor(BetType type) => type switch
{
BetType.Win => new[] { Side.Side1, Side.Side2 },
BetType.WinFora => new[] { Side.Side1, Side.Side2 },
BetType.Draw => new[] { Side.Draw },
BetType.Total => new[] { Side.Less, Side.More },
_ => new[] { Side.Side1 },
};
private string BetTypeLabel(BetType type) => type switch
{
BetType.Win => L["Journal.BetType.Win"],
BetType.Draw => L["Journal.BetType.Draw"],
BetType.WinFora => L["Journal.BetType.WinFora"],
BetType.Total => L["Journal.BetType.Total"],
_ => type.ToString(),
};
private string SideLabel(Side side) => side switch
{
Side.Side1 => L["Journal.Side.Side1"],
Side.Side2 => L["Journal.Side.Side2"],
Side.Draw => L["Journal.Side.Draw"],
Side.Less => L["Journal.Side.Less"],
Side.More => L["Journal.Side.More"],
_ => side.ToString(),
};
private string OutcomeLabel(BetOutcome o) => o switch
{
BetOutcome.Won => L["Journal.Outcome.Won"],
BetOutcome.Lost => L["Journal.Outcome.Lost"],
BetOutcome.Void => L["Journal.Outcome.Void"],
BetOutcome.Pending => L["Journal.Outcome.Pending"],
_ => L["Journal.Outcome.Pending"],
};
private static string OutcomeCss(BetOutcome o) => o switch
{
BetOutcome.Won => "won",
BetOutcome.Lost => "lost",
BetOutcome.Void => "void",
BetOutcome.Pending => "pending",
_ => "pending",
};
private string SelectionLabel(Bet selection)
{
var typeText = BetTypeLabel(selection.Type);
var sideText = SideLabel(selection.Side);
var rate = selection.Rate.Value.ToString("0.00", CultureInfo.InvariantCulture);
if (selection.Value is { } v)
{
var threshold = v.Value.ToString("0.##", CultureInfo.InvariantCulture);
return typeText + " " + sideText + " " + threshold + " @ " + rate;
}
return typeText + " " + sideText + " @ " + rate;
}
private static string FormatSignedPercent(decimal? value)
{
if (value is null) return "—";
var v = value.Value;
var sign = v > 0m ? "+" : (v < 0m ? "-" : "");
var abs = Math.Abs(v);
return sign + abs.ToString("0.0", CultureInfo.InvariantCulture) + "%";
}
private static string FormatPercent(decimal? value)
{
if (value is null) return "—";
// Show one decimal so strike-rate 66.67% does not collapse to 67% —
// the user wants to see "50.5%" rather than be lied to.
return value.Value.ToString("0.0", CultureInfo.InvariantCulture) + "%";
}
private static string FormatClvPoints(decimal? probabilityDelta)
{
if (probabilityDelta is null) return "—";
var pts = probabilityDelta.Value * 100m;
var sign = pts > 0m ? "+" : (pts < 0m ? "-" : "");
var abs = Math.Abs(pts);
return sign + abs.ToString("0.0", CultureInfo.InvariantCulture) + " pp";
}
private static string FormatSignedDecimal(decimal value, int resolvedCount)
{
if (resolvedCount == 0) return "—";
var sign = value > 0m ? "+" : (value < 0m ? "-" : "");
var abs = Math.Abs(value);
return sign + abs.ToString("0.00", CultureInfo.InvariantCulture);
}
private static string FormatProfit(decimal? value)
{
if (value is null) return "—";
var v = value.Value;
var sign = v > 0m ? "+" : (v < 0m ? "-" : "");
var abs = Math.Abs(v);
return sign + abs.ToString("0.00", CultureInfo.InvariantCulture);
}
private static string SignedTone(decimal? value) => value switch
{
null => "neutral",
> 0m => "positive",
< 0m => "negative",
_ => "neutral",
};
private static string ProfitTone(decimal? value) => value switch
{
null => "neutral",
> 0m => "positive",
< 0m => "negative",
_ => "neutral",
};
private static string ClvTone(decimal? value) => value switch
{
null => "neutral",
> 0m => "positive",
< 0m => "negative",
_ => "neutral",
};
public void Dispose()
{
_loadCts?.Cancel();
_loadCts?.Dispose();
}
}
@@ -347,4 +347,67 @@
<data name="Insights.Action.Refresh"><value>Refresh</value></data>
<data name="Insights.Action.OpenAnomaly"><value>Open</value></data>
<data name="Insights.Bucket.NotApplicable"><value>—</value></data>
<data name="Nav.MyBets"><value>My bets</value></data>
<data name="Journal.Kicker"><value>Journal</value></data>
<data name="Journal.Title"><value>Your bets and CLV</value></data>
<data name="Journal.Lede"><value>Every wager you've recorded, graded against final results and scored against the closing line. Positive CLV is the leading indicator that says you're consistently beating the market.</value></data>
<data name="Journal.Stat.Roi"><value>ROI</value></data>
<data name="Journal.Stat.Roi.Hint"><value>Net profit ÷ total staked.</value></data>
<data name="Journal.Stat.StrikeRate"><value>Strike rate</value></data>
<data name="Journal.Stat.StrikeRate.Hint"><value>Wins ÷ (wins + losses).</value></data>
<data name="Journal.Stat.AvgClv"><value>Avg CLV</value></data>
<data name="Journal.Stat.AvgClv.Hint"><value>Mean closing-line implied-probability gain.</value></data>
<data name="Journal.Stat.NetProfit"><value>Net profit</value></data>
<data name="Journal.Stat.NetProfit.Hint"><value>Returns minus stakes (resolved bets).</value></data>
<data name="Journal.Stat.TotalBets"><value>Total bets</value></data>
<data name="Journal.Stat.Pending"><value>Pending</value></data>
<data name="Journal.Stat.Won"><value>Won</value></data>
<data name="Journal.Stat.Lost"><value>Lost</value></data>
<data name="Journal.Stat.Void"><value>Void</value></data>
<data name="Journal.Section.Add"><value>Record a bet</value></data>
<data name="Journal.Section.List"><value>Bet journal</value></data>
<data name="Journal.Action.Refresh"><value>Refresh</value></data>
<data name="Journal.Action.Resolve"><value>Resolve pending</value></data>
<data name="Journal.Action.Submit"><value>Record bet</value></data>
<data name="Journal.Action.Delete"><value>Delete</value></data>
<data name="Journal.Action.Confirm"><value>Confirm</value></data>
<data name="Journal.Action.Cancel"><value>Cancel</value></data>
<data name="Journal.Field.EventId"><value>Event ID</value></data>
<data name="Journal.Field.EventId.Hint"><value>Numeric ID from the event detail URL.</value></data>
<data name="Journal.Field.Type"><value>Bet type</value></data>
<data name="Journal.Field.Side"><value>Side</value></data>
<data name="Journal.Field.Value"><value>Threshold</value></data>
<data name="Journal.Field.Value.Hint"><value>Handicap or total line (e.g. -1.5, 2.5).</value></data>
<data name="Journal.Field.Rate"><value>Taken rate</value></data>
<data name="Journal.Field.Stake"><value>Stake</value></data>
<data name="Journal.Field.Notes"><value>Notes</value></data>
<data name="Journal.Field.Notes.Placeholder"><value>Strategy tag, bookmaker, or anything you want to remember…</value></data>
<data name="Journal.BetType.Win"><value>Win</value></data>
<data name="Journal.BetType.Draw"><value>Draw</value></data>
<data name="Journal.BetType.WinFora"><value>Handicap</value></data>
<data name="Journal.BetType.Total"><value>Total</value></data>
<data name="Journal.Side.Side1"><value>Side 1</value></data>
<data name="Journal.Side.Side2"><value>Side 2</value></data>
<data name="Journal.Side.Draw"><value>Draw</value></data>
<data name="Journal.Side.Less"><value>Under</value></data>
<data name="Journal.Side.More"><value>Over</value></data>
<data name="Journal.Outcome.Pending"><value>Pending</value></data>
<data name="Journal.Outcome.Won"><value>Won</value></data>
<data name="Journal.Outcome.Lost"><value>Lost</value></data>
<data name="Journal.Outcome.Void"><value>Void</value></data>
<data name="Journal.Column.PlacedAt"><value>Placed</value></data>
<data name="Journal.Column.Match"><value>Match</value></data>
<data name="Journal.Column.Selection"><value>Selection</value></data>
<data name="Journal.Column.Stake"><value>Stake</value></data>
<data name="Journal.Column.Rate"><value>Rate</value></data>
<data name="Journal.Column.Profit"><value>P&amp;L</value></data>
<data name="Journal.Column.Clv"><value>CLV</value></data>
<data name="Journal.Column.Outcome"><value>Outcome</value></data>
<data name="Journal.Empty.None"><value>No bets recorded yet. Use the form above to log a wager — once the event finishes the journal will auto-grade it and compute closing-line value against the latest pre-match snapshot.</value></data>
<data name="Journal.Empty.NotApplicable"><value>—</value></data>
<data name="Journal.Error.Generic"><value>Failed to save bet — check the event ID and try again.</value></data>
<data name="Journal.Resolve.None"><value>No pending bets needed grading.</value></data>
<data name="Journal.Resolve.Done"><value>Graded {0} pending bet(s).</value></data>
<data name="Journal.Confirm.Delete"><value>Delete this bet permanently?</value></data>
</root>
@@ -360,4 +360,67 @@
<data name="Insights.Action.Refresh"><value>Обновить</value></data>
<data name="Insights.Action.OpenAnomaly"><value>Открыть</value></data>
<data name="Insights.Bucket.NotApplicable"><value>—</value></data>
<data name="Nav.MyBets"><value>Мои ставки</value></data>
<data name="Journal.Kicker"><value>Журнал</value></data>
<data name="Journal.Title"><value>Ваши ставки и CLV</value></data>
<data name="Journal.Lede"><value>Каждая зафиксированная ставка с автоматическим расчётом результата и оценкой против линии закрытия. Положительный CLV — главный долгосрочный индикатор того, что вы стабильно обыгрываете рынок.</value></data>
<data name="Journal.Stat.Roi"><value>ROI</value></data>
<data name="Journal.Stat.Roi.Hint"><value>Чистая прибыль ÷ сумма ставок.</value></data>
<data name="Journal.Stat.StrikeRate"><value>Strike rate</value></data>
<data name="Journal.Stat.StrikeRate.Hint"><value>Победы ÷ (победы + поражения).</value></data>
<data name="Journal.Stat.AvgClv"><value>Средний CLV</value></data>
<data name="Journal.Stat.AvgClv.Hint"><value>Средний прирост вероятности к линии закрытия.</value></data>
<data name="Journal.Stat.NetProfit"><value>Чистая прибыль</value></data>
<data name="Journal.Stat.NetProfit.Hint"><value>Возвраты минус ставки (учтены сыгравшие).</value></data>
<data name="Journal.Stat.TotalBets"><value>Всего ставок</value></data>
<data name="Journal.Stat.Pending"><value>В ожидании</value></data>
<data name="Journal.Stat.Won"><value>Победа</value></data>
<data name="Journal.Stat.Lost"><value>Проигрыш</value></data>
<data name="Journal.Stat.Void"><value>Возврат</value></data>
<data name="Journal.Section.Add"><value>Записать ставку</value></data>
<data name="Journal.Section.List"><value>Журнал ставок</value></data>
<data name="Journal.Action.Refresh"><value>Обновить</value></data>
<data name="Journal.Action.Resolve"><value>Рассчитать ожидающие</value></data>
<data name="Journal.Action.Submit"><value>Записать</value></data>
<data name="Journal.Action.Delete"><value>Удалить</value></data>
<data name="Journal.Action.Confirm"><value>Подтвердить</value></data>
<data name="Journal.Action.Cancel"><value>Отмена</value></data>
<data name="Journal.Field.EventId"><value>ID события</value></data>
<data name="Journal.Field.EventId.Hint"><value>Числовой ID из URL детальной страницы.</value></data>
<data name="Journal.Field.Type"><value>Тип ставки</value></data>
<data name="Journal.Field.Side"><value>Сторона</value></data>
<data name="Journal.Field.Value"><value>Порог</value></data>
<data name="Journal.Field.Value.Hint"><value>Гандикап или тотал (например 1.5, 2.5).</value></data>
<data name="Journal.Field.Rate"><value>Кэф на момент ставки</value></data>
<data name="Journal.Field.Stake"><value>Сумма ставки</value></data>
<data name="Journal.Field.Notes"><value>Заметки</value></data>
<data name="Journal.Field.Notes.Placeholder"><value>Тег стратегии, букмекер или что угодно для памяти…</value></data>
<data name="Journal.BetType.Win"><value>Победа</value></data>
<data name="Journal.BetType.Draw"><value>Ничья</value></data>
<data name="Journal.BetType.WinFora"><value>Фора</value></data>
<data name="Journal.BetType.Total"><value>Тотал</value></data>
<data name="Journal.Side.Side1"><value>Сторона 1</value></data>
<data name="Journal.Side.Side2"><value>Сторона 2</value></data>
<data name="Journal.Side.Draw"><value>Ничья</value></data>
<data name="Journal.Side.Less"><value>Меньше</value></data>
<data name="Journal.Side.More"><value>Больше</value></data>
<data name="Journal.Outcome.Pending"><value>Ожидает</value></data>
<data name="Journal.Outcome.Won"><value>Победа</value></data>
<data name="Journal.Outcome.Lost"><value>Проигрыш</value></data>
<data name="Journal.Outcome.Void"><value>Возврат</value></data>
<data name="Journal.Column.PlacedAt"><value>Размещено</value></data>
<data name="Journal.Column.Match"><value>Матч</value></data>
<data name="Journal.Column.Selection"><value>Выбор</value></data>
<data name="Journal.Column.Stake"><value>Ставка</value></data>
<data name="Journal.Column.Rate"><value>Кэф</value></data>
<data name="Journal.Column.Profit"><value>P&amp;L</value></data>
<data name="Journal.Column.Clv"><value>CLV</value></data>
<data name="Journal.Column.Outcome"><value>Итог</value></data>
<data name="Journal.Empty.None"><value>Ставок пока нет. Запишите свою ставку через форму выше — после окончания матча журнал авто-проставит результат и посчитает CLV против последнего пре-матч снимка.</value></data>
<data name="Journal.Empty.NotApplicable"><value>—</value></data>
<data name="Journal.Error.Generic"><value>Не удалось сохранить ставку — проверьте ID события и повторите.</value></data>
<data name="Journal.Resolve.None"><value>Ожидающих ставок к расчёту нет.</value></data>
<data name="Journal.Resolve.Done"><value>Рассчитано ожидающих: {0}.</value></data>
<data name="Journal.Confirm.Delete"><value>Удалить эту ставку безвозвратно?</value></data>
</root>
@@ -0,0 +1,98 @@
using Marathon.Application.Abstractions;
using Marathon.Application.UseCases;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
using DomainEventId = Marathon.Domain.ValueObjects.EventId;
namespace Marathon.UI.Services;
/// <summary>
/// Page-facing implementation of <see cref="IBetJournalService"/>. Composes the
/// four bet-journal use cases and joins event titles for the table rows.
/// </summary>
public sealed class BetJournalService : IBetJournalService
{
private readonly BuildBetJournalReportUseCase _build;
private readonly RecordPlacedBetUseCase _record;
private readonly ResolvePendingBetsUseCase _resolve;
private readonly DeletePlacedBetUseCase _delete;
private readonly IEventRepository _events;
public BetJournalService(
BuildBetJournalReportUseCase build,
RecordPlacedBetUseCase record,
ResolvePendingBetsUseCase resolve,
DeletePlacedBetUseCase delete,
IEventRepository events)
{
_build = build ?? throw new ArgumentNullException(nameof(build));
_record = record ?? throw new ArgumentNullException(nameof(record));
_resolve = resolve ?? throw new ArgumentNullException(nameof(resolve));
_delete = delete ?? throw new ArgumentNullException(nameof(delete));
_events = events ?? throw new ArgumentNullException(nameof(events));
}
public async Task<BetJournalVm> GetReportAsync(CancellationToken ct)
{
var report = await _build.ExecuteAsync(ct).ConfigureAwait(false);
if (report.Bets.Count == 0)
return new BetJournalVm(report.Stats, Array.Empty<BetJournalRowVm>());
// Resolve event titles in one pass — distinct ids only.
var distinctIds = report.Bets.Select(r => r.Bet.EventId).Distinct().ToList();
var titles = new Dictionary<DomainEventId, string>(distinctIds.Count);
foreach (var id in distinctIds)
{
ct.ThrowIfCancellationRequested();
var ev = await _events.GetAsync(id, ct).ConfigureAwait(false);
titles[id] = ev is not null
? string.Concat(ev.Side1Name, " vs ", ev.Side2Name)
: id.Value;
}
var rows = report.Bets
.Select(r => new BetJournalRowVm(
Id: r.Bet.Id,
EventTitle: titles.TryGetValue(r.Bet.EventId, out var t) ? t : r.Bet.EventId.Value,
Bet: r.Bet,
ClvProbabilityDelta: r.ClvProbabilityDelta))
.ToList();
return new BetJournalVm(report.Stats, rows);
}
public async Task<Guid> AddAsync(AddBetForm form, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(form);
if (!form.IsValid(out var error))
throw new ArgumentException(error ?? "Invalid form.", nameof(form));
var selection = new Bet(
scope: MatchScope.Instance,
type: form.Type,
side: form.Side,
value: form.Value is { } v ? new OddsValue(v) : null,
rate: new OddsRate(form.Rate));
var bet = new PlacedBet(
Id: Guid.NewGuid(),
EventId: new DomainEventId(form.EventId.Trim()),
Selection: selection,
Stake: form.Stake,
PlacedAt: MoscowTime.Now,
Outcome: BetOutcome.Pending,
Notes: form.Notes);
var stored = await _record.ExecuteAsync(bet, ct).ConfigureAwait(false);
return stored.Id;
}
public Task DeleteAsync(Guid betId, CancellationToken ct) =>
_delete.ExecuteAsync(betId, ct);
public Task<int> ResolvePendingAsync(CancellationToken ct) =>
_resolve.ExecuteAsync(ct);
}
@@ -0,0 +1,86 @@
using Marathon.Application.Betting;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
namespace Marathon.UI.Services;
/// <summary>
/// Page-facing projection of <see cref="BetJournalReport"/>. Adds the
/// pre-shaped event title per row so the Journal page never has to round-trip
/// back to <see cref="Marathon.Application.Abstractions.IEventRepository"/>.
/// </summary>
public sealed record BetJournalVm(
BetJournalStats Stats,
IReadOnlyList<BetJournalRowVm> Bets);
/// <summary>Row-level view model for the journal table.</summary>
public sealed record BetJournalRowVm(
Guid Id,
string EventTitle,
PlacedBet Bet,
decimal? ClvProbabilityDelta);
/// <summary>
/// Data the Add-Bet form posts. Loose-typed so the form can bind raw inputs;
/// <see cref="ToDomain"/> applies the same invariants as
/// <see cref="Bet"/> / <see cref="PlacedBet"/> and surfaces validation errors
/// as exceptions.
/// </summary>
public sealed class AddBetForm
{
public string EventId { get; set; } = string.Empty;
/// <summary>Bet type enum value — defaults to Win.</summary>
public BetType Type { get; set; } = BetType.Win;
/// <summary>Side enum value — Win/Side1 default.</summary>
public Side Side { get; set; } = Side.Side1;
/// <summary>Handicap / total threshold; required for WinFora and Total.</summary>
public decimal? Value { get; set; }
/// <summary>The decimal odds the user took at placement.</summary>
public decimal Rate { get; set; } = 1.90m;
public decimal Stake { get; set; } = 100m;
public string? Notes { get; set; }
/// <summary>Upper sanity caps so a typo cannot torch the KPI strip.</summary>
public const decimal MaxRate = 1000m;
/// <summary>Upper sanity cap on a single wager.</summary>
public const decimal MaxStake = 10_000_000m;
public bool IsValid(out string? error)
{
if (string.IsNullOrWhiteSpace(EventId)) { error = "EventId is required."; return false; }
if (Stake <= 0m) { error = "Stake must be positive."; return false; }
if (Stake > MaxStake) { error = $"Stake must be at most {MaxStake:N0}."; return false; }
if (Rate < 1.01m) { error = "Rate must be at least 1.01."; return false; }
if (Rate > MaxRate) { error = $"Rate must be at most {MaxRate:N0}."; return false; }
// Mirror Bet invariants — surface a friendly message instead of throwing
// ArgumentException deep in the use case.
switch (Type)
{
case BetType.Win:
if (Side is not (Side.Side1 or Side.Side2)) { error = "Win bet requires Side1 or Side2."; return false; }
break;
case BetType.Draw:
if (Side != Side.Draw) { error = "Draw bet requires Side = Draw."; return false; }
break;
case BetType.WinFora:
if (Side is not (Side.Side1 or Side.Side2)) { error = "Handicap bet requires Side1 or Side2."; return false; }
if (Value is null or 0m) { error = "Handicap bet needs a non-zero threshold."; return false; }
break;
case BetType.Total:
if (Side is not (Side.Less or Side.More)) { error = "Total bet requires Less or More."; return false; }
if (Value is null or 0m) { error = "Total bet needs a non-zero threshold."; return false; }
break;
}
error = null;
return true;
}
}
@@ -0,0 +1,29 @@
namespace Marathon.UI.Services;
/// <summary>
/// Browsing facade over the bet-journal use cases. The Journal page binds to
/// this — never the use cases directly — so view-model shaping, event-title
/// joining, and validation surface in one place.
/// </summary>
public interface IBetJournalService
{
/// <summary>Builds the full report and projects it for the UI.</summary>
Task<BetJournalVm> GetReportAsync(CancellationToken ct);
/// <summary>
/// Validates and persists a manually entered bet. Returns the newly stored
/// row's id. Throws <see cref="InvalidOperationException"/> when the form
/// validates but references an unknown event.
/// </summary>
/// <exception cref="ArgumentException">Form fails its own validation.</exception>
Task<Guid> AddAsync(AddBetForm form, CancellationToken ct);
/// <summary>Removes a bet by id. No-op when the id is unknown.</summary>
Task DeleteAsync(Guid betId, CancellationToken ct);
/// <summary>
/// Sweeps pending bets and grades the ones whose events are now resolved.
/// Returns the count graded in this pass.
/// </summary>
Task<int> ResolvePendingAsync(CancellationToken ct);
}
@@ -59,6 +59,7 @@ public static class UiServicesExtensions
services.AddScoped<IAnomalyBrowsingService, AnomalyBrowsingService>();
services.AddScoped<IAnomalyInsightsService, AnomalyInsightsService>();
services.AddScoped<IResultsBrowsingService, ResultsBrowsingService>();
services.AddScoped<IBetJournalService, BetJournalService>();
// Settings writer — file path is host-resolved.
services.AddSingleton<ISettingsWriter>(_ => new JsonSettingsWriter(settingsLocalPath));
@@ -0,0 +1,96 @@
using FluentAssertions;
using Marathon.Application.Betting;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
namespace Marathon.Application.Tests.Betting;
/// <summary>
/// Unit tests for <see cref="ClosingLineValueCalculator"/> covering the math
/// itself and the snapshot-matching path used by the report use case.
/// </summary>
public sealed class ClosingLineValueCalculatorTests
{
private static readonly EventId EventId = new("11111111");
[Fact]
public void Compute_Should_ReturnPositive_When_TakenRate_BeatsClose()
{
// Taken 2.20 (implied 0.4545); closed 2.00 (implied 0.5000) → CLV = +0.0455
var clv = ClosingLineValueCalculator.Compute(takenRate: 2.20m, closingRate: 2.00m);
clv.Should().BeGreaterThan(0m);
clv.Should().BeApproximately(0.04545m, 0.00001m);
}
[Fact]
public void Compute_Should_ReturnNegative_When_TakenRate_WorseThanClose()
{
// Taken 1.80 (0.5556); closed 2.00 (0.5000) → CLV = -0.0556
var clv = ClosingLineValueCalculator.Compute(takenRate: 1.80m, closingRate: 2.00m);
clv.Should().BeLessThan(0m);
clv.Should().BeApproximately(-0.05556m, 0.00001m);
}
[Fact]
public void Compute_Should_ReturnZero_When_RatesMatch()
{
ClosingLineValueCalculator.Compute(2.00m, 2.00m).Should().Be(0m);
}
[Theory]
[InlineData(0)]
[InlineData(-1)]
public void Compute_Should_Throw_When_AnyRateIsZeroOrNegative(decimal rate)
{
((Action)(() => ClosingLineValueCalculator.Compute(rate, 2m)))
.Should().Throw<ArgumentOutOfRangeException>();
((Action)(() => ClosingLineValueCalculator.Compute(2m, rate)))
.Should().Throw<ArgumentOutOfRangeException>();
}
[Fact]
public void TryCompute_Should_ReturnNull_When_NoMatchingBetInSnapshot()
{
var taken = new Bet(MatchScope.Instance, BetType.Win, Side.Side1,
value: null, new OddsRate(2.20m));
// Snapshot contains only Side2 — no match for Side1 Win.
var snapshot = new OddsSnapshot(EventId, DateTimeOffset.UtcNow, OddsSource.PreMatch,
new[] { new Bet(MatchScope.Instance, BetType.Win, Side.Side2, null, new OddsRate(1.70m)) });
var clv = ClosingLineValueCalculator.TryCompute(2.20m, taken, snapshot);
clv.Should().BeNull();
}
[Fact]
public void TryCompute_Should_ReturnNull_When_SnapshotIsNull()
{
var taken = new Bet(MatchScope.Instance, BetType.Win, Side.Side1, null, new OddsRate(2m));
ClosingLineValueCalculator.TryCompute(2m, taken, closingSnapshot: null).Should().BeNull();
}
[Fact]
public void TryCompute_Should_MatchOnScopeTypeSideAndValue()
{
// Two handicap markets with different thresholds — pick the right one.
var taken = new Bet(MatchScope.Instance, BetType.WinFora, Side.Side1,
new OddsValue(-1.5m), new OddsRate(2.20m));
var snapshot = new OddsSnapshot(EventId, DateTimeOffset.UtcNow, OddsSource.PreMatch,
new[]
{
new Bet(MatchScope.Instance, BetType.WinFora, Side.Side1,
new OddsValue(-2.5m), new OddsRate(3.50m)), // wrong threshold
new Bet(MatchScope.Instance, BetType.WinFora, Side.Side1,
new OddsValue(-1.5m), new OddsRate(2.00m)), // match
});
var clv = ClosingLineValueCalculator.TryCompute(2.20m, taken, snapshot);
clv.Should().BeApproximately(0.04545m, 0.00001m);
}
}
@@ -0,0 +1,204 @@
using FluentAssertions;
using Marathon.Application.Abstractions;
using Marathon.Application.UseCases;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
namespace Marathon.Application.Tests.UseCases;
public sealed class BuildBetJournalReportUseCaseTests
{
private static readonly TimeSpan MoscowOffset = TimeSpan.FromHours(3);
private static readonly DateTimeOffset Placed = new(2026, 5, 16, 12, 0, 0, MoscowOffset);
private static readonly DateTimeOffset Kickoff = new(2026, 5, 16, 18, 0, 0, MoscowOffset);
private readonly IPlacedBetRepository _bets = Substitute.For<IPlacedBetRepository>();
private readonly IEventRepository _events = Substitute.For<IEventRepository>();
private readonly ISnapshotRepository _snapshots = Substitute.For<ISnapshotRepository>();
private BuildBetJournalReportUseCase CreateSut() =>
new(_bets, _events, _snapshots, NullLogger<BuildBetJournalReportUseCase>.Instance);
private static PlacedBet MakeBet(
EventId id,
BetOutcome outcome,
Side side = Side.Side1,
decimal stake = 100m,
decimal rate = 2.10m) =>
new(
Guid.NewGuid(), id,
new Bet(MatchScope.Instance, BetType.Win, side, null, new OddsRate(rate)),
stake, Placed, outcome, null);
private static Event MakeEvent(EventId id, DateTimeOffset scheduledAt) =>
new(id, new SportCode(11), "BY", "L1", "Cat", scheduledAt, "Team A", "Team B");
private static OddsSnapshot MakeSnapshot(EventId id, DateTimeOffset at, decimal rateSide1) =>
new(id, at, OddsSource.PreMatch,
new[]
{
new Bet(MatchScope.Instance, BetType.Win, Side.Side1, null, new OddsRate(rateSide1)),
new Bet(MatchScope.Instance, BetType.Win, Side.Side2, null, new OddsRate(2.00m)),
});
[Fact]
public async Task Should_ReturnEmptyReport_When_NoBets()
{
_bets.ListAsync(Arg.Any<CancellationToken>())
.Returns(Array.Empty<PlacedBet>().ToList().AsReadOnly());
var report = await CreateSut().ExecuteAsync(CancellationToken.None);
report.Bets.Should().BeEmpty();
report.Stats.TotalBets.Should().Be(0);
report.Stats.RoiPercent.Should().BeNull();
}
[Fact]
public async Task Should_AggregateStats_AcrossMixedOutcomes()
{
var id1 = new EventId("e-1");
var id2 = new EventId("e-2");
var id3 = new EventId("e-3");
_bets.ListAsync(Arg.Any<CancellationToken>())
.Returns(new[]
{
MakeBet(id1, BetOutcome.Won, stake: 100m, rate: 2.00m), // gross 200, +100
MakeBet(id2, BetOutcome.Lost, stake: 100m, rate: 2.00m), // gross 0, -100
MakeBet(id3, BetOutcome.Pending),
}.ToList().AsReadOnly());
// Wire events so the report can compute CLV (we don't need actual CLV here — leave snapshots empty).
foreach (var id in new[] { id1, id2, id3 })
{
_events.GetAsync(id, Arg.Any<CancellationToken>()).Returns(MakeEvent(id, Kickoff));
_snapshots.GetLatestPreMatchAsync(id, Arg.Any<DateTimeOffset>(), Arg.Any<CancellationToken>())
.Returns((OddsSnapshot?)null);
}
var report = await CreateSut().ExecuteAsync(CancellationToken.None);
report.Stats.TotalBets.Should().Be(3);
report.Stats.PendingCount.Should().Be(1);
report.Stats.WonCount.Should().Be(1);
report.Stats.LostCount.Should().Be(1);
report.Stats.TotalStaked.Should().Be(200m, "pending bets are excluded from totals");
report.Stats.TotalReturned.Should().Be(200m);
report.Stats.NetProfit.Should().Be(0m);
report.Stats.RoiPercent.Should().Be(0m);
report.Stats.StrikeRatePercent.Should().Be(50m);
}
[Fact]
public async Task Should_ComputeClv_AgainstClosingSnapshot()
{
var id = new EventId("clv-event");
var bet = MakeBet(id, BetOutcome.Won, rate: 2.20m, stake: 100m);
_bets.ListAsync(Arg.Any<CancellationToken>())
.Returns(new[] { bet }.ToList().AsReadOnly());
_events.GetAsync(id, Arg.Any<CancellationToken>()).Returns(MakeEvent(id, Kickoff));
// Closing snapshot returned by the dedicated repo method.
_snapshots.GetLatestPreMatchAsync(id, Kickoff, Arg.Any<CancellationToken>())
.Returns(MakeSnapshot(id, Kickoff.AddMinutes(-5), rateSide1: 2.00m));
var report = await CreateSut().ExecuteAsync(CancellationToken.None);
report.Bets.Should().HaveCount(1);
report.Bets[0].ClvProbabilityDelta.Should().NotBeNull();
// taken 2.20 vs closing 2.00 → +0.04545
report.Bets[0].ClvProbabilityDelta!.Value.Should().BeApproximately(0.04545m, 0.00001m);
report.Stats.AverageClvProbabilityDelta.Should().NotBeNull();
}
[Fact]
public async Task Should_LeaveClvNull_When_NoClosingSnapshotAvailable()
{
var id = new EventId("no-close");
_bets.ListAsync(Arg.Any<CancellationToken>())
.Returns(new[] { MakeBet(id, BetOutcome.Won) }.ToList().AsReadOnly());
_events.GetAsync(id, Arg.Any<CancellationToken>()).Returns(MakeEvent(id, Kickoff));
_snapshots.GetLatestPreMatchAsync(id, Kickoff, Arg.Any<CancellationToken>())
.Returns((OddsSnapshot?)null);
var report = await CreateSut().ExecuteAsync(CancellationToken.None);
report.Bets[0].ClvProbabilityDelta.Should().BeNull();
report.Stats.AverageClvProbabilityDelta.Should().BeNull(
"no rows had a computable CLV — average is undefined");
}
[Fact]
public async Task Should_ExcludeVoidStakes_FromRoiTurnover()
{
// 1 Won (+100), 1 Lost (-100), 1 Void (stake returned). Industry-standard
// ROI excludes pushes from turnover, so total staked = 200, returned 200,
// net 0, ROI 0%. If voids were included turnover would be 300 → ROI ≈ 0%
// numerator but inflated denominator semantics.
var ids = Enumerable.Range(1, 3)
.Select(i => new EventId($"void-{i}")).ToArray();
_bets.ListAsync(Arg.Any<CancellationToken>())
.Returns(new[]
{
MakeBet(ids[0], BetOutcome.Won, stake: 100m, rate: 2.00m),
MakeBet(ids[1], BetOutcome.Lost, stake: 100m, rate: 2.00m),
MakeBet(ids[2], BetOutcome.Void, stake: 100m, rate: 2.00m),
}.ToList().AsReadOnly());
foreach (var id in ids)
{
_events.GetAsync(id, Arg.Any<CancellationToken>()).Returns(MakeEvent(id, Kickoff));
_snapshots.GetLatestPreMatchAsync(id, Arg.Any<DateTimeOffset>(), Arg.Any<CancellationToken>())
.Returns((OddsSnapshot?)null);
}
var report = await CreateSut().ExecuteAsync(CancellationToken.None);
report.Stats.VoidCount.Should().Be(1);
report.Stats.TotalStaked.Should().Be(200m,
"void bets are pushes — the stake was returned and should not count as turnover");
report.Stats.TotalReturned.Should().Be(200m);
report.Stats.NetProfit.Should().Be(0m);
report.Stats.RoiPercent.Should().Be(0m);
}
[Fact]
public async Task Should_OrderBets_NewestPlacedFirst()
{
var ids = Enumerable.Range(0, 3).Select(i => new EventId($"ord-{i}")).ToArray();
var older = new DateTimeOffset(2026, 5, 10, 12, 0, 0, MoscowOffset);
var newer = new DateTimeOffset(2026, 5, 16, 12, 0, 0, MoscowOffset);
// Bet 0 is the middle one, bet 1 oldest, bet 2 newest.
var b0 = new PlacedBet(Guid.NewGuid(), ids[0],
new Bet(MatchScope.Instance, BetType.Win, Side.Side1, null, new OddsRate(2m)),
100m, older.AddDays(1), BetOutcome.Won, null);
var b1 = new PlacedBet(Guid.NewGuid(), ids[1],
new Bet(MatchScope.Instance, BetType.Win, Side.Side1, null, new OddsRate(2m)),
100m, older, BetOutcome.Lost, null);
var b2 = new PlacedBet(Guid.NewGuid(), ids[2],
new Bet(MatchScope.Instance, BetType.Win, Side.Side1, null, new OddsRate(2m)),
100m, newer, BetOutcome.Pending, null);
_bets.ListAsync(Arg.Any<CancellationToken>())
.Returns(new[] { b0, b1, b2 }.ToList().AsReadOnly());
foreach (var id in ids)
{
_events.GetAsync(id, Arg.Any<CancellationToken>()).Returns(MakeEvent(id, Kickoff));
_snapshots.GetLatestPreMatchAsync(id, Arg.Any<DateTimeOffset>(), Arg.Any<CancellationToken>())
.Returns((OddsSnapshot?)null);
}
var report = await CreateSut().ExecuteAsync(CancellationToken.None);
report.Bets.Select(r => r.Bet.Id).Should().ContainInOrder(b2.Id, b0.Id, b1.Id);
}
}
@@ -0,0 +1,75 @@
using FluentAssertions;
using Marathon.Application.Abstractions;
using Marathon.Application.UseCases;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
namespace Marathon.Application.Tests.UseCases;
public sealed class RecordPlacedBetUseCaseTests
{
private static readonly TimeSpan MoscowOffset = TimeSpan.FromHours(3);
private readonly IPlacedBetRepository _bets = Substitute.For<IPlacedBetRepository>();
private readonly IEventRepository _events = Substitute.For<IEventRepository>();
private readonly IResultRepository _results = Substitute.For<IResultRepository>();
private RecordPlacedBetUseCase CreateSut() =>
new(_bets, _events, _results, NullLogger<RecordPlacedBetUseCase>.Instance);
private static PlacedBet MakePending(EventId id, Side side = Side.Side1) =>
new(
Id: Guid.NewGuid(),
EventId: id,
Selection: new Bet(MatchScope.Instance, BetType.Win, side, null, new OddsRate(2.10m)),
Stake: 100m,
PlacedAt: new DateTimeOffset(2026, 5, 16, 12, 0, 0, MoscowOffset),
Outcome: BetOutcome.Pending,
Notes: null);
[Fact]
public async Task Should_Throw_When_EventDoesNotExist()
{
var id = new EventId("missing");
_events.GetAsync(id, Arg.Any<CancellationToken>()).Returns((Event?)null);
var act = async () => await CreateSut().ExecuteAsync(MakePending(id), CancellationToken.None);
await act.Should().ThrowAsync<InvalidOperationException>().WithMessage("*unknown event*");
await _bets.DidNotReceive().AddAsync(Arg.Any<PlacedBet>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Should_PersistPending_When_NoResultYet()
{
var id = new EventId("event001");
_events.GetAsync(id, Arg.Any<CancellationToken>()).Returns(TestFixtures.MakeEvent(id.Value));
_results.GetAsync(id, Arg.Any<CancellationToken>()).Returns((EventResult?)null);
var bet = MakePending(id);
var stored = await CreateSut().ExecuteAsync(bet, CancellationToken.None);
stored.Outcome.Should().Be(BetOutcome.Pending, "no result yet — should remain pending");
await _bets.Received(1).AddAsync(Arg.Any<PlacedBet>(), Arg.Any<CancellationToken>());
await _bets.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task Should_AutoGrade_When_ResultAlreadyAvailable()
{
var id = new EventId("event002");
_events.GetAsync(id, Arg.Any<CancellationToken>()).Returns(TestFixtures.MakeEvent(id.Value));
_results.GetAsync(id, Arg.Any<CancellationToken>())
.Returns(new EventResult(id, 2, 1, Side.Side1, DateTimeOffset.UtcNow));
var bet = MakePending(id, side: Side.Side1);
var stored = await CreateSut().ExecuteAsync(bet, CancellationToken.None);
stored.Outcome.Should().Be(BetOutcome.Won, "Side1 was selected and Side1 won");
}
}
@@ -0,0 +1,91 @@
using FluentAssertions;
using Marathon.Application.Abstractions;
using Marathon.Application.UseCases;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
namespace Marathon.Application.Tests.UseCases;
public sealed class ResolvePendingBetsUseCaseTests
{
private static readonly TimeSpan MoscowOffset = TimeSpan.FromHours(3);
private static readonly DateTimeOffset Placed =
new(2026, 5, 16, 12, 0, 0, MoscowOffset);
private readonly IPlacedBetRepository _bets = Substitute.For<IPlacedBetRepository>();
private readonly IResultRepository _results = Substitute.For<IResultRepository>();
private ResolvePendingBetsUseCase CreateSut() =>
new(_bets, _results, NullLogger<ResolvePendingBetsUseCase>.Instance);
private static PlacedBet MakePending(EventId id, Side side = Side.Side1) =>
new(
Guid.NewGuid(), id,
new Bet(MatchScope.Instance, BetType.Win, side, null, new OddsRate(2.10m)),
100m, Placed, BetOutcome.Pending, null);
[Fact]
public async Task Should_ReturnZero_When_NoPendingBets()
{
_bets.ListByOutcomeAsync(BetOutcome.Pending, Arg.Any<CancellationToken>())
.Returns(Array.Empty<PlacedBet>().ToList().AsReadOnly());
var count = await CreateSut().ExecuteAsync(CancellationToken.None);
count.Should().Be(0);
await _bets.DidNotReceive().UpdateAsync(Arg.Any<PlacedBet>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Should_GradeBetsWithResults_AndLeaveOthersAlone()
{
var idGraded = new EventId("event-1");
var idUngraded = new EventId("event-2");
_bets.ListByOutcomeAsync(BetOutcome.Pending, Arg.Any<CancellationToken>())
.Returns(new[]
{
MakePending(idGraded, side: Side.Side1),
MakePending(idUngraded, side: Side.Side2),
}.ToList().AsReadOnly());
_results.GetAsync(idGraded, Arg.Any<CancellationToken>())
.Returns(new EventResult(idGraded, 2, 1, Side.Side1, DateTimeOffset.UtcNow));
_results.GetAsync(idUngraded, Arg.Any<CancellationToken>())
.Returns((EventResult?)null);
var count = await CreateSut().ExecuteAsync(CancellationToken.None);
count.Should().Be(1, "only event-1 has a result");
await _bets.Received(1).UpdateAsync(
Arg.Is<PlacedBet>(b => b.EventId == idGraded && b.Outcome == BetOutcome.Won),
Arg.Any<CancellationToken>());
await _bets.DidNotReceive().UpdateAsync(
Arg.Is<PlacedBet>(b => b.EventId == idUngraded),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task Should_CacheResultLookups_PerEvent()
{
// Two pending bets on the same event — only one result fetch should fire.
var id = new EventId("event-shared");
_bets.ListByOutcomeAsync(BetOutcome.Pending, Arg.Any<CancellationToken>())
.Returns(new[]
{
MakePending(id, side: Side.Side1),
MakePending(id, side: Side.Side2),
}.ToList().AsReadOnly());
_results.GetAsync(id, Arg.Any<CancellationToken>())
.Returns(new EventResult(id, 2, 1, Side.Side1, DateTimeOffset.UtcNow));
await CreateSut().ExecuteAsync(CancellationToken.None);
await _results.Received(1).GetAsync(id, Arg.Any<CancellationToken>());
}
}
@@ -0,0 +1,127 @@
using FluentAssertions;
using Marathon.Domain.Betting;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
namespace Marathon.Domain.Tests.Betting;
/// <summary>
/// Unit tests for <see cref="BetOutcomeResolver"/> across every bet type +
/// every important boundary (handicap push, total push, period-scope null).
/// </summary>
public sealed class BetOutcomeResolverTests
{
private static readonly EventId EventId = new("12345678");
// ── Win bets ─────────────────────────────────────────────────────────────
[Theory]
[InlineData(Side.Side1, Side.Side1, BetOutcome.Won)]
[InlineData(Side.Side1, Side.Side2, BetOutcome.Lost)]
[InlineData(Side.Side1, Side.Draw, BetOutcome.Lost)]
[InlineData(Side.Side2, Side.Side2, BetOutcome.Won)]
[InlineData(Side.Side2, Side.Side1, BetOutcome.Lost)]
public void Should_GradeWinBet(Side selectionSide, Side winner, BetOutcome expected)
{
var bet = MakeBet(BetType.Win, selectionSide);
var result = MakeResult(winner);
BetOutcomeResolver.Resolve(bet, result).Should().Be(expected);
}
// ── Draw bets ────────────────────────────────────────────────────────────
[Theory]
[InlineData(Side.Draw, BetOutcome.Won)]
[InlineData(Side.Side1, BetOutcome.Lost)]
[InlineData(Side.Side2, BetOutcome.Lost)]
public void Should_GradeDrawBet(Side winner, BetOutcome expected)
{
var bet = MakeBet(BetType.Draw, Side.Draw);
var result = MakeResult(winner, 1, 1);
BetOutcomeResolver.Resolve(bet, result).Should().Be(expected);
}
// ── Handicap (WinFora) ───────────────────────────────────────────────────
[Theory]
// Side1 with +1.5 handicap; final 0-2 → adjusted 1.5 vs 2 → loss
[InlineData(Side.Side1, 1.5, 0, 2, BetOutcome.Lost)]
// Side1 with +1.5; final 1-2 → adjusted 2.5 vs 2 → win
[InlineData(Side.Side1, 1.5, 1, 2, BetOutcome.Won)]
// Side1 with -1.5; final 3-1 → adjusted 1.5 vs 1 → win
[InlineData(Side.Side1, -1.5, 3, 1, BetOutcome.Won)]
// Whole-number handicap that ties: Side1 +1, final 1-2 → 2 vs 2 → push (Void)
[InlineData(Side.Side1, 1, 1, 2, BetOutcome.Void)]
// Side2 with -1; final 0-2 → adjusted Side2 1 vs Side1 0 → win
[InlineData(Side.Side2, -1, 0, 2, BetOutcome.Won)]
public void Should_GradeHandicapBet(
Side side, double handicap, int s1, int s2, BetOutcome expected)
{
var bet = new Bet(MatchScope.Instance, BetType.WinFora, side,
new OddsValue((decimal)handicap), new OddsRate(1.85m));
var result = new EventResult(EventId, s1, s2,
DeriveWinner(s1, s2), DateTimeOffset.UtcNow);
BetOutcomeResolver.Resolve(bet, result).Should().Be(expected);
}
// ── Totals ───────────────────────────────────────────────────────────────
[Theory]
// Over 2.5; total 3 → win
[InlineData(Side.More, 2.5, 1, 2, BetOutcome.Won)]
// Over 2.5; total 2 → loss
[InlineData(Side.More, 2.5, 0, 2, BetOutcome.Lost)]
// Over 3.0; total 3 → push (Void)
[InlineData(Side.More, 3.0, 1, 2, BetOutcome.Void)]
// Under 2.5; total 2 → win
[InlineData(Side.Less, 2.5, 1, 1, BetOutcome.Won)]
// Under 2.5; total 3 → loss
[InlineData(Side.Less, 2.5, 1, 2, BetOutcome.Lost)]
public void Should_GradeTotalBet(
Side side, double threshold, int s1, int s2, BetOutcome expected)
{
var bet = new Bet(MatchScope.Instance, BetType.Total, side,
new OddsValue((decimal)threshold), new OddsRate(1.85m));
var result = new EventResult(EventId, s1, s2,
DeriveWinner(s1, s2), DateTimeOffset.UtcNow);
BetOutcomeResolver.Resolve(bet, result).Should().Be(expected);
}
// ── Period-scope guard ───────────────────────────────────────────────────
[Fact]
public void Should_ReturnNull_When_BetScopeIsPeriod()
{
var bet = new Bet(new PeriodScope(1), BetType.Win, Side.Side1,
value: null, new OddsRate(2.10m));
var result = MakeResult(Side.Side1);
BetOutcomeResolver.Resolve(bet, result).Should().BeNull(
"period scope cannot be graded from full-time score alone");
}
[Fact]
public void Should_Throw_When_BetOrResultIsNull()
{
((Action)(() => BetOutcomeResolver.Resolve(null!, MakeResult(Side.Side1))))
.Should().Throw<ArgumentNullException>();
((Action)(() => BetOutcomeResolver.Resolve(MakeBet(BetType.Win, Side.Side1), null!)))
.Should().Throw<ArgumentNullException>();
}
// ── Helpers ──────────────────────────────────────────────────────────────
private static Bet MakeBet(BetType type, Side side) =>
new(MatchScope.Instance, type, side, value: null, new OddsRate(2.00m));
private static EventResult MakeResult(Side winner, int s1 = 1, int s2 = 0) =>
new(EventId, s1, s2, winner, DateTimeOffset.UtcNow);
private static Side DeriveWinner(int s1, int s2) =>
s1 == s2 ? Side.Draw : (s1 > s2 ? Side.Side1 : Side.Side2);
}
@@ -0,0 +1,120 @@
using FluentAssertions;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
namespace Marathon.Domain.Tests.Entities;
/// <summary>
/// Invariants and derived-property tests for <see cref="PlacedBet"/>.
/// </summary>
public sealed class PlacedBetTests
{
private static readonly TimeSpan MoscowOffset = TimeSpan.FromHours(3);
private static readonly DateTimeOffset MoscowMoment =
new(2026, 5, 16, 12, 0, 0, MoscowOffset);
private static PlacedBet Make(BetOutcome outcome, decimal rate = 2.10m, decimal stake = 100m) =>
new(
Id: Guid.NewGuid(),
EventId: new EventId("12345678"),
Selection: new Bet(MatchScope.Instance, BetType.Win, Side.Side1,
value: null, new OddsRate(rate)),
Stake: stake,
PlacedAt: MoscowMoment,
Outcome: outcome,
Notes: null);
[Fact]
public void Should_ComputeWonReturn_As_StakeTimesRate()
{
var bet = Make(BetOutcome.Won, rate: 2.10m, stake: 100m);
bet.GrossReturn.Should().Be(210m);
bet.NetProfit.Should().Be(110m);
}
[Fact]
public void Should_ComputeLossReturn_As_Zero()
{
var bet = Make(BetOutcome.Lost, stake: 50m);
bet.GrossReturn.Should().Be(0m);
bet.NetProfit.Should().Be(-50m);
}
[Fact]
public void Should_ReturnStake_When_Outcome_IsVoid()
{
var bet = Make(BetOutcome.Void, stake: 75m);
bet.GrossReturn.Should().Be(75m);
bet.NetProfit.Should().Be(0m);
}
[Fact]
public void Should_ReturnNullProfit_When_OutcomeIsPending()
{
var bet = Make(BetOutcome.Pending);
bet.GrossReturn.Should().BeNull();
bet.NetProfit.Should().BeNull();
}
[Fact]
public void WithOutcome_Should_ReturnNewInstance_With_GradedOutcome()
{
var pending = Make(BetOutcome.Pending);
var graded = pending.WithOutcome(BetOutcome.Won);
graded.Should().NotBeSameAs(pending);
graded.Outcome.Should().Be(BetOutcome.Won);
graded.Id.Should().Be(pending.Id);
graded.Stake.Should().Be(pending.Stake);
}
[Fact]
public void Should_Throw_When_StakeIsZeroOrNegative()
{
var act = () => new PlacedBet(
Id: Guid.NewGuid(),
EventId: new EventId("11111111"),
Selection: new Bet(MatchScope.Instance, BetType.Win, Side.Side1, null, new OddsRate(2m)),
Stake: 0m,
PlacedAt: MoscowMoment,
Outcome: BetOutcome.Pending,
Notes: null);
act.Should().Throw<ArgumentOutOfRangeException>().WithMessage("*Stake*");
}
[Fact]
public void Should_Throw_When_PlacedAt_IsNotMoscowOffset()
{
var act = () => new PlacedBet(
Id: Guid.NewGuid(),
EventId: new EventId("11111111"),
Selection: new Bet(MatchScope.Instance, BetType.Win, Side.Side1, null, new OddsRate(2m)),
Stake: 100m,
PlacedAt: DateTimeOffset.UtcNow, // UTC offset, not Moscow
Outcome: BetOutcome.Pending,
Notes: null);
act.Should().Throw<ArgumentException>().WithMessage("*Moscow*");
}
[Fact]
public void Should_NormaliseWhitespace_Notes_To_Null()
{
var bet = new PlacedBet(
Id: Guid.NewGuid(),
EventId: new EventId("11111111"),
Selection: new Bet(MatchScope.Instance, BetType.Win, Side.Side1, null, new OddsRate(2m)),
Stake: 100m,
PlacedAt: MoscowMoment,
Outcome: BetOutcome.Pending,
Notes: " ");
bet.Notes.Should().BeNull();
}
}
@@ -0,0 +1,200 @@
using FluentAssertions;
using Marathon.Application.Storage;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
using Marathon.Infrastructure.Persistence;
using Marathon.Infrastructure.Persistence.Repositories;
namespace Marathon.Infrastructure.Tests.Persistence;
/// <summary>
/// Round-trip + query tests for <see cref="PlacedBetRepository"/>. Uses the
/// in-memory SQLite fixture so the schema + indices declared in the migration
/// are exercised on every test.
/// </summary>
public sealed class PlacedBetRoundTripTests : IDisposable
{
private static readonly TimeSpan MoscowOffset = TimeSpan.FromHours(3);
private static readonly DateTimeOffset Placed =
new(2026, 5, 16, 12, 0, 0, MoscowOffset);
private readonly InMemoryDbFixture _fixture;
private readonly PlacedBetRepository _repo;
public PlacedBetRoundTripTests()
{
_fixture = new InMemoryDbFixture();
_repo = new PlacedBetRepository(_fixture.DbContext);
}
public void Dispose() => _fixture.Dispose();
private static PlacedBet MakeBet(
Guid? id = null,
string eventCode = "12345678",
BetType type = BetType.Win,
Side side = Side.Side1,
decimal? value = null,
decimal rate = 2.10m,
decimal stake = 100m,
DateTimeOffset? placedAt = null,
BetOutcome outcome = BetOutcome.Pending,
string? notes = null) =>
new(
Id: id ?? Guid.NewGuid(),
EventId: new EventId(eventCode),
Selection: new Bet(MatchScope.Instance, type, side,
value is { } v ? new OddsValue(v) : null, new OddsRate(rate)),
Stake: stake,
PlacedAt: placedAt ?? Placed,
Outcome: outcome,
Notes: notes);
[Fact]
public async Task PlacedBet_RoundTrip_PreservesAllFields()
{
var bet = MakeBet(
type: BetType.WinFora,
side: Side.Side2,
value: -1.5m,
rate: 2.30m,
stake: 250m,
notes: "test note");
await _repo.AddAsync(bet);
await _repo.SaveChangesAsync();
_fixture.DbContext.ChangeTracker.Clear();
var retrieved = await _repo.GetAsync(bet.Id);
retrieved.Should().NotBeNull();
retrieved!.Id.Should().Be(bet.Id);
retrieved.EventId.Value.Should().Be("12345678");
retrieved.Selection.Type.Should().Be(BetType.WinFora);
retrieved.Selection.Side.Should().Be(Side.Side2);
retrieved.Selection.Value!.Value.Should().Be(-1.5m);
retrieved.Selection.Rate.Value.Should().Be(2.30m);
retrieved.Stake.Should().Be(250m);
retrieved.PlacedAt.Should().Be(Placed);
retrieved.PlacedAt.Offset.Should().Be(MoscowOffset);
retrieved.Outcome.Should().Be(BetOutcome.Pending);
retrieved.Notes.Should().Be("test note");
}
[Fact]
public async Task ListByOutcomeAsync_ReturnsOnlyMatching()
{
await _repo.AddAsync(MakeBet(outcome: BetOutcome.Pending, eventCode: "1"));
await _repo.AddAsync(MakeBet(outcome: BetOutcome.Won, eventCode: "2"));
await _repo.AddAsync(MakeBet(outcome: BetOutcome.Pending, eventCode: "3"));
await _repo.SaveChangesAsync();
_fixture.DbContext.ChangeTracker.Clear();
var pending = await _repo.ListByOutcomeAsync(BetOutcome.Pending);
pending.Should().HaveCount(2);
pending.Should().OnlyContain(b => b.Outcome == BetOutcome.Pending);
}
[Fact]
public async Task ListByEventAsync_ReturnsEveryBet_OnTheEvent()
{
await _repo.AddAsync(MakeBet(eventCode: "evt-A", side: Side.Side1));
await _repo.AddAsync(MakeBet(eventCode: "evt-A", side: Side.Side2));
await _repo.AddAsync(MakeBet(eventCode: "evt-B"));
await _repo.SaveChangesAsync();
_fixture.DbContext.ChangeTracker.Clear();
var onA = await _repo.ListByEventAsync(new EventId("evt-A"));
onA.Should().HaveCount(2);
onA.Should().OnlyContain(b => b.EventId.Value == "evt-A");
}
[Fact]
public async Task ListByDateRangeAsync_FiltersByPlacedAt()
{
var older = new DateTimeOffset(2026, 5, 1, 12, 0, 0, MoscowOffset);
var newer = new DateTimeOffset(2026, 5, 20, 12, 0, 0, MoscowOffset);
await _repo.AddAsync(MakeBet(placedAt: older, eventCode: "old"));
await _repo.AddAsync(MakeBet(placedAt: newer, eventCode: "new"));
await _repo.SaveChangesAsync();
_fixture.DbContext.ChangeTracker.Clear();
var range = new DateRange(
from: new DateTimeOffset(2026, 5, 10, 0, 0, 0, MoscowOffset),
to: new DateTimeOffset(2026, 5, 31, 0, 0, 0, MoscowOffset));
var inRange = await _repo.ListByDateRangeAsync(range);
inRange.Should().HaveCount(1);
inRange[0].EventId.Value.Should().Be("new");
}
[Fact]
public async Task UpdateAsync_PersistsOutcomeChange()
{
var bet = MakeBet(outcome: BetOutcome.Pending);
await _repo.AddAsync(bet);
await _repo.SaveChangesAsync();
_fixture.DbContext.ChangeTracker.Clear();
var graded = bet.WithOutcome(BetOutcome.Won);
await _repo.UpdateAsync(graded);
await _repo.SaveChangesAsync();
_fixture.DbContext.ChangeTracker.Clear();
var reloaded = await _repo.GetAsync(bet.Id);
reloaded!.Outcome.Should().Be(BetOutcome.Won);
}
[Fact]
public async Task PlacedBet_Survives_Event_Deletion()
{
// Documents the explicit "no foreign key" design choice — the journal
// is user data and must survive snapshot retention pruning the source
// event row.
var eventRepo = new EventRepository(_fixture.DbContext);
var evt = new Event(
Id: new EventId("evt-prune"),
Sport: new SportCode(11),
CountryCode: "England",
LeagueId: "league",
Category: string.Empty,
ScheduledAt: new DateTimeOffset(2026, 5, 16, 20, 0, 0, MoscowOffset),
Side1Name: "Home",
Side2Name: "Away");
await eventRepo.AddAsync(evt);
await eventRepo.SaveChangesAsync();
var bet = MakeBet(eventCode: "evt-prune");
await _repo.AddAsync(bet);
await _repo.SaveChangesAsync();
_fixture.DbContext.ChangeTracker.Clear();
// Delete the event — the bet should remain untouched.
await eventRepo.DeleteAsync(new EventId("evt-prune"));
await eventRepo.SaveChangesAsync();
_fixture.DbContext.ChangeTracker.Clear();
var stillThere = await _repo.GetAsync(bet.Id);
stillThere.Should().NotBeNull();
stillThere!.EventId.Value.Should().Be("evt-prune");
}
[Fact]
public async Task DeleteAsync_RemovesBet()
{
var bet = MakeBet();
await _repo.AddAsync(bet);
await _repo.SaveChangesAsync();
_fixture.DbContext.ChangeTracker.Clear();
await _repo.DeleteAsync(bet.Id);
await _repo.SaveChangesAsync();
(await _repo.GetAsync(bet.Id)).Should().BeNull();
}
}