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:
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user