Add phase requirement filter for vote/results endpoints
This commit is contained in:
31
Infrastructure/PhaseRequirementFilter.cs
Normal file
31
Infrastructure/PhaseRequirementFilter.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using GameList.Data;
|
||||
using GameList.Domain;
|
||||
using GameList.Endpoints;
|
||||
|
||||
namespace GameList.Infrastructure;
|
||||
|
||||
public class PhaseRequirementFilter : IEndpointFilter
|
||||
{
|
||||
private readonly Phase _required;
|
||||
|
||||
public PhaseRequirementFilter(Phase required)
|
||||
{
|
||||
_required = required;
|
||||
}
|
||||
|
||||
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
|
||||
{
|
||||
var httpContext = context.HttpContext;
|
||||
var db = httpContext.RequestServices.GetRequiredService<AppDbContext>();
|
||||
var player = await EndpointHelpers.GetAuthenticatedPlayer(httpContext, db);
|
||||
if (player is null) return Results.Unauthorized();
|
||||
|
||||
var phase = await EndpointHelpers.GetPhase(db, player.Id);
|
||||
if (phase != _required)
|
||||
{
|
||||
return EndpointHelpers.PhaseMismatch(_required, phase);
|
||||
}
|
||||
|
||||
return await next(context);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user