Files
maraphon-app/src/Marathon.Infrastructure/Persistence/Repositories/SnapshotRepository.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

109 lines
3.7 KiB
C#

using Marathon.Application.Abstractions;
using Marathon.Domain.Entities;
using Marathon.Domain.ValueObjects;
using Microsoft.EntityFrameworkCore;
namespace Marathon.Infrastructure.Persistence.Repositories;
internal sealed class SnapshotRepository : ISnapshotRepository
{
private readonly MarathonDbContext _db;
public SnapshotRepository(MarathonDbContext db) => _db = db;
public async Task<IReadOnlyList<OddsSnapshot>> ListAsync(CancellationToken ct = default)
{
var entities = await _db.Snapshots.AsNoTracking()
.Include(s => s.Bets)
.ToListAsync(ct);
return entities.Select(Mapping.ToDomain).ToList().AsReadOnly();
}
public async Task<IReadOnlyList<OddsSnapshot>> ListByEventAsync(
EventId eventId,
DateTimeOffset from,
DateTimeOffset to,
CancellationToken ct = default)
{
var fromStr = from.ToString("O");
var toStr = to.ToString("O");
var entities = await _db.Snapshots.AsNoTracking()
.Include(s => s.Bets)
.Where(s => s.EventCode == eventId.Value
&& s.CapturedAt.CompareTo(fromStr) >= 0
&& s.CapturedAt.CompareTo(toStr) <= 0)
.ToListAsync(ct);
return entities.Select(Mapping.ToDomain).ToList().AsReadOnly();
}
public async Task<IReadOnlyDictionary<EventId, IReadOnlyList<OddsSnapshot>>> ListByEventsAsync(
IReadOnlyCollection<EventId> eventIds,
DateTimeOffset from,
DateTimeOffset to,
CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(eventIds);
var result = new Dictionary<EventId, IReadOnlyList<OddsSnapshot>>(eventIds.Count);
if (eventIds.Count == 0)
return result;
var ids = eventIds.Select(e => e.Value).Distinct().ToArray();
var fromStr = from.ToString("O");
var toStr = to.ToString("O");
var entities = await _db.Snapshots.AsNoTracking()
.Include(s => s.Bets)
.Where(s => ids.Contains(s.EventCode)
&& s.CapturedAt.CompareTo(fromStr) >= 0
&& s.CapturedAt.CompareTo(toStr) <= 0)
.ToListAsync(ct);
var grouped = entities
.GroupBy(e => e.EventCode)
.ToDictionary(g => g.Key, g => g.Select(Mapping.ToDomain).ToList());
foreach (var id in eventIds)
{
result[id] = grouped.TryGetValue(id.Value, out var list)
? list.AsReadOnly()
: Array.Empty<OddsSnapshot>();
}
return result;
}
public async Task AddAsync(OddsSnapshot entity, CancellationToken ct = default)
{
var efEntity = Mapping.ToEntity(entity);
await _db.Snapshots.AddAsync(efEntity, ct);
}
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);
}
}