Enforce phase for suggestions with joker allowance
This commit is contained in:
29
Infrastructure/PhaseOrJokerFilter.cs
Normal file
29
Infrastructure/PhaseOrJokerFilter.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using GameList.Data;
|
||||
using GameList.Domain;
|
||||
using GameList.Endpoints;
|
||||
|
||||
namespace GameList.Infrastructure;
|
||||
|
||||
/// <summary>
|
||||
/// Allows Suggest phase, or Vote phase when the player has a joker available.
|
||||
/// Used for creating suggestions during Vote with joker.
|
||||
/// </summary>
|
||||
public class PhaseOrJokerFilter : IEndpointFilter
|
||||
{
|
||||
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);
|
||||
var allow = phase == Phase.Suggest || (phase == Phase.Vote && player.HasJoker);
|
||||
if (!allow)
|
||||
{
|
||||
return EndpointHelpers.PhaseMismatch(Phase.Suggest, phase);
|
||||
}
|
||||
|
||||
return await next(context);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user