private List <ApplicationSummary> SearchCandidateApplications(VacancyApplicationsSearchViewModel vacancyApplicationsSearch, List <ApplicationSummary> applications)
        {
            if (!vacancyApplicationsSearch.IsCandidateSearch())
            {
                return(applications);
            }

            var applicantId = CandidateSearchExtensions.GetCandidateId(vacancyApplicationsSearch.ApplicantId);

            if (applicantId.HasValue)
            {
                var candidate = _candidateApplicationService.GetCandidate(applicantId.Value, false);
                if (candidate != null)
                {
                    return(applications.Where(a => a.CandidateId == candidate.EntityId).ToList());
                }
                return(new List <ApplicationSummary>());
            }

            var candidateGuidPrefix = CandidateSearchExtensions.GetCandidateGuidPrefix(vacancyApplicationsSearch.ApplicantId);

            if (!string.IsNullOrEmpty(candidateGuidPrefix))
            {
                return(applications.Where(a => a.CandidateId.ToString().StartsWith(candidateGuidPrefix, StringComparison.InvariantCultureIgnoreCase)).ToList());
            }

            if (!string.IsNullOrEmpty(vacancyApplicationsSearch.ApplicantId))
            {
                //Attempt to search by an unrecognised application id. Return no results
                return(new List <ApplicationSummary>());
            }

            return
                (applications.Where(
                     a =>
                     (string.IsNullOrEmpty(vacancyApplicationsSearch.FirstName) || a.CandidateDetails.FirstName.StartsWith(vacancyApplicationsSearch.FirstName, StringComparison.InvariantCultureIgnoreCase)) &&
                     (string.IsNullOrEmpty(vacancyApplicationsSearch.LastName) || a.CandidateDetails.LastName.StartsWith(vacancyApplicationsSearch.LastName, StringComparison.InvariantCultureIgnoreCase)) &&
                     (string.IsNullOrEmpty(vacancyApplicationsSearch.Postcode) || a.CandidateDetails.Address.Postcode.Replace(" ", "").StartsWith(vacancyApplicationsSearch.Postcode.Replace(" ", ""), StringComparison.InvariantCultureIgnoreCase))
                     ).ToList());
        }
        public CandidateSearchResultsViewModel SearchCandidates(CandidateSearchViewModel searchViewModel, string ukprn)
        {
            var dateOfBirth = string.IsNullOrEmpty(searchViewModel.DateOfBirth) ? (DateTime?)null : DateTime.Parse(searchViewModel.DateOfBirth, _dateCultureInfo);

            IEnumerable <int> providerSiteIds = null;

            if (!string.IsNullOrEmpty(ukprn))
            {
                var providerId         = _providerService.GetProvider(ukprn).ProviderId;
                var ownedProviderSites = _providerService.GetOwnedProviderSites(providerId);
                providerSiteIds = ownedProviderSites.Select(ps => ps.ProviderSiteId).ToList();
            }

            var request    = new CandidateSearchRequest(searchViewModel.FirstName, searchViewModel.LastName, dateOfBirth, searchViewModel.Postcode, CandidateSearchExtensions.GetCandidateGuidPrefix(searchViewModel.ApplicantId), CandidateSearchExtensions.GetCandidateId(searchViewModel.ApplicantId), providerSiteIds, !string.IsNullOrEmpty(ukprn));
            var candidates = _candidateSearchService.SearchCandidates(request) ?? new List <CandidateSummary>();

            var results = new CandidateSearchResultsViewModel
            {
                SearchViewModel = searchViewModel,
                Candidates      = new PageableViewModel <CandidateSummaryViewModel>
                {
                    Page =
                        candidates.OrderBy(c => c.LastName)
                        .ThenBy(c => c.FirstName)
                        .ThenBy(c => c.Address.Postcode)
                        .ThenBy(c => c.Address.AddressLine1)
                        .Skip((searchViewModel.CurrentPage - 1) * searchViewModel.PageSize)
                        .Take(searchViewModel.PageSize)
                        .Select(c => _mapper.Map <CandidateSummary, CandidateSummaryViewModel>(c))
                        .ToList(),
                    ResultsCount       = candidates.Count,
                    CurrentPage        = searchViewModel.CurrentPage,
                    TotalNumberOfPages =
                        candidates.Count == 0
                            ? 1
                            : (int)Math.Ceiling((double)candidates.Count / searchViewModel.PageSize)
                }
            };

            return(results);
        }