Files
maraphon-app/src/Marathon.Application/UseCases/RecordPlacedBetUseCase.cs
T
alexei.dolgolyov 1ad896b07e 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>
2026-05-16 17:45:42 +03:00

91 lines
3.7 KiB
C#

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;
}
}