Skip to content

Commit

Permalink
Apply search filters on database search results (loic-sharma#418)
Browse files Browse the repository at this point in the history
Addresses loic-sharma#417

Co-Authored-By: Loïc Sharma <[email protected]>
  • Loading branch information
tiksn and loic-sharma committed Jan 19, 2020
1 parent 89c75ee commit dcbd51d
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions src/BaGet.Core/Search/DatabaseSearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,32 +176,41 @@ private async Task<List<IGrouping<string, Package>>> SearchImplAsync(
CancellationToken cancellationToken)
{
var frameworks = GetCompatibleFrameworksOrNull(framework);
var search = (IQueryable<Package>)_context.Packages.Where(p => p.Listed);
IQueryable<Package> search = _context.Packages;

if (!string.IsNullOrEmpty(query))
IQueryable<Package> AddSearchFilters(IQueryable<Package> 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)
Expand All @@ -227,7 +236,9 @@ private async Task<List<IGrouping<string, Package>>> 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();
}
Expand Down

0 comments on commit dcbd51d

Please sign in to comment.