Add factory reset admin endpoint and use it in smoke test
This commit is contained in:
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 });
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user