refactor: Refactor Get connections
This commit is contained in:
14
Features/Connections/ConnectionsGroup.cs
Normal file
14
Features/Connections/ConnectionsGroup.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace ConnectionsAPI.Features.Connections;
|
||||
|
||||
public class ConnectionsGroup : Group
|
||||
{
|
||||
public ConnectionsGroup()
|
||||
{
|
||||
Configure("connections", ep =>
|
||||
{
|
||||
ep.AllowAnonymous();
|
||||
});
|
||||
}
|
||||
}
|
||||
35
Features/Connections/Get/Endpoint.cs
Normal file
35
Features/Connections/Get/Endpoint.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using ConnectionsAPI.Database.Repository;
|
||||
using ConnectionsAPI.Models.Request;
|
||||
using ConnectionsAPI.Models.Response;
|
||||
|
||||
namespace ConnectionsAPI.Features.Connections.Get;
|
||||
|
||||
public class GetConnectionsEndpoint(PuzzleRepository _puzzleRepo) : Endpoint<GetPuzzleRequest, ConnectionsPuzzleDTO>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("{PrintDate}");
|
||||
Group<ConnectionsGroup>();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetPuzzleRequest req, CancellationToken ct)
|
||||
{
|
||||
bool hideSolutions = Query<bool>("hideSolutions", isRequired: false);
|
||||
|
||||
// query for the puzzle
|
||||
var puzzle = await _puzzleRepo.GetPuzzleByDateAsync(req.PrintDate, 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);
|
||||
}
|
||||
}
|
||||
@@ -1,67 +1,67 @@
|
||||
using ConnectionsAPI.Database.Repository;
|
||||
using ConnectionsAPI.Models;
|
||||
using FluentValidation;
|
||||
using LazyCache;
|
||||
using System.Text.RegularExpressions;
|
||||
// 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);
|
||||
// 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 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 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 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 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;
|
||||
}
|
||||
// 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);
|
||||
// bool hideSolutions = Query<bool>("hideSolutions", isRequired: false);
|
||||
|
||||
// query for the puzzle
|
||||
var puzzle = await _puzzleRepo.GetPuzzleByDateAsync(req.PuzzleDate, includeSolutions: !hideSolutions);
|
||||
// // 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;
|
||||
}
|
||||
// // if not found, done here
|
||||
// if (puzzle == null)
|
||||
// {
|
||||
// await SendNotFoundAsync(ct);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// get response from cache
|
||||
var response = ConnectionsPuzzleDTO.FromEntity(puzzle);
|
||||
// // get response from cache
|
||||
// var response = ConnectionsPuzzleDTO.FromEntity(puzzle);
|
||||
|
||||
// done
|
||||
await SendAsync(response, cancellation: ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
// // done
|
||||
// await SendAsync(response, cancellation: ct);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -1,34 +1,34 @@
|
||||
using ConnectionsAPI.Database.Repository;
|
||||
using ConnectionsAPI.Models;
|
||||
using LazyCache;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
// 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;
|
||||
// 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 void Configure()
|
||||
// {
|
||||
// Get("/all.json",
|
||||
// "/puzzle/all");
|
||||
// AllowAnonymous();
|
||||
// }
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
bool hideSolutions = Query<bool>("hideSolutions", isRequired: false);
|
||||
// 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);
|
||||
// // query all, ordered by print date
|
||||
// var puzzles = await _puzzleRepo.GetAllPuzzlesAsync(includeSolutions: !hideSolutions);
|
||||
|
||||
// map to response object
|
||||
var response = puzzles.Select(ConnectionsPuzzleDTO.FromEntity).ToList();
|
||||
// // map to response object
|
||||
// var response = puzzles.Select(ConnectionsPuzzleDTO.FromEntity).ToList();
|
||||
|
||||
// done
|
||||
await SendAsync(response, cancellation: ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
// // done
|
||||
// await SendAsync(response, cancellation: ct);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user