Skip to content

Commit

Permalink
Add TotalAds property to GetBrowseAdsPageResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
Zjyslav committed Jun 19, 2024
1 parent 4c0e275 commit bf7490e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -142,6 +144,5 @@ public async Task GetBrowseAdsPage_WhenRequestIsValid_ShouldReturnCorrectAds(int
Assert.Equal(expected.Location, actual.Location);
Assert.Equal(expected.IsRemote, actual.IsRemote);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
13 changes: 7 additions & 6 deletions TutorLizard.BusinessLogic/Services/BrowseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<GetBrowseAdsPageResponse> GetBrowseAdsPage(GetBrowseAdsPageReq
}

IQueryable<Ad> 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<AdListItemDto> ads = await adsQuery
Expand All @@ -60,6 +60,7 @@ public async Task<GetBrowseAdsPageResponse> GetBrowseAdsPage(GetBrowseAdsPageReq
PageNumber = request.PageNumber,
PageSize = request.PageSize,
TotalPages = totalPages,
TotalAds = totalAds,
SearchCriteria = request.SearchCriteria
};

Expand Down Expand Up @@ -228,17 +229,17 @@ private IQueryable<Ad> ApplySearchByCategoryId(IQueryable<Ad> ads, int? category
return ads;
}

private async Task<int> GetTotalPagesCount(IQueryable<Ad> ads, GetBrowseAdsPageRequest request)
private async Task<(int totalPages, int totalAds)> GetTotalCounts(IQueryable<Ad> 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<Ad> ApplyPagination(IQueryable<Ad> ads, GetBrowseAdsPageRequest request, int totalPages)
{
Expand Down
6 changes: 6 additions & 0 deletions TutorLizard.Web/Views/Browse/Ads.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
<partial name="@PartialNames.AdSearchCriteria" model="Model.SearchCriteria.ToViewModel()"/>
<button type="submit" class="btn btn-primary m-2">Wyszukaj</button>
</form>

@if (Model.SearchCriteria.AnySearch)
{
<div class="p-2">Liczba wyników wyszukiwania: @Model.TotalAds</div>
}

@foreach (var ad in Model.Ads)
{
<partial name="@PartialNames.AdListItem" model="ad" />
Expand Down

0 comments on commit bf7490e

Please sign in to comment.