feat(bp): repositories + identity generation for runtime-inserted tables
Add ValueGeneratedOnAdd to ViewerBattlePassProgress.Id and ViewerBattlePassClaims.Id so Postgres generates IDENTITY values at runtime. Regenerate AddBattlePass migration in-place to include the IdentityByDefaultColumn annotations. Add IBattlePassRepository / BattlePassRepository (season lookup + level-curve cache) and IViewerBattlePassRepository / ViewerBattlePassRepository (get-or-create progress, claim reads/writes). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SVSim.Database.Models;
|
||||
|
||||
namespace SVSim.Database.Repositories.BattlePass;
|
||||
|
||||
public sealed class BattlePassRepository : IBattlePassRepository
|
||||
{
|
||||
private readonly SVSimDbContext _db;
|
||||
|
||||
// Process-level cache for the immutable level curve. Bootstrap re-baseline = host restart = cache cleared.
|
||||
private static IReadOnlyList<BattlePassLevelEntry>? _curveCache;
|
||||
private static readonly SemaphoreSlim _curveCacheLock = new(1, 1);
|
||||
|
||||
public BattlePassRepository(SVSimDbContext db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
public async Task<BattlePassSeasonEntry?> GetActiveSeasonAsync(DateTimeOffset when, CancellationToken ct)
|
||||
{
|
||||
return await _db.BattlePassSeasons
|
||||
.AsNoTracking()
|
||||
.Where(s => s.StartDate <= when && s.EndDate > when)
|
||||
.OrderByDescending(s => s.StartDate)
|
||||
.FirstOrDefaultAsync(ct);
|
||||
}
|
||||
|
||||
public Task<BattlePassSeasonEntry?> GetSeasonAsync(int seasonId, CancellationToken ct) =>
|
||||
_db.BattlePassSeasons.AsNoTracking().FirstOrDefaultAsync(s => s.Id == seasonId, ct);
|
||||
|
||||
public async Task<List<BattlePassRewardEntry>> GetSeasonRewardsAsync(int seasonId, CancellationToken ct) =>
|
||||
await _db.BattlePassRewards.AsNoTracking()
|
||||
.Where(r => r.SeasonId == seasonId)
|
||||
.OrderBy(r => r.Track).ThenBy(r => r.Level)
|
||||
.ToListAsync(ct);
|
||||
|
||||
public async Task<IReadOnlyList<BattlePassLevelEntry>> GetLevelCurveAsync(CancellationToken ct)
|
||||
{
|
||||
if (_curveCache is not null) return _curveCache;
|
||||
await _curveCacheLock.WaitAsync(ct);
|
||||
try
|
||||
{
|
||||
if (_curveCache is null)
|
||||
{
|
||||
_curveCache = await _db.BattlePassLevels.AsNoTracking()
|
||||
.OrderBy(e => e.Level)
|
||||
.ToListAsync(ct);
|
||||
}
|
||||
return _curveCache;
|
||||
}
|
||||
finally { _curveCacheLock.Release(); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using SVSim.Database.Models;
|
||||
|
||||
namespace SVSim.Database.Repositories.BattlePass;
|
||||
|
||||
public interface IBattlePassRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// Active season for the given moment (StartDate <= when < EndDate). Returns null
|
||||
/// if none. If multiple match (overlap), the most recently started wins.
|
||||
/// </summary>
|
||||
Task<BattlePassSeasonEntry?> GetActiveSeasonAsync(DateTimeOffset when, CancellationToken ct);
|
||||
|
||||
/// <summary>
|
||||
/// Season by id (no time-window filter). Used by /battle_pass/buy to validate request.season_id.
|
||||
/// </summary>
|
||||
Task<BattlePassSeasonEntry?> GetSeasonAsync(int seasonId, CancellationToken ct);
|
||||
|
||||
/// <summary>
|
||||
/// All rewards for a season, both tracks. Sorted by (Track, Level) for deterministic wire order.
|
||||
/// </summary>
|
||||
Task<List<BattlePassRewardEntry>> GetSeasonRewardsAsync(int seasonId, CancellationToken ct);
|
||||
|
||||
/// <summary>
|
||||
/// Global level curve. Cached after first load.
|
||||
/// </summary>
|
||||
Task<IReadOnlyList<BattlePassLevelEntry>> GetLevelCurveAsync(CancellationToken ct);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using SVSim.Database.Enums;
|
||||
using SVSim.Database.Models;
|
||||
|
||||
namespace SVSim.Database.Repositories.BattlePass;
|
||||
|
||||
public interface IViewerBattlePassRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// Get-or-create progress row for (viewer, season). New rows are added to the change-tracker
|
||||
/// but NOT saved — caller batches with other mutations.
|
||||
/// </summary>
|
||||
Task<ViewerBattlePassProgressEntry> GetOrCreateProgressAsync(long viewerId, int seasonId, CancellationToken ct);
|
||||
|
||||
/// <summary>
|
||||
/// All claim rows for (viewer, season). Used by /battle_pass/info to enrich is_received.
|
||||
/// </summary>
|
||||
Task<List<ViewerBattlePassClaimEntry>> GetClaimsAsync(long viewerId, int seasonId, CancellationToken ct);
|
||||
|
||||
/// <summary>
|
||||
/// Append a claim row (in-memory; caller saves).
|
||||
/// </summary>
|
||||
void AddClaim(long viewerId, int seasonId, BattlePassTrack track, int level, DateTimeOffset claimedAt);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SVSim.Database.Enums;
|
||||
using SVSim.Database.Models;
|
||||
|
||||
namespace SVSim.Database.Repositories.BattlePass;
|
||||
|
||||
public sealed class ViewerBattlePassRepository : IViewerBattlePassRepository
|
||||
{
|
||||
private readonly SVSimDbContext _db;
|
||||
|
||||
public ViewerBattlePassRepository(SVSimDbContext db) { _db = db; }
|
||||
|
||||
public async Task<ViewerBattlePassProgressEntry> GetOrCreateProgressAsync(long viewerId, int seasonId, CancellationToken ct)
|
||||
{
|
||||
var existing = await _db.ViewerBattlePassProgress
|
||||
.FirstOrDefaultAsync(p => p.ViewerId == viewerId && p.SeasonId == seasonId, ct);
|
||||
if (existing is not null) return existing;
|
||||
|
||||
var entry = new ViewerBattlePassProgressEntry
|
||||
{
|
||||
ViewerId = viewerId,
|
||||
SeasonId = seasonId,
|
||||
CurrentPoint = 0,
|
||||
IsPremium = false,
|
||||
WeeklyPoints = 0,
|
||||
WeeklyPeriodStart = null,
|
||||
};
|
||||
_db.ViewerBattlePassProgress.Add(entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
public Task<List<ViewerBattlePassClaimEntry>> GetClaimsAsync(long viewerId, int seasonId, CancellationToken ct) =>
|
||||
_db.ViewerBattlePassClaims.AsNoTracking()
|
||||
.Where(c => c.ViewerId == viewerId && c.SeasonId == seasonId)
|
||||
.ToListAsync(ct);
|
||||
|
||||
public void AddClaim(long viewerId, int seasonId, BattlePassTrack track, int level, DateTimeOffset claimedAt)
|
||||
{
|
||||
_db.ViewerBattlePassClaims.Add(new ViewerBattlePassClaimEntry
|
||||
{
|
||||
ViewerId = viewerId, SeasonId = seasonId, Track = track,
|
||||
Level = level, ClaimedAt = claimedAt,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user