Rework admin player table and account deletion
This commit is contained in:
@@ -42,9 +42,13 @@ public static class AdminEndpoints
|
||||
|
||||
var voters = await db.Players
|
||||
.AsNoTracking()
|
||||
.Where(p => p.CurrentPhase == Phase.Vote || p.Suggestions.Any())
|
||||
.OrderBy(p => p.DisplayName ?? p.Username)
|
||||
.Select(p => new VoteStatusDto(p.Id, p.DisplayName ?? p.Username, p.VotesFinal, p.HasJoker))
|
||||
.Select(p => new VoteStatusDto(p.Id,
|
||||
p.DisplayName ?? p.Username,
|
||||
p.Username,
|
||||
p.CurrentPhase,
|
||||
p.VotesFinal,
|
||||
p.HasJoker))
|
||||
.ToListAsync();
|
||||
|
||||
var waiting = voters.Where(v => !v.Finalized).Select(v => v.Name).ToList();
|
||||
@@ -69,6 +73,41 @@ public static class AdminEndpoints
|
||||
return Results.Ok(new { player.Id, player.HasJoker });
|
||||
});
|
||||
|
||||
admin.MapDelete("/players/{playerId:guid}", async (Guid playerId, HttpContext ctx, AppDbContext db, IConfiguration config) =>
|
||||
{
|
||||
if (!await EndpointHelpers.IsAdmin(ctx, db, config)) return Results.Unauthorized();
|
||||
|
||||
var player = await db.Players
|
||||
.Include(p => p.Suggestions)
|
||||
.FirstOrDefaultAsync(p => p.Id == playerId);
|
||||
if (player is null) return Results.NotFound(new { error = "Player not found." });
|
||||
|
||||
await using var tx = await db.Database.BeginTransactionAsync();
|
||||
|
||||
// Remove votes cast by the player
|
||||
await db.Votes.Where(v => v.PlayerId == playerId).ExecuteDeleteAsync();
|
||||
|
||||
// Collect suggestions authored by the player
|
||||
var suggestionIds = player.Suggestions.Select(s => s.Id).ToList();
|
||||
if (suggestionIds.Count > 0)
|
||||
{
|
||||
// Break links pointing to these suggestions
|
||||
await db.Suggestions
|
||||
.Where(s => s.ParentSuggestionId != null && suggestionIds.Contains(s.ParentSuggestionId.Value))
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(x => x.ParentSuggestionId, (int?)null));
|
||||
|
||||
// Remove votes for these suggestions to avoid orphaned rows
|
||||
await db.Votes.Where(v => suggestionIds.Contains(v.SuggestionId)).ExecuteDeleteAsync();
|
||||
}
|
||||
|
||||
// Delete player (cascades suggestions)
|
||||
db.Players.Remove(player);
|
||||
await db.SaveChangesAsync();
|
||||
await tx.CommitAsync();
|
||||
|
||||
return Results.Ok(new { DeletedPlayerId = playerId });
|
||||
});
|
||||
|
||||
admin.MapPost("/link-suggestions", async ([FromBody] LinkSuggestionsRequest request, HttpContext ctx, AppDbContext db, IConfiguration config) =>
|
||||
{
|
||||
var player = await EndpointHelpers.GetAuthenticatedPlayer(ctx, db);
|
||||
|
||||
Reference in New Issue
Block a user