diff --git a/PetSearch.API.Tests/Handlers/PetHandlerTest.cs b/PetSearch.API.Tests/Handlers/PetHandlerTest.cs index 053797f..2119b76 100644 --- a/PetSearch.API.Tests/Handlers/PetHandlerTest.cs +++ b/PetSearch.API.Tests/Handlers/PetHandlerTest.cs @@ -16,14 +16,14 @@ public class PetHandlerTest { private readonly PetsParams _params; private readonly Mock _mockPetFinderClient; - private readonly HttpContext _mockHttpContext; + private readonly HttpResponse _mockHttpResponse; private readonly string _expectedJsonSerializer; public PetHandlerTest() { _params = new PetsParams("dog", "92010"); _mockPetFinderClient = new Mock(); - _mockHttpContext = new DefaultHttpContext(); + _mockHttpResponse = new DefaultHttpContext().Response; var petList = new List(); petList.Add(PetDtoData.GetPetWithAllAttributesInitialized()); @@ -38,18 +38,18 @@ 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>>(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); } @@ -57,7 +57,7 @@ public async Task GetPetsAsync_ShouldSet_XPaginationHeaders() public async Task GetSinglePetAsync_ShouldReturnAnOkPetDto() { var result = await PetHandler.GetSinglePetAsync(0, _mockPetFinderClient.Object); - + Assert.IsType>(result.Result); } } \ No newline at end of file diff --git a/PetSearch.API/Handlers/PetHandler.cs b/PetSearch.API/Handlers/PetHandler.cs index c0a1090..42292e9 100644 --- a/PetSearch.API/Handlers/PetHandler.cs +++ b/PetSearch.API/Handlers/PetHandler.cs @@ -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; @@ -19,20 +17,17 @@ public class PetHandler : BaseHandler /// /// Pets parameters /// Dependency Injection for IPetFinderClient - /// Dependency Injection to add add response headers + /// Dependency Injection to add add response headers /// /// Returns a list available pets and the pagination object if successful, else returns a /// problem detail. /// /// Returns the pet list from the search query and its pagination metadata. /// If search parameters contains invalid values. - [ProducesResponseType(StatusCodes.Status400BadRequest, - MediaTypeNames.Application.ProblemJson)] - [Tags("pets")] public static async Task>, ProblemHttpResult>> GetPetsAsync( [AsParameters] PetsParams petsParams, IPetFinderClient petFinderClient, - HttpContext httpContext + HttpResponse httpResponse ) { ErrorOr> petsResult = await petFinderClient.GetPetsAsync(petsParams); @@ -41,7 +36,8 @@ HttpContext httpContext List 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); } @@ -56,12 +52,8 @@ HttpContext httpContext /// Returns the pet object. /// If id is not an integer. /// If pet with passed id is not found or has been adopted. - [ProducesResponseType(StatusCodes.Status400BadRequest, - MediaTypeNames.Application.ProblemJson)] - [ProducesResponseType(StatusCodes.Status404NotFound, MediaTypeNames.Application.ProblemJson)] - [Tags("pets")] public static async Task, ProblemHttpResult>> GetSinglePetAsync( - [FromRoute] int id, + int id, IPetFinderClient petFinderClient ) { diff --git a/PetSearch.API/Models/LocationDto.cs b/PetSearch.API/Models/LocationDto.cs index 2709775..b1d5f15 100644 --- a/PetSearch.API/Models/LocationDto.cs +++ b/PetSearch.API/Models/LocationDto.cs @@ -1,3 +1,5 @@ +using System.ComponentModel.DataAnnotations; + namespace PetSearch.API.Models; /// @@ -5,4 +7,7 @@ namespace PetSearch.API.Models; /// /// A five digit zipcode. /// The location name in the following format: {City}, {State} {Zipcode}, {Country} -public record LocationDto(string Zipcode, string LocationName); \ No newline at end of file +public record LocationDto( + [property: Required] string Zipcode, + [property: Required] string LocationName +); \ No newline at end of file diff --git a/PetSearch.API/Models/PaginationMetaData.cs b/PetSearch.API/Models/PaginationMetaData.cs index 2cfd284..7c8e5c7 100644 --- a/PetSearch.API/Models/PaginationMetaData.cs +++ b/PetSearch.API/Models/PaginationMetaData.cs @@ -1,3 +1,14 @@ +using System.Text.Json.Serialization; + namespace PetSearch.API.Models; -public record PaginationMetaData(int CurrentPage, int TotalPages, int PageSize, int TotalCount); \ No newline at end of file +public record PaginationMetaData( + [property: JsonPropertyName("currentPage")] + int CurrentPage, + [property: JsonPropertyName("totalPages")] + int TotalPages, + [property: JsonPropertyName("pageSize")] + int PageSize, + [property: JsonPropertyName("totalCount")] + int TotalCount +); \ No newline at end of file