Exemplo n.º 1
0
        public async Task <IReadOnlyList <Package> > FindPackagesOrNullAsync(string id, CancellationToken cancellationToken)
        {
            var items = await _upstreamClient.GetPackageMetadataAsync(id, cancellationToken);

            if (!items.Any())
            {
                return(null);
            }

            var upstreamPackages = items.Select(ToPackage);

            // Return the upstream packages if there are no local packages matching the package id.
            var localPackages = await _localPackages.FindAsync(id, includeUnlisted : true);

            if (!localPackages.Any())
            {
                return(upstreamPackages.ToList());
            }

            // Otherwise, merge the local packages into the upstream packages.
            var result = upstreamPackages.ToDictionary(p => new PackageIdentity(p.Id, p.Version));
            var local  = localPackages.ToDictionary(p => new PackageIdentity(p.Id, p.Version));

            foreach (var localPackage in local)
            {
                result[localPackage.Key] = localPackage.Value;
            }

            return(result.Values.ToList());
        }