Пример #1
0
        public async Task <IEnumerable <IPackageSearchMetadata> > LoadAsync(string searchTerm, PageContinuation pageContinuation, SearchFilter searchFilter, CancellationToken token)
        {
            Argument.IsValid(nameof(pageContinuation), pageContinuation, pageContinuation.IsValid);

            var source           = pageContinuation.Source.PackageSources.FirstOrDefault();
            var observedProjects = _extensibleProjectLocator.GetAllExtensibleProjects();

            SourceRepository repository = null;

            if (source is not null)
            {
                repository = _repositoryProvider.CreateRepository(source);
            }
            else
            {
                repository = observedProjects.FirstOrDefault().AsSourceRepository(_repositoryProvider);
            }

            try
            {
                var localPackages = await _projectManager.CreatePackagesCollectionFromProjectsAsync(observedProjects, token);

                var pagedPackages = localPackages
                                    .GetLatest(VersionComparer.Default)
                                    .Where(package => package.Id.IndexOf(searchTerm ?? String.Empty, StringComparison.OrdinalIgnoreCase) != -1)
                                    .OrderBy(package => package.Id)
                                    .Skip(pageContinuation.GetNext());


                if (pageContinuation.Size > 0)
                {
                    pagedPackages = pagedPackages.Take(pageContinuation.Size).ToList();
                }

                List <IPackageSearchMetadata> combinedFindedMetadata = new List <IPackageSearchMetadata>();

                foreach (var package in pagedPackages)
                {
                    var metadata = await GetPackageMetadataAsync(package, searchFilter.IncludePrerelease, token);

                    if (metadata is not null)
                    {
                        combinedFindedMetadata.Add(metadata);
                    }
                }

                return(combinedFindedMetadata);
            }
            catch (FatalProtocolException ex) when(token.IsCancellationRequested)
            {
                //task is cancelled, supress
                throw new OperationCanceledException("Search request was canceled", ex, token);
            }
        }