Skip to content

Commit

Permalink
Added X-Pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
AmielCyber committed Jun 23, 2024
1 parent fb3e390 commit 8cb346c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
18 changes: 9 additions & 9 deletions PetSearch.API.Tests/Handlers/PetHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public class PetHandlerTest
{
private readonly PetsParams _params;
private readonly Mock<IPetFinderClient> _mockPetFinderClient;
private readonly HttpContext _mockHttpContext;
private readonly HttpResponse _mockHttpResponse;
private readonly string _expectedJsonSerializer;

public PetHandlerTest()
{
_params = new PetsParams("dog", "92010");
_mockPetFinderClient = new Mock<IPetFinderClient>();
_mockHttpContext = new DefaultHttpContext();
_mockHttpResponse = new DefaultHttpContext().Response;

var petList = new List<PetDto>();
petList.Add(PetDtoData.GetPetWithAllAttributesInitialized());
Expand All @@ -38,26 +38,26 @@ public PetHandlerTest()
[Fact]
public async Task GetPets_ShouldReturnAn_OkPetListResult()
{
var result = await PetHandler.GetPetsAsync(_params, _mockPetFinderClient.Object, _mockHttpContext);
var result = await PetHandler.GetPetsAsync(_params, _mockPetFinderClient.Object, _mockHttpResponse);

Assert.IsType<Ok<List<PetDto>>>(result.Result);
}

[Fact]
public async Task GetPetsAsync_ShouldSet_XPaginationHeaders()
{
await PetHandler.GetPetsAsync(_params, _mockPetFinderClient.Object, _mockHttpContext);
await PetHandler.GetPetsAsync(_params, _mockPetFinderClient.Object, _mockHttpResponse);

Assert.True(_mockHttpContext.Response.Headers.ContainsKey("X-Pagination"));
_mockHttpContext.Response.Headers.TryGetValue("X-Pagination", out var paginationStringVal);
Assert.True(_mockHttpResponse.Headers.ContainsKey("X-Pagination"));
_mockHttpResponse.Headers.TryGetValue("X-Pagination", out var paginationStringVal);
Assert.Equal(paginationStringVal, _expectedJsonSerializer);
}

[Fact]
public async Task GetSinglePetAsync_ShouldReturnAnOkPetDto()
{
var result = await PetHandler.GetSinglePetAsync(0, _mockPetFinderClient.Object);

Assert.IsType<Ok<PetDto>>(result.Result);
}
}
18 changes: 5 additions & 13 deletions PetSearch.API/Handlers/PetHandler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Net.Mime;
using System.Text.Json;
using ErrorOr;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using PetSearch.API.Clients;
using PetSearch.API.Models;
using PetSearch.API.Helpers;
Expand All @@ -19,20 +17,17 @@ public class PetHandler : BaseHandler
/// </remarks>
/// <param name="petsParams">Pets parameters</param>
/// <param name="petFinderClient">Dependency Injection for IPetFinderClient</param>
/// <param name="httpContext">Dependency Injection to add add response headers</param>
/// <param name="httpResponse">Dependency Injection to add add response headers</param>
/// <returns>
/// Returns a list available pets and the pagination object if successful, else returns a
/// problem detail.
/// </returns>
/// <response code="200">Returns the pet list from the search query and its pagination metadata.</response>
/// <response code="400">If search parameters contains invalid values.</response>
[ProducesResponseType<HttpValidationProblemDetails>(StatusCodes.Status400BadRequest,
MediaTypeNames.Application.ProblemJson)]
[Tags("pets")]
public static async Task<Results<Ok<List<PetDto>>, ProblemHttpResult>> GetPetsAsync(
[AsParameters] PetsParams petsParams,
IPetFinderClient petFinderClient,
HttpContext httpContext
HttpResponse httpResponse
)
{
ErrorOr<PagedList<PetDto>> petsResult = await petFinderClient.GetPetsAsync(petsParams);
Expand All @@ -41,7 +36,8 @@ HttpContext httpContext

List<PetDto> petDtoList = petsResult.Value.Items;
PaginationMetaData paginationMetaData = petsResult.Value.Pagination;
httpContext.Response.Headers.Append("X-Pagination", JsonSerializer.Serialize(paginationMetaData));
httpResponse.Headers.Append("X-Pagination", JsonSerializer.Serialize(paginationMetaData));
httpResponse.Headers.CacheControl = ($"private,max-age={TimeSpan.FromMinutes(10).TotalSeconds}");

return TypedResults.Ok(petDtoList);
}
Expand All @@ -56,12 +52,8 @@ HttpContext httpContext
/// <response code="200">Returns the pet object.</response>
/// <response code="400">If id is not an integer.</response>
/// <response code="404">If pet with passed id is not found or has been adopted.</response>
[ProducesResponseType<HttpValidationProblemDetails>(StatusCodes.Status400BadRequest,
MediaTypeNames.Application.ProblemJson)]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound, MediaTypeNames.Application.ProblemJson)]
[Tags("pets")]
public static async Task<Results<Ok<PetDto>, ProblemHttpResult>> GetSinglePetAsync(
[FromRoute] int id,
int id,
IPetFinderClient petFinderClient
)
{
Expand Down
7 changes: 6 additions & 1 deletion PetSearch.API/Models/LocationDto.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using System.ComponentModel.DataAnnotations;

namespace PetSearch.API.Models;

/// <summary>
/// The location DTO object to serve to our client application after a successful MapBox API request.
/// </summary>
/// <param name="Zipcode">A five digit zipcode.</param>
/// <param name="LocationName">The location name in the following format: {City}, {State} {Zipcode}, {Country}</param>
public record LocationDto(string Zipcode, string LocationName);
public record LocationDto(
[property: Required] string Zipcode,
[property: Required] string LocationName
);
13 changes: 12 additions & 1 deletion PetSearch.API/Models/PaginationMetaData.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
using System.Text.Json.Serialization;

namespace PetSearch.API.Models;

public record PaginationMetaData(int CurrentPage, int TotalPages, int PageSize, int TotalCount);
public record PaginationMetaData(
[property: JsonPropertyName("currentPage")]
int CurrentPage,
[property: JsonPropertyName("totalPages")]
int TotalPages,
[property: JsonPropertyName("pageSize")]
int PageSize,
[property: JsonPropertyName("totalCount")]
int TotalCount
);

0 comments on commit 8cb346c

Please sign in to comment.