private static IEnumerable <ProjectDto> FilterByTags(ProjectsFilterDto filter, IEnumerable <ProjectDto> query)
        {
            if (filter.Tags != null)
            {
                query = query.Where(t2 => filter.Tags.Any(t1 => t2.Tags.Contains(t1)));
            }

            return(query);
        }
        public async Task <FilteredResutsDto <ProjectDto> > Get([FromQuery] ProjectsFilterDto filter)
        {
            var userId   = User.Identity.Name.ToLower();
            var projects = await _projectsService.GetProjectsAsync(filter, userId);

            await AssignBranchesToProjects(projects);

            return(projects);
        }
        private static IEnumerable <ProjectDto> FilterByDescription(ProjectsFilterDto filter, IEnumerable <ProjectDto> query)
        {
            if (!string.IsNullOrWhiteSpace(filter.Query))
            {
                string filterQueryNormalized = GetNormalizedQuery(filter);
                query = query.Where(x => x.Description.ToUpper().Contains(filterQueryNormalized));
            }

            return(query);
        }
 private static IEnumerable <ProjectDto> FilterByLimitedAccess(ProjectsFilterDto filter, IEnumerable <ProjectDto> query, string userId)
 {
     query = query.Where(x =>
                         (
                             !x.IsAccessLimited ||
                             (
                                 x.IsAccessLimited &&
                                 ((x.ContactPeople != null && x.ContactPeople.Any(y => y.Email == userId)) || x.Owners.Any(o => o == userId)))
                         )
                         );
     return(query);
 }
        private static string GetNormalizedQuery(ProjectsFilterDto filter)
        {
            var filterQueryNormalized = filter.Query.ToUpper().Trim();

            if (filterQueryNormalized.Last() == '*')
            {
                filterQueryNormalized = filterQueryNormalized.TrimEnd('*');
            }
            else
            {
                filterQueryNormalized = $"{filterQueryNormalized} ";
            }

            return(filterQueryNormalized);
        }
        private static IEnumerable <ProjectDto> FilterByGroups(ProjectsFilterDto filter, IEnumerable <ProjectDto> query)
        {
            if (filter.Groups != null)
            {
                var groupsNormalized = new List <string>(filter.Groups.Count);
                foreach (var item in filter.Groups)
                {
                    groupsNormalized.Add(item.ToUpper());
                }

                query = query.Where(x => groupsNormalized.Contains(x.Group.ToUpper()));
            }

            return(query);
        }
        public async Task <FilteredResutsDto <ProjectDto> > GetProjectsAsync(ProjectsFilterDto filter, string userId)
        {
            IList <ProjectDto> projects = null;

            if (!_cacheService.TryGetProjects(out projects))
            {
                var projectsTask = _projectsClient.Get();
                var usersTask    = _usersClient.Get();

                projects = await projectsTask;
                CleanProjectsAccessTokens(projects);

                var users = await usersTask;
                AddOwnersToProjects(projects, users);

                _cacheService.AddProjectsToCache(projects);
            }

            IEnumerable <ProjectDto> query = projects;

            query = FilterByName(filter, query);
            query = FilterByDescription(filter, query);
            query = FilterByGroups(filter, query);
            query = FilterByTags(filter, query);
            query = FilterByLimitedAccess(filter, query, userId);

            var allResults = query.Count();

            query = query.OrderBy(x => x.Name);
            if (filter.Limit > 0)
            {
                query = query.Skip(filter.Page * filter.Limit).Take(filter.Limit);
            }

            var filteredResults = new FilteredResutsDto <ProjectDto>
            {
                Results    = query,
                AllResults = allResults
            };

            return(filteredResults);
        }