feat: Implement Query endpoint for Connections puzzles

This commit is contained in:
2024-12-27 09:45:53 +01:00
parent 82733693f1
commit 70ee4e076e
7 changed files with 66 additions and 118 deletions

View File

@@ -1,17 +1,29 @@
using ConnectionsAPI.Database.Repository;
using ConnectionsAPI.Models.Request;
using ConnectionsAPI.Models.Response;
namespace ConnectionsAPI.Features.Connections.Query;
public class Endpoint : Endpoint<QueryPuzzlesRequest, PagedDataResponse<ConnectionsPuzzleDTO>>
public class Endpoint(PuzzleRepository _puzzleRepository) : Endpoint<QueryPuzzlesRequest, PagedDataResponse<ConnectionsPuzzleDTO>>
{
public override void Configure()
{
Get("query");
Group<ConnectionsGroup>();
}
public override Task HandleAsync(QueryPuzzlesRequest req, CancellationToken ct)
public override async Task HandleAsync(QueryPuzzlesRequest req, CancellationToken ct)
{
return base.HandleAsync(req, ct);
bool hideSolutions = Query<bool>("hideSolutions", isRequired: false);
var puzzles = await _puzzleRepository.QueryConnectionsPuzzles(req.Page,
req.Count,
req.Year,
req.Month,
!hideSolutions);
PagedDataResponse<ConnectionsPuzzleDTO> response = new(puzzles.Page, puzzles.Count, puzzles.MaxCount, puzzles.Data.Select(ConnectionsPuzzleDTO.FromEntity).ToList());
await SendAsync(response, cancellation: ct);
}
}

View File

@@ -1,67 +0,0 @@
// using ConnectionsAPI.Database.Repository;
// using ConnectionsAPI.Models;
// using FluentValidation;
// using LazyCache;
// using System.Text.RegularExpressions;
// namespace ConnectionsAPI.Features.Puzzle.Get
// {
// public record GetPuzzleEndpointRequest(string PuzzleDate);
// public partial class GetPuzzleEndpointRequestValidator : Validator<GetPuzzleEndpointRequest>
// {
// [GeneratedRegex("^\\d{4}-\\d{2}-\\d{2}$", RegexOptions.IgnoreCase)]
// private static partial Regex PrintDateGeneratedRegex();
// public GetPuzzleEndpointRequestValidator()
// {
// RuleFor(x => x.PuzzleDate)
// .NotEmpty().WithMessage("Puzzle date is required")
// .Must(x => PrintDateGeneratedRegex().IsMatch(x)).WithMessage("Puzzle date must be in the format yyyy-MM-dd");
// }
// }
// public class GetPuzzleEndpoint(PuzzleRepository puzzleRepo, ILogger<GetPuzzleEndpoint> logger, IAppCache cache) : Endpoint<GetPuzzleEndpointRequest, ConnectionsPuzzleDTO>
// {
// private readonly PuzzleRepository _puzzleRepo = puzzleRepo;
// private readonly ILogger<GetPuzzleEndpoint> _logger = logger;
// private readonly IAppCache _cache = cache;
// public override void Configure()
// {
// Get("/{PuzzleDate}.json",
// "/puzzle/{PuzzleDate}");
// AllowAnonymous();
// DontThrowIfValidationFails();
// }
// public override async Task HandleAsync(GetPuzzleEndpointRequest req, CancellationToken ct)
// {
// // default to 404 if validation fails
// if (ValidationFailed)
// {
// _logger.LogError("Validation error. {path} {pathBase}", HttpContext.Request.Path, HttpContext.Request.PathBase);
// await SendNotFoundAsync(ct);
// return;
// }
// bool hideSolutions = Query<bool>("hideSolutions", isRequired: false);
// // query for the puzzle
// var puzzle = await _puzzleRepo.GetPuzzleByDateAsync(req.PuzzleDate, includeSolutions: !hideSolutions);
// // if not found, done here
// if (puzzle == null)
// {
// await SendNotFoundAsync(ct);
// return;
// }
// // get response from cache
// var response = ConnectionsPuzzleDTO.FromEntity(puzzle);
// // done
// await SendAsync(response, cancellation: ct);
// }
// }
// }

View File

@@ -1,34 +0,0 @@
// using ConnectionsAPI.Database.Repository;
// using ConnectionsAPI.Models;
// using LazyCache;
// using Microsoft.EntityFrameworkCore;
// namespace ConnectionsAPI.Features.Puzzle.List
// {
// public class ListPuzzlesEndpoint(PuzzleRepository puzzleRepo, IAppCache cache) : EndpointWithoutRequest<ICollection<ConnectionsPuzzleDTO>>
// {
// private readonly PuzzleRepository _puzzleRepo = puzzleRepo;
// private readonly IAppCache _cache = cache;
// public override void Configure()
// {
// Get("/all.json",
// "/puzzle/all");
// AllowAnonymous();
// }
// public override async Task HandleAsync(CancellationToken ct)
// {
// bool hideSolutions = Query<bool>("hideSolutions", isRequired: false);
// // query all, ordered by print date
// var puzzles = await _puzzleRepo.GetAllPuzzlesAsync(includeSolutions: !hideSolutions);
// // map to response object
// var response = puzzles.Select(ConnectionsPuzzleDTO.FromEntity).ToList();
// // done
// await SendAsync(response, cancellation: ct);
// }
// }
// }