Add factory reset admin endpoint and use it in smoke test

This commit is contained in:
2026-01-28 15:02:24 +01:00
parent 37c3eed552
commit 44514f8ecc
4 changed files with 32 additions and 3 deletions

View File

@@ -60,6 +60,7 @@ Do not introduce MVC controllers, Razor Pages, Blazor, or SPA frameworks.
6. Vote phase (blind scoring)
7. Results phase (aggregated leaderboard)
8. Admin controls (phase switch, reset)
9. Factory reset (clear all data including players) for fresh testing/deploy
---

1
API.md
View File

@@ -24,3 +24,4 @@ GET /api/results
## Admin
POST /api/admin/phase
POST /api/admin/reset
POST /api/admin/factory-reset

View File

@@ -308,6 +308,26 @@ admin.MapPost("/reset", async (HttpContext ctx, AppDbContext db, IConfiguration
return Results.Ok(new { state.CurrentPhase, state.UpdatedAt });
});
admin.MapPost("/factory-reset", async (HttpContext ctx, AppDbContext db, IConfiguration config) =>
{
if (!IsAuthorized(ctx, config)) return Results.Unauthorized();
await using var tx = await db.Database.BeginTransactionAsync();
await db.Votes.ExecuteDeleteAsync();
await db.Suggestions.ExecuteDeleteAsync();
await db.Players.ExecuteDeleteAsync();
await db.AppState.ExecuteDeleteAsync();
var fresh = NewAppState();
db.AppState.Add(fresh);
await db.SaveChangesAsync();
await tx.CommitAsync();
return Results.Ok(new { fresh.CurrentPhase, fresh.UpdatedAt });
});
app.Run();
static async Task<Player> GetOrCreatePlayer(HttpContext ctx, AppDbContext db)
@@ -350,6 +370,13 @@ static bool IsAuthorized(HttpContext ctx, IConfiguration config)
return !string.IsNullOrWhiteSpace(expected) && provided == expected;
}
static AppState NewAppState() => new()
{
Id = 1,
CurrentPhase = Phase.Suggest,
UpdatedAt = DateTimeOffset.UnixEpoch
};
public record SetNameRequest(string Name);
public record SuggestionRequest(string Name, string? Genre, string? Description, string? ScreenshotUrl, string? YoutubeUrl);
public record SuggestionDto(int Id, string Name, string? Genre, string? Description, string? ScreenshotUrl, string? YoutubeUrl);

View File

@@ -42,8 +42,8 @@ function Invoke-Json {
Write-Host "1) Health check"
Invoke-Json -Method GET -Path "/health" | Out-Host
Write-Host "`n2) Admin reset"
Invoke-Json -Method POST -Path "/api/admin/reset" -Headers @{ "X-Admin-Key" = $AdminKey } | Out-Host
Write-Host "`n2) Admin factory reset (clears players, suggestions, votes)"
Invoke-Json -Method POST -Path "/api/admin/factory-reset" -Headers @{ "X-Admin-Key" = $AdminKey } | Out-Host
Write-Host "`n3) Set player name"
$me = Invoke-Json -Method POST -Path "/api/me/name" -Body @{ name = "SmokeTester" }