From bf7490e0f69891d15c46765669428d82f53e93d3 Mon Sep 17 00:00:00 2001 From: Adam Monikowski Date: Wed, 19 Jun 2024 12:40:54 +0200 Subject: [PATCH] Add TotalAds property to GetBrowseAdsPageResponse --- .../Browse/BrowseServiceGetBrowseAdsPageTests.cs | 7 ++++--- .../DTOs/Responses/GetBrowseAdsPageResponse.cs | 1 + TutorLizard.BusinessLogic/Services/BrowseService.cs | 13 +++++++------ TutorLizard.Web/Views/Browse/Ads.cshtml | 6 ++++++ 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Tests/TutorLizard.BusinessLogic.Tests/Services/Browse/BrowseServiceGetBrowseAdsPageTests.cs b/Tests/TutorLizard.BusinessLogic.Tests/Services/Browse/BrowseServiceGetBrowseAdsPageTests.cs index fad3d510..1900d0cf 100644 --- a/Tests/TutorLizard.BusinessLogic.Tests/Services/Browse/BrowseServiceGetBrowseAdsPageTests.cs +++ b/Tests/TutorLizard.BusinessLogic.Tests/Services/Browse/BrowseServiceGetBrowseAdsPageTests.cs @@ -81,20 +81,22 @@ public async Task GetBrowseAdsPage_WhenRequestIsValid_ShouldReturnCorrectNumberO [InlineData(1, 1, 0, 1)] [InlineData(1, 10, 101, 11)] [InlineData(12, 10, 101, 11)] - public async Task GetBrowseAdsPage_WhenRequestIsValid_ShouldReturnCorrectTotalPages(int pageNumber, int pageSize, int adCount, int expectedTotalPages) + public async Task GetBrowseAdsPage_WhenRequestIsValid_ShouldReturnCorrectTotals(int pageNumber, int pageSize, int expectedTotalAds, int expectedTotalPages) { // Arrange - var ads = CreateTestAds(adCount); + var ads = CreateTestAds(expectedTotalAds); SetupMockGetAllAds(ads); GetBrowseAdsPageRequest request = new(pageNumber, pageSize); // Act var response = await BrowseService.GetBrowseAdsPage(request); + int actualTotalAds = response.TotalAds; int actualTotalPages = response.TotalPages; // Assert Assert.True(response.Success); + Assert.Equal(expectedTotalAds, actualTotalAds); Assert.Equal(expectedTotalPages, actualTotalPages); } @@ -142,6 +144,5 @@ public async Task GetBrowseAdsPage_WhenRequestIsValid_ShouldReturnCorrectAds(int Assert.Equal(expected.Location, actual.Location); Assert.Equal(expected.IsRemote, actual.IsRemote); } - } } diff --git a/TutorLizard.BusinessLogic/Models/DTOs/Responses/GetBrowseAdsPageResponse.cs b/TutorLizard.BusinessLogic/Models/DTOs/Responses/GetBrowseAdsPageResponse.cs index d0b41367..2df5cde9 100644 --- a/TutorLizard.BusinessLogic/Models/DTOs/Responses/GetBrowseAdsPageResponse.cs +++ b/TutorLizard.BusinessLogic/Models/DTOs/Responses/GetBrowseAdsPageResponse.cs @@ -6,5 +6,6 @@ public class GetBrowseAdsPageResponse public int PageNumber { get; set; } public int PageSize { get; set; } public int TotalPages { get; set; } + public int TotalAds { get; set; } public AdSearchCriteriaDto SearchCriteria { get; set; } = new(); } diff --git a/TutorLizard.BusinessLogic/Services/BrowseService.cs b/TutorLizard.BusinessLogic/Services/BrowseService.cs index cbfa12fb..5a725bee 100644 --- a/TutorLizard.BusinessLogic/Services/BrowseService.cs +++ b/TutorLizard.BusinessLogic/Services/BrowseService.cs @@ -33,7 +33,7 @@ public async Task GetBrowseAdsPage(GetBrowseAdsPageReq } IQueryable adsQuery = ApplySearchCriteria(_adRepository.GetAll(), request.SearchCriteria); - int totalPages = await GetTotalPagesCount(adsQuery, request); + (int totalPages, int totalAds) = await GetTotalCounts(adsQuery, request); adsQuery = ApplyPagination(adsQuery, request, totalPages); List ads = await adsQuery @@ -60,6 +60,7 @@ public async Task GetBrowseAdsPage(GetBrowseAdsPageReq PageNumber = request.PageNumber, PageSize = request.PageSize, TotalPages = totalPages, + TotalAds = totalAds, SearchCriteria = request.SearchCriteria }; @@ -228,17 +229,17 @@ private IQueryable ApplySearchByCategoryId(IQueryable ads, int? category return ads; } - private async Task GetTotalPagesCount(IQueryable ads, GetBrowseAdsPageRequest request) + private async Task<(int totalPages, int totalAds)> GetTotalCounts(IQueryable ads, GetBrowseAdsPageRequest request) { - int adCount = await ads.CountAsync(); + int totalAds = await ads.CountAsync(); - int totalPages = adCount / request.PageSize; - if (adCount == 0 || adCount % request.PageSize != 0) + int totalPages = totalAds / request.PageSize; + if (totalAds == 0 || totalAds % request.PageSize != 0) { totalPages++; } - return totalPages; + return (totalPages, totalAds); } private IQueryable ApplyPagination(IQueryable ads, GetBrowseAdsPageRequest request, int totalPages) { diff --git a/TutorLizard.Web/Views/Browse/Ads.cshtml b/TutorLizard.Web/Views/Browse/Ads.cshtml index 833dc7d3..ed40ffed 100644 --- a/TutorLizard.Web/Views/Browse/Ads.cshtml +++ b/TutorLizard.Web/Views/Browse/Ads.cshtml @@ -12,6 +12,12 @@ + +@if (Model.SearchCriteria.AnySearch) +{ +
Liczba wyników wyszukiwania: @Model.TotalAds
+} + @foreach (var ad in Model.Ads) {