using GameList.Data; using GameList.Domain; using Microsoft.EntityFrameworkCore; namespace GameList.Endpoints; public static class ResultsEndpoints { public static void MapResultsEndpoints(this IEndpointRouteBuilder app) { app.MapGet("/api/results", async (AppDbContext db) => { var phase = await EndpointHelpers.GetPhase(db); if (phase != Phase.Results) return EndpointHelpers.PhaseMismatch(Phase.Results, phase); var results = await db.Suggestions.AsNoTracking() .Include(s => s.Player) .Include(s => s.Votes) .Select(s => new { s.Id, s.Name, Author = s.Player!.DisplayName, Total = s.Votes.Sum(v => v.Score), Count = s.Votes.Count, Average = s.Votes.Count == 0 ? 0 : s.Votes.Average(v => v.Score), s.ScreenshotUrl, s.YoutubeUrl, s.Description, s.Genre }) .OrderByDescending(r => r.Total) .ToListAsync(); return Results.Ok(results); }); } }