Add factory reset admin endpoint and use it in smoke test
This commit is contained in:
@@ -60,6 +60,7 @@ Do not introduce MVC controllers, Razor Pages, Blazor, or SPA frameworks.
|
|||||||
6. Vote phase (blind scoring)
|
6. Vote phase (blind scoring)
|
||||||
7. Results phase (aggregated leaderboard)
|
7. Results phase (aggregated leaderboard)
|
||||||
8. Admin controls (phase switch, reset)
|
8. Admin controls (phase switch, reset)
|
||||||
|
9. Factory reset (clear all data including players) for fresh testing/deploy
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -76,4 +77,4 @@ Do not introduce MVC controllers, Razor Pages, Blazor, or SPA frameworks.
|
|||||||
- Implement API first, UI second
|
- Implement API first, UI second
|
||||||
- Keep changes small and testable
|
- Keep changes small and testable
|
||||||
- Prefer clarity over abstraction
|
- Prefer clarity over abstraction
|
||||||
- After every iteration, do a git commit with a brief summary of the changes as a commit message.
|
- After every iteration, do a git commit with a brief summary of the changes as a commit message.
|
||||||
|
|||||||
1
API.md
1
API.md
@@ -24,3 +24,4 @@ GET /api/results
|
|||||||
## Admin
|
## Admin
|
||||||
POST /api/admin/phase
|
POST /api/admin/phase
|
||||||
POST /api/admin/reset
|
POST /api/admin/reset
|
||||||
|
POST /api/admin/factory-reset
|
||||||
|
|||||||
27
Program.cs
27
Program.cs
@@ -308,6 +308,26 @@ admin.MapPost("/reset", async (HttpContext ctx, AppDbContext db, IConfiguration
|
|||||||
return Results.Ok(new { state.CurrentPhase, state.UpdatedAt });
|
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();
|
app.Run();
|
||||||
|
|
||||||
static async Task<Player> GetOrCreatePlayer(HttpContext ctx, AppDbContext db)
|
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;
|
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 SetNameRequest(string Name);
|
||||||
public record SuggestionRequest(string Name, string? Genre, string? Description, string? ScreenshotUrl, string? YoutubeUrl);
|
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);
|
public record SuggestionDto(int Id, string Name, string? Genre, string? Description, string? ScreenshotUrl, string? YoutubeUrl);
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ function Invoke-Json {
|
|||||||
Write-Host "1) Health check"
|
Write-Host "1) Health check"
|
||||||
Invoke-Json -Method GET -Path "/health" | Out-Host
|
Invoke-Json -Method GET -Path "/health" | Out-Host
|
||||||
|
|
||||||
Write-Host "`n2) Admin reset"
|
Write-Host "`n2) Admin factory reset (clears players, suggestions, votes)"
|
||||||
Invoke-Json -Method POST -Path "/api/admin/reset" -Headers @{ "X-Admin-Key" = $AdminKey } | Out-Host
|
Invoke-Json -Method POST -Path "/api/admin/factory-reset" -Headers @{ "X-Admin-Key" = $AdminKey } | Out-Host
|
||||||
|
|
||||||
Write-Host "`n3) Set player name"
|
Write-Host "`n3) Set player name"
|
||||||
$me = Invoke-Json -Method POST -Path "/api/me/name" -Body @{ name = "SmokeTester" }
|
$me = Invoke-Json -Method POST -Path "/api/me/name" -Body @{ name = "SmokeTester" }
|
||||||
|
|||||||
Reference in New Issue
Block a user