Exemple #1
0
        public async Task <IPaginatedList <AssetDto> > GetByRepositoryIdAsync(Guid repositoryId, string search, IEnumerable <EcosystemKind> kinds, bool dependents, int pageIndex = 1, int pageSize = 10)
        {
            var filter = kinds.ToIntArray();
            var query  = context.Assets.AsQueryable();

            if (dependents)
            {
                var dependencies = await dependencyService.GetByRepositoryIdAsync(repositoryId, kinds, search, pageIndex, pageSize);

                if (dependencies.Count == 0)
                {
                    return(new PaginatedList <AssetDto>(Enumerable.Empty <AssetDto>().ToList(), 0, 0, 0));
                }

                query = context.Assets.Where(asset => asset.RepositoryId != repositoryId);

                foreach (var dependency in dependencies)
                {
                    query = query.Where(asset => context.AssetDependencies.Any(ad => ad.DependencyId == dependency.DependencyId && ad.AssetId == asset.Id));
                }
            }
            else
            {
                query = context.Assets.Where(asset => asset.RepositoryId == repositoryId).AsQueryable();
            }

            if (filter.Any())
            {
                query = query.Where(asset => filter.Contains((int)asset.Kind));
            }

            if (!string.IsNullOrWhiteSpace(search))
            {
                query = query.Where(asset => asset.Path.Contains(search));
            }

            var paging = query.Select(asset => new AssetDto
            {
                AssetId      = asset.Id,
                Repository   = asset.Repository.Url,
                Dependencies = context.AssetDependencies.Count(assetDependency => assetDependency.AssetId == asset.Id),
                Asset        = asset.Path,
                Kind         = asset.Kind,
                RepositoryId = asset.RepositoryId,
                VcsId        = asset.Repository.VcsId
            })
                         .OrderByDescending(asset => asset.Dependencies);

            return(await PaginatedList <AssetDto> .CreateAsync(paging, pageIndex, pageSize));
        }