From dcbd51df2247651e5341c394de691c829b4f60f0 Mon Sep 17 00:00:00 2001 From: Tigran TIKSN Torosyan Date: Sun, 19 Jan 2020 05:11:18 +0200 Subject: [PATCH] Apply search filters on database search results (#418) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses #417 Co-Authored-By: Loïc Sharma --- .../Search/DatabaseSearchService.cs | 49 ++++++++++++------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/src/BaGet.Core/Search/DatabaseSearchService.cs b/src/BaGet.Core/Search/DatabaseSearchService.cs index 87102dcc..593e7085 100644 --- a/src/BaGet.Core/Search/DatabaseSearchService.cs +++ b/src/BaGet.Core/Search/DatabaseSearchService.cs @@ -176,32 +176,41 @@ private async Task>> SearchImplAsync( CancellationToken cancellationToken) { var frameworks = GetCompatibleFrameworksOrNull(framework); - var search = (IQueryable)_context.Packages.Where(p => p.Listed); + IQueryable search = _context.Packages; - if (!string.IsNullOrEmpty(query)) + IQueryable AddSearchFilters(IQueryable packageQuery) { - query = query.ToLower(); - search = search.Where(p => p.Id.ToLower().Contains(query)); - } + if (!includePrerelease) + { + packageQuery = packageQuery.Where(p => !p.IsPrerelease); + } - if (!includePrerelease) - { - search = search.Where(p => !p.IsPrerelease); - } + if (!includeSemVer2) + { + packageQuery = packageQuery.Where(p => p.SemVerLevel != SemVerLevel.SemVer2); + } - if (!includeSemVer2) - { - search = search.Where(p => p.SemVerLevel != SemVerLevel.SemVer2); - } + if (!string.IsNullOrEmpty(packageType)) + { + packageQuery = packageQuery.Where(p => p.PackageTypes.Any(t => t.Name == packageType)); + } - if (!string.IsNullOrEmpty(packageType)) - { - search = search.Where(p => p.PackageTypes.Any(t => t.Name == packageType)); + if (frameworks != null) + { + packageQuery = packageQuery.Where(p => p.TargetFrameworks.Any(f => frameworks.Contains(f.Moniker))); + } + + packageQuery = packageQuery.Where(p => p.Listed); + + return packageQuery; } - if (frameworks != null) + search = AddSearchFilters(search); + + if (!string.IsNullOrEmpty(query)) { - search = search.Where(p => p.TargetFrameworks.Any(f => frameworks.Contains(f.Moniker))); + query = query.ToLower(); + search = search.Where(p => p.Id.ToLower().Contains(query)); } var packageIds = search.Select(p => p.Id) @@ -227,7 +236,9 @@ private async Task>> SearchImplAsync( search = _context.Packages.Where(p => packageIdResults.Contains(p.Id)); } - var results = await search.Where(p => p.Listed).ToListAsync(cancellationToken); + search = AddSearchFilters(search); + + var results = await search.ToListAsync(cancellationToken); return results.GroupBy(p => p.Id).ToList(); }