diff --git a/Database/Repository/PuzzleRepository.cs b/Database/Repository/PuzzleRepository.cs index 999d38c..f2c5a54 100644 --- a/Database/Repository/PuzzleRepository.cs +++ b/Database/Repository/PuzzleRepository.cs @@ -7,7 +7,7 @@ public class PuzzleRepository(ConnectionsContext _db) { private readonly ConnectionsContext _db = _db; - public async Task GetPuzzleByDateAsync(string printDate, bool includeSolutions = true) + public async Task GetConnectionsByDateAsync(string printDate, bool includeSolutions = true) { // query for the puzzle var query = _db.ConnectionsPuzzles.AsNoTracking(); @@ -27,12 +27,12 @@ public class PuzzleRepository(ConnectionsContext _db) return null; } - await EnhancePuzzleWithDatesAsync(puzzle); + await EnhanceConnectionsWithDatesAsync(puzzle); return puzzle; } - public async Task> GetAllPuzzlesAsync(bool includeSolutions = true) + public async Task> GetAllConnectionsAsync(bool includeSolutions = true) { // query all, ordered by print date var query = _db.ConnectionsPuzzles @@ -49,13 +49,13 @@ public class PuzzleRepository(ConnectionsContext _db) foreach (var puzzle in result) { - await EnhancePuzzleWithDatesAsync(puzzle); + await EnhanceConnectionsWithDatesAsync(puzzle); } return result; } - private async Task EnhancePuzzleWithDatesAsync(ConnectionsPuzzle puzzle) + private async Task EnhanceConnectionsWithDatesAsync(ConnectionsPuzzle puzzle) { string? previousPuzzleDate = await _db.ConnectionsPuzzles.AsNoTracking() .Where(x => x.PrintDate.CompareTo(puzzle.PrintDate) < 0) diff --git a/Features/Connections/Get/Endpoint.cs b/Features/Connections/Get/Endpoint.cs index 2727e52..bd6edf6 100644 --- a/Features/Connections/Get/Endpoint.cs +++ b/Features/Connections/Get/Endpoint.cs @@ -17,7 +17,7 @@ public class GetConnectionsEndpoint(PuzzleRepository _puzzleRepo) : Endpoint("hideSolutions", isRequired: false); // query for the puzzle - var puzzle = await _puzzleRepo.GetPuzzleByDateAsync(req.PrintDate, includeSolutions: !hideSolutions); + var puzzle = await _puzzleRepo.GetConnectionsByDateAsync(req.PrintDate, includeSolutions: !hideSolutions); // if not found, done here if (puzzle == null) diff --git a/Features/Connections/Query/Endpoint.cs b/Features/Connections/Query/Endpoint.cs new file mode 100644 index 0000000..40b9a98 --- /dev/null +++ b/Features/Connections/Query/Endpoint.cs @@ -0,0 +1,17 @@ +using ConnectionsAPI.Models.Request; +using ConnectionsAPI.Models.Response; + +namespace ConnectionsAPI.Features.Connections.Query; + +public class Endpoint : Endpoint> +{ + public override void Configure() + { + Get("query"); + } + + public override Task HandleAsync(QueryPuzzlesRequest req, CancellationToken ct) + { + return base.HandleAsync(req, ct); + } +} diff --git a/Features/Version/Get/GetVersionEndpoint.cs b/Features/Version/Get/GetVersionEndpoint.cs index 8f65802..b424b19 100644 --- a/Features/Version/Get/GetVersionEndpoint.cs +++ b/Features/Version/Get/GetVersionEndpoint.cs @@ -1,40 +1,38 @@  using System.Text; -namespace ConnectionsAPI.Features.Version.Get +namespace ConnectionsAPI.Features.Version.Get; +public class GetVersionEndpoint : EndpointWithoutRequest { - public class GetVersionEndpoint : EndpointWithoutRequest + private static readonly int START_YEAR = 2024; + + public override void Configure() { - private static readonly int START_YEAR = 2024; + Get("/version"); + AllowAnonymous(); + } - public override void Configure() + public override async Task HandleAsync(CancellationToken ct) + { + StringBuilder responseBuilder = new(); + + responseBuilder.Append("ConnectionsAPI ©"); + if (DateTime.UtcNow.Year != START_YEAR) { - Get("/version"); - AllowAnonymous(); + responseBuilder.AppendFormat("{0} - {1}; ", START_YEAR, DateTime.UtcNow.Year); + } + else + { + responseBuilder.AppendFormat("{0}; ", DateTime.UtcNow.Year); } - public override async Task HandleAsync(CancellationToken ct) - { - StringBuilder responseBuilder = new(); + responseBuilder.Append("by Mate Farkas; "); - responseBuilder.Append("ConnectionsAPI ©"); - if (DateTime.UtcNow.Year != START_YEAR) - { - responseBuilder.AppendFormat("{0} - {1}; ", START_YEAR, DateTime.UtcNow.Year); - } - else - { - responseBuilder.AppendFormat("{0}; ", DateTime.UtcNow.Year); - } + var currentAsm = System.Reflection.Assembly.GetExecutingAssembly(); + string asmVersion = currentAsm.GetName()?.Version?.ToString() ?? string.Empty; - responseBuilder.Append("by Mate Farkas; "); + responseBuilder.AppendFormat("v{0};", asmVersion); - var currentAsm = System.Reflection.Assembly.GetExecutingAssembly(); - string asmVersion = currentAsm.GetName()?.Version?.ToString() ?? string.Empty; - - responseBuilder.AppendFormat("v{0};", asmVersion); - - await SendStringAsync(responseBuilder.ToString(), cancellation: ct); - } + await SendStringAsync(responseBuilder.ToString(), cancellation: ct); } } diff --git a/Models/Request/QueryPuzzlesRequest.cs b/Models/Request/QueryPuzzlesRequest.cs new file mode 100644 index 0000000..881a24b --- /dev/null +++ b/Models/Request/QueryPuzzlesRequest.cs @@ -0,0 +1,7 @@ +namespace ConnectionsAPI.Models.Request; + +public class QueryPuzzlesRequest +{ + [QueryParam] public int Page { get; set; } + [QueryParam] public int Count { get; set; } +} diff --git a/Models/Response/PagedDataResponse.cs b/Models/Response/PagedDataResponse.cs new file mode 100644 index 0000000..9b04ada --- /dev/null +++ b/Models/Response/PagedDataResponse.cs @@ -0,0 +1,8 @@ +namespace ConnectionsAPI.Models.Response; + +public record PagedDataResponse( + int Page, + int Count, + int MaxCount, + ICollection Data +); diff --git a/Validators/QueryPuzzlesRequestValidator.cs b/Validators/QueryPuzzlesRequestValidator.cs new file mode 100644 index 0000000..27fda23 --- /dev/null +++ b/Validators/QueryPuzzlesRequestValidator.cs @@ -0,0 +1,19 @@ +using System; +using ConnectionsAPI.Models.Request; +using FluentValidation; + +namespace ConnectionsAPI.Validators; + +public class QueryPuzzlesRequestValidator : Validator +{ + public QueryPuzzlesRequestValidator() + { + RuleFor(x => x.Page) + .Must(x => x > 0) + .WithMessage("Page number must be a positive integer"); + + RuleFor(x => x.Count) + .Must(x => x > 0) + .WithMessage(x => "Item count must be a positive integer"); + } +}