Files
maraphon-app/src/Marathon.Infrastructure/Persistence/Configurations/SavedStrategyConfiguration.cs
T
alexei.dolgolyov 2a0ea7b3a6 feat(backtest): saved strategy presets (strategy editor v1)
Persist named backtest-strategy presets so a staking config (bankroll,
min-score, stake rule, flat/percent/Kelly params) can be saved, listed,
loaded back into the form, and deleted. The per-run date range is not
part of a preset.

- Domain: SavedStrategy record (name trimmed + bounded to 80 chars,
  Create() factory) wrapping the pure BacktestStrategy.
- Persistence: SavedStrategyEntity + config (TEXT decimals, unique
  case-insensitive NOCASE index on Name), repository, mapping, and a
  hand-trimmed AddSavedStrategies migration (additive — only the new
  table). Case-insensitive names mean save-by-name overwrites instead of
  creating near-duplicates.
- Application: SaveStrategyUseCase (upsert by name, keeps Id+CreatedAt) +
  DeleteStrategyUseCase.
- UI: presets panel on the Backtest page (load/save/delete) + service
  methods; fraction<->percent round-trip; en/ru resx.
- Fix: pin Sports.Code as ValueGeneratedNever — it is the bookmaker's
  natural sport id, not an autoincrement surrogate. Corrects long-standing
  model-snapshot drift; the snapshot is regenerated to match the DB.
- 25 tests across all four layers: domain validation, real-SQLite
  round-trip incl. case-insensitive lookup/uniqueness, the upsert use
  case, and the service percent mapping.
2026-05-29 02:13:16 +03:00

36 lines
1.8 KiB
C#

using Marathon.Infrastructure.Persistence.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Marathon.Infrastructure.Persistence.Configurations;
internal sealed class SavedStrategyConfiguration : IEntityTypeConfiguration<SavedStrategyEntity>
{
public void Configure(EntityTypeBuilder<SavedStrategyEntity> builder)
{
builder.ToTable("SavedStrategies");
builder.HasKey(s => s.Id);
builder.Property(s => s.Id).HasColumnType("TEXT").IsRequired();
// NOCASE so the unique index and the GetByNameAsync lookup both treat names
// case-insensitively (ASCII) — "Kelly" and "kelly" are the same preset, and
// save-by-name overwrites rather than creating a near-duplicate.
builder.Property(s => s.Name).HasColumnType("TEXT").UseCollation("NOCASE").IsRequired();
builder.Property(s => s.StartingBankroll).HasColumnType("TEXT").IsRequired();
builder.Property(s => s.MinScore).HasColumnType("TEXT").IsRequired();
builder.Property(s => s.StakeRule).HasColumnType("INTEGER").IsRequired();
builder.Property(s => s.FlatStake).HasColumnType("TEXT").IsRequired();
builder.Property(s => s.PercentOfBankroll).HasColumnType("TEXT").IsRequired();
builder.Property(s => s.KellyFraction).HasColumnType("TEXT").IsRequired();
builder.Property(s => s.CreatedAt).HasColumnType("TEXT").IsRequired();
// Names are the user-facing identity for save/overwrite, so they must be
// unique — the SaveStrategyUseCase upserts by name and the index backstops
// any race that would otherwise create a duplicate.
builder.HasIndex(s => s.Name)
.IsUnique()
.HasDatabaseName("IX_SavedStrategies_Name");
}
}