Skip to content

Commit

Permalink
Add searching logic to GetBrowseAdsPage()
Browse files Browse the repository at this point in the history
  • Loading branch information
Zjyslav committed Jun 19, 2024
1 parent fcefafa commit 4c0e275
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public async Task GetBrowseAdsPage_WhenRequestIsValid_ShouldReturnCorrectAds(int

int expectedAdsSkipped = (pageNumber - 1) * pageSize;
List<Ad> expectedAds = ads
.OrderBy(ad => ad.DateCreated)
.Skip(expectedAdsSkipped)
.Take(pageSize)
.ToList();
Expand Down
134 changes: 120 additions & 14 deletions TutorLizard.BusinessLogic/Services/BrowseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,11 @@ public async Task<GetBrowseAdsPageResponse> GetBrowseAdsPage(GetBrowseAdsPageReq
};
}

int adCount = await _adRepository.GetAll()
.CountAsync();
IQueryable<Ad> adsQuery = ApplySearchCriteria(_adRepository.GetAll(), request.SearchCriteria);
int totalPages = await GetTotalPagesCount(adsQuery, request);
adsQuery = ApplyPagination(adsQuery, request, totalPages);

int totalPages = adCount / request.PageSize;
if (adCount == 0 || adCount % request.PageSize != 0)
{
totalPages++;
}

request.PageNumber = Math.Min(request.PageNumber, totalPages);

int resultsToSkip = Math.Max((request.PageNumber - 1) * request.PageSize, 0);
List<AdListItemDto> ads = await _adRepository.GetAll()
.Skip(resultsToSkip)
.Take(request.PageSize)
List<AdListItemDto> ads = await adsQuery
.Select(ad => new AdListItemDto()
{
Id = ad.Id,
Expand Down Expand Up @@ -147,4 +137,120 @@ public async Task<GetUsersScheduleResponse> GetUsersSchedule(GetUsersScheduleReq

return response;
}
private IQueryable<Ad> ApplySearchCriteria(IQueryable<Ad> ads, AdSearchCriteriaDto searchCriteria)
{
ads = ApplySearchByText(ads, searchCriteria.Text);
ads = ApplySearchByPriceMin(ads, searchCriteria.PriceMin);
ads = ApplySearchByPriceMax(ads, searchCriteria.PriceMax);
ads = ApplySearchByLocation(ads, searchCriteria.Location);
ads = ApplySearchByIsRemote(ads, searchCriteria.IsRemote);
ads = ApplySearchByCategoryId(ads, searchCriteria.CategoryId);

return ads;
}

private IQueryable<Ad> ApplySearchByText(IQueryable<Ad> ads, string? text)
{
if (text is null)
{
return ads;
}

text = text.ToLower();

ads = ads.Where(ad =>
ad.Title.ToLower().Contains(text) ||
ad.Subject.ToLower().Contains(text) ||
ad.Description.ToLower().Contains(text));

return ads;
}

private IQueryable<Ad> ApplySearchByPriceMin(IQueryable<Ad> ads, decimal? priceMin)
{
if (priceMin is null)
{
return ads;
}

ads = ads.Where(ad => ad.Price >= priceMin);

return ads;
}

private IQueryable<Ad> ApplySearchByPriceMax(IQueryable<Ad> ads, decimal? priceMax)
{
if (priceMax is null)
{
return ads;
}

ads = ads.Where(ad => ad.Price <= priceMax);

return ads;
}

private IQueryable<Ad> ApplySearchByLocation(IQueryable<Ad> ads, string? location)
{
if (location is null)
{
return ads;
}

location = location.ToLower();

ads = ads.Where(ad => ad.Location.ToLower().Contains(location));

return ads;
}

private IQueryable<Ad> ApplySearchByIsRemote(IQueryable<Ad> ads, bool? isRemote)
{
if (isRemote is null)
{
return ads;
}

ads = ads.Where(ad => ad.IsRemote == isRemote);

return ads;
}

private IQueryable<Ad> ApplySearchByCategoryId(IQueryable<Ad> ads, int? categoryId)
{
if (categoryId is null)
{
return ads;
}

ads = ads.Where(ad => ad.CategoryId == categoryId);

return ads;
}

private async Task<int> GetTotalPagesCount(IQueryable<Ad> ads, GetBrowseAdsPageRequest request)
{
int adCount = await ads.CountAsync();

int totalPages = adCount / request.PageSize;
if (adCount == 0 || adCount % request.PageSize != 0)
{
totalPages++;
}

return totalPages;
}
private IQueryable<Ad> ApplyPagination(IQueryable<Ad> ads, GetBrowseAdsPageRequest request, int totalPages)
{
request.PageNumber = Math.Min(request.PageNumber, totalPages);

int resultsToSkip = Math.Max((request.PageNumber - 1) * request.PageSize, 0);

ads = ads
.OrderBy(ad => ad.DateCreated)
.Skip(resultsToSkip)
.Take(request.PageSize);

return ads;
}
}

0 comments on commit 4c0e275

Please sign in to comment.