From cf98f958ac338045855160ced71b7bb6d00545bc Mon Sep 17 00:00:00 2001 From: cuqmbr Date: Wed, 12 Oct 2022 10:51:35 +0300 Subject: [PATCH] feat: add filtering/searching to countries controller --- Server/Services/CountryManagementService.cs | 74 ++++++++++++++++--- .../CountryParameters.cs | 3 +- .../QueryStringParameters.cs | 2 + 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/Server/Services/CountryManagementService.cs b/Server/Services/CountryManagementService.cs index 2caad8d..e16c8ca 100644 --- a/Server/Services/CountryManagementService.cs +++ b/Server/Services/CountryManagementService.cs @@ -30,20 +30,74 @@ public class CountryManagementService : ICountryManagementService return (true, String.Empty, _mapper.Map(country)); } - public async - Task<(bool isSucceed, string message, IEnumerable countries, + public async Task<(bool isSucceed, string message, IEnumerable countries, PagingMetadata pagingMetadata)> GetCountries(CountryParameters parameters) { - var dbCountries = await _dbContext.Countries - .Skip((parameters.PageNumber - 1) * parameters.PageSize) - .Take(parameters.PageSize) - .ToListAsync(); + var dbCountries = _dbContext.Countries.AsQueryable(); + + SearchByAllCountryFields(ref dbCountries, parameters.Search); + SearchByCountryCode(ref dbCountries, parameters.CountryCode); + SearchByCountryName(ref dbCountries, parameters.CountryName); - var pagingMetadata = new PagingMetadata(_dbContext.Countries, - parameters.PageNumber, parameters.PageSize); + var pagingMetadata = ApplyPaging(ref dbCountries, parameters.PageNumber, + parameters.PageSize); - return (true, "", dbCountries.ConvertAll(c => _mapper.Map(c)), - pagingMetadata); + var countryDtos = dbCountries.ToList() + .ConvertAll(c => _mapper.Map(c)); + + return (true, "", countryDtos, pagingMetadata); + + void SearchByAllCountryFields(ref IQueryable countries, + string? search) + { + if (!countries.Any() || String.IsNullOrWhiteSpace(search)) + { + return; + } + + var s = search.Trim().ToLower(); + + countries = countries.Where(c => + c.Code.ToLower().Contains(search) || + c.Name.ToLower().Contains(search)); + } + + void SearchByCountryCode(ref IQueryable countries, + string? countryCode) + { + if (!countries.Any() || String.IsNullOrWhiteSpace(countryCode)) + { + return; + } + + countries = countries.Where(c => + c.Code.ToLower().Contains(countryCode.Trim().ToLower())); + } + + void SearchByCountryName(ref IQueryable countries, + string? countryName) + { + if (!countries.Any() || String.IsNullOrWhiteSpace(countryName)) + { + return; + } + + countries = countries.Where(c => + c.Name.ToLower().Contains(countryName.Trim().ToLower())); + } + + PagingMetadata ApplyPaging(ref IQueryable countries, + int pageNumber, int pageSize) + { + var metadata = new PagingMetadata(countries, + parameters.PageNumber, parameters.PageSize); + + countries = countries + .Skip((parameters.PageNumber - 1) * parameters.PageSize) + .Take(parameters.PageSize); + + return metadata; + } } public async Task<(bool isSucceed, string message, CountryDto country)> GetCountry(int id) diff --git a/SharedModels/QueryStringParameters/CountryParameters.cs b/SharedModels/QueryStringParameters/CountryParameters.cs index 9f7d560..e4227b7 100644 --- a/SharedModels/QueryStringParameters/CountryParameters.cs +++ b/SharedModels/QueryStringParameters/CountryParameters.cs @@ -2,5 +2,6 @@ namespace SharedModels.QueryStringParameters; public class CountryParameters : QueryStringParameters { - + public string? CountryCode { get; set; } + public string? CountryName { get; set; } } \ No newline at end of file diff --git a/SharedModels/QueryStringParameters/QueryStringParameters.cs b/SharedModels/QueryStringParameters/QueryStringParameters.cs index fe916be..5623baa 100644 --- a/SharedModels/QueryStringParameters/QueryStringParameters.cs +++ b/SharedModels/QueryStringParameters/QueryStringParameters.cs @@ -11,4 +11,6 @@ public class QueryStringParameters get => _pageSize; set => _pageSize = value > MaxPageSize ? MaxPageSize : value; } + + public string? Search { get; set; } } \ No newline at end of file