Split API into phase-specific endpoint files

This commit is contained in:
2026-01-28 17:11:25 +01:00
parent 9363b029df
commit 983488d258
8 changed files with 405 additions and 346 deletions

View File

@@ -0,0 +1,56 @@
using GameList.Data;
using GameList.Domain;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace GameList.Endpoints;
internal static class EndpointHelpers
{
public static async Task<Player> GetOrCreatePlayer(HttpContext ctx, AppDbContext db)
{
if (!ctx.Items.TryGetValue(Infrastructure.PlayerIdentityExtensions.PlayerCookieName, out var value) || value is not Guid playerId)
{
throw new InvalidOperationException("Player cookie missing.");
}
var existing = await db.Players.FindAsync(playerId);
if (existing != null) return existing;
var player = new Player { Id = playerId };
db.Players.Add(player);
await db.SaveChangesAsync();
return player;
}
public static async Task<Phase> GetPhase(AppDbContext db)
{
var state = await db.AppState.AsNoTracking().FirstAsync();
return state.CurrentPhase;
}
public static IResult PhaseMismatch(Phase required, Phase current) =>
Results.BadRequest(new { error = $"This endpoint is available in the {required} phase. Current phase is {current}." });
public static string? TrimTo(string? input, int max) =>
string.IsNullOrWhiteSpace(input)
? null
: input.Trim() is var t && t.Length > 0
? t[..Math.Min(t.Length, max)]
: null;
public static bool IsAuthorized(HttpContext ctx, IConfiguration config)
{
var provided = ctx.Request.Headers["X-Admin-Key"].FirstOrDefault()
?? ctx.Request.Query["key"].FirstOrDefault();
var expected = config["ADMIN_PASSWORD"];
return !string.IsNullOrWhiteSpace(expected) && provided == expected;
}
public static AppState NewAppState() => new()
{
Id = 1,
CurrentPhase = Phase.Suggest,
UpdatedAt = DateTimeOffset.UnixEpoch
};
}