Split API into phase-specific endpoint files
This commit is contained in:
45
Endpoints/StateEndpoints.cs
Normal file
45
Endpoints/StateEndpoints.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using GameList.Contracts;
|
||||
using GameList.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace GameList.Endpoints;
|
||||
|
||||
public static class StateEndpoints
|
||||
{
|
||||
public static void MapStateEndpoints(this IEndpointRouteBuilder app)
|
||||
{
|
||||
app.MapGet("/api/state", async (AppDbContext db) =>
|
||||
{
|
||||
var state = await db.AppState.AsNoTracking().FirstAsync();
|
||||
var summary = new
|
||||
{
|
||||
state.CurrentPhase,
|
||||
state.UpdatedAt,
|
||||
Players = await db.Players.CountAsync(),
|
||||
Suggestions = await db.Suggestions.CountAsync(),
|
||||
Votes = await db.Votes.CountAsync()
|
||||
};
|
||||
return Results.Ok(summary);
|
||||
});
|
||||
|
||||
app.MapGet("/api/me", async (HttpContext ctx, AppDbContext db) =>
|
||||
{
|
||||
var player = await EndpointHelpers.GetOrCreatePlayer(ctx, db);
|
||||
return Results.Ok(new { player.Id, player.DisplayName });
|
||||
});
|
||||
|
||||
app.MapPost("/api/me/name", async ([FromBody] SetNameRequest request, HttpContext ctx, AppDbContext db) =>
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(request.Name) || request.Name.Length > 64)
|
||||
{
|
||||
return Results.BadRequest(new { error = "Name is required and must be <= 64 characters." });
|
||||
}
|
||||
|
||||
var player = await EndpointHelpers.GetOrCreatePlayer(ctx, db);
|
||||
player.DisplayName = request.Name.Trim();
|
||||
await db.SaveChangesAsync();
|
||||
return Results.Ok(new { player.Id, player.DisplayName });
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user