Example #1
0
        public ApprenticeshipSearchResponseViewModel Build()
        {
            var vacancies = new List <ApprenticeshipVacancySummaryViewModel>();
            var hits      = _totalLocalHits > 0 ? _totalLocalHits : _totalNationalHits;

            if (hits > 0)
            {
                vacancies = new Fixture()
                            .Build <ApprenticeshipVacancySummaryViewModel>()
                            .With(x => x.IsPositiveAboutDisability, _isPositiveAboutDisability)
                            .With(x => x.NumberOfPositions, _numberOfPositions)
                            .With(x => x.VacancyLocationType, ApprenticeshipLocationType.NonNational)
                            .CreateMany(hits)
                            .ToList();
            }

            var viewModel = new ApprenticeshipSearchResponseViewModel
            {
                TotalLocalHits    = _totalLocalHits,
                TotalNationalHits = _totalNationalHits,
                VacancySearch     = _vacancySearchViewModel,
                Vacancies         = vacancies
            };

            return(viewModel);
        }
Example #2
0
        private static void SetAggregationResults(
            ApprenticeshipSearchResponseViewModel response,
            IEnumerable <AggregationResult> aggregationResults,
            IEnumerable <AggregationResult> unfilteredAggregationResults)
        {
            if (aggregationResults == null || response.VacancySearch == null || response.VacancySearch.Categories == null)
            {
                return;
            }

            var aggregationResultsList           = aggregationResults.ToList();
            var unfilteredAggregationResultsList = unfilteredAggregationResults.ToList();

            foreach (var category in response.VacancySearch.Categories)
            {
                foreach (var subCategory in category.SubCategories)
                {
                    var aggregationResult = aggregationResultsList.SingleOrDefault(each => each.Code == subCategory.CodeName);

                    if (aggregationResult == null && category.CodeName != response.VacancySearch.Category)
                    {
                        aggregationResult = unfilteredAggregationResultsList.SingleOrDefault(each => each.Code == subCategory.CodeName);
                    }

                    subCategory.Count = aggregationResult?.Count ?? 0;
                }

                category.Count = category.SubCategories.Sum(each => each.Count);
            }
        }
Example #3
0
        public ResultsViewBuilder()
        {
            _viewModel = new ApprenticeshipSearchResponseViewModelBuilder().Build();

            RouteTable.Routes.Clear();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
Example #4
0
        private static void SetAggregationResults(ApprenticeshipSearchResponseViewModel response, IEnumerable <AggregationResult> aggregationResults)
        {
            if (aggregationResults == null || response.VacancySearch == null || response.VacancySearch.Categories == null)
            {
                return;
            }

            var aggregationResultsList = aggregationResults.ToList();

            foreach (var category in response.VacancySearch.Categories)
            {
                foreach (var subCategory in category.SubCategories)
                {
                    var aggregationResult = aggregationResultsList.SingleOrDefault(ar => ar.Code == subCategory.CodeName);
                    if (aggregationResult != null)
                    {
                        subCategory.Count = aggregationResult.Count;
                    }
                }
            }

            foreach (var category in response.VacancySearch.Categories.Where(c => c.CodeName == response.VacancySearch.Category || c.SubCategories.Any(sc => sc.Count.HasValue)))
            {
                foreach (var subCategory in category.SubCategories)
                {
                    if (!subCategory.Count.HasValue)
                    {
                        subCategory.Count = 0;
                    }
                }
            }
        }
Example #5
0
        public void ShouldReturnPrevPageNumberGivenStartPage(int startPage, int expected)
        {
            var test = new ApprenticeshipSearchResponseViewModel {
                TotalLocalHits = 101, PageSize = 10, VacancySearch = new ApprenticeshipSearchViewModel {
                    PageNumber = startPage, LocationType = ApprenticeshipLocationType.NonNational
                }
            };

            test.PrevPage.Should().Be(expected);
        }
Example #6
0
        public void ShouldReturnNextPageIfNoStartPage()
        {
            var test = new ApprenticeshipSearchResponseViewModel
            {
                TotalLocalHits = 101,
                PageSize       = 10
            };

            test.NextPage.Should().Be(1);
        }
        public ApprenticeshipSearchResponseViewModel Convert(ResolutionContext context)
        {
            var source = (SearchResults <ApprenticeshipSearchResponse, ApprenticeshipSearchParameters>)context.SourceValue;

            var viewModel = new ApprenticeshipSearchResponseViewModel
            {
                Vacancies = context.Engine.Map <IEnumerable <ApprenticeshipSearchResponse>, IEnumerable <ApprenticeshipVacancySummaryViewModel> >(source.Results)
            };

            return(viewModel);
        }
        public ApprenticeshipSearchResponseViewModel Build()
        {
            var viewModel = new ApprenticeshipSearchResponseViewModel
            {
                TotalLocalHits    = _totalLocalHits,
                TotalNationalHits = _totalNationalHits,
                VacancySearch     = new ApprenticeshipSearchViewModelBuilder().Build()
            };

            return(viewModel);
        }
Example #9
0
        public void ShouldReturnPrevPageIfNoStartPage()
        {
            var test = new ApprenticeshipSearchResponseViewModel
            {
                TotalLocalHits = 101,
                PageSize       = 10,
                VacancySearch  = new ApprenticeshipSearchViewModel {
                    LocationType = ApprenticeshipLocationType.NonNational
                }
            };

            test.PrevPage.Should().Be(0);
        }
Example #10
0
        public void ShouldReturnTheNumberOfPagesGivenThePageSize(int hits, int pageSize, int expected)
        {
            var test = new ApprenticeshipSearchResponseViewModel
            {
                VacancySearch = new ApprenticeshipSearchViewModel {
                    LocationType = ApprenticeshipLocationType.NonNational
                },
                TotalLocalHits = hits,
                PageSize       = pageSize
            };

            test.Pages.Should().Be(expected);
        }
Example #11
0
        private void SetSavedVacancyStatuses(Guid candidateId, ApprenticeshipSearchResponseViewModel results)
        {
            var apprenticeshipApplications = _candidateServiceProvider.GetApprenticeshipApplications(candidateId, false).ToList();

            foreach (var result in results.Vacancies)
            {
                var apprenticeshipApplication = apprenticeshipApplications
                                                .SingleOrDefault(each => each.LegacyVacancyId == result.Id);

                result.CandidateApplicationStatus = apprenticeshipApplication == null
                    ? default(ApplicationStatuses?)
                                                    : apprenticeshipApplication.Status;
            }
        }
Example #12
0
 public SearchResultsViewBuilder With(ApprenticeshipSearchResponseViewModel viewModel)
 {
     _viewModel = viewModel;
     return(this);
 }
Example #13
0
        public ApprenticeshipSearchResponseViewModel FindVacancies(ApprenticeshipSearchViewModel search)
        {
            _logger.Debug("Calling SearchProvider to find apprenticeship vacancies.");

            var searchLocation = _apprenticeshipSearchMapper.Map <ApprenticeshipSearchViewModel, Location>(search);

            try
            {
                string vacancyReference;
                var    isVacancyReference = VacancyHelper.TryGetVacancyReference(search.Keywords, out vacancyReference);

                if ((search.SearchField == ApprenticeshipSearchField.All.ToString() && isVacancyReference) || search.SearchField == ApprenticeshipSearchField.ReferenceNumber.ToString())
                {
                    if (isVacancyReference)
                    {
                        var searchParameters = new ApprenticeshipSearchParameters
                        {
                            VacancyReference = vacancyReference,
                            PageNumber       = 1,
                            PageSize         = 1
                        };

                        var searchResults = _apprenticeshipSearchService.Search(searchParameters);

                        //Expect only a single result. Any other number should be interpreted as no results
                        if (searchResults.Total == 1)
                        {
                            var exactMatchResponse = _apprenticeshipSearchMapper.Map <SearchResults <ApprenticeshipSearchResponse, ApprenticeshipSearchParameters>, ApprenticeshipSearchResponseViewModel>(searchResults);
                            exactMatchResponse.ExactMatchFound = true;
                            return(exactMatchResponse);
                        }

                        if (searchResults.Total > 1)
                        {
                            _logger.Info("{0} results found for Vacancy Reference Number {1} parsed from {2}. Expected 0 or 1", searchResults.Total, vacancyReference, search.Keywords);
                        }
                    }

                    var response = new ApprenticeshipSearchResponseViewModel
                    {
                        Vacancies     = new List <ApprenticeshipVacancySummaryViewModel>(),
                        VacancySearch = search
                    };

                    return(response);
                }

                var results = ProcessNationalAndNonNationalSearches(search, searchLocation);

                var nationalResults    = results[ResultsKeys.National];
                var nonNationalResults = results[ResultsKeys.NonNational];
                var unfilteredResults  = results[ResultsKeys.Unfiltered];

                var nationalResponse = _apprenticeshipSearchMapper.Map <SearchResults <ApprenticeshipSearchResponse, ApprenticeshipSearchParameters>, ApprenticeshipSearchResponseViewModel>(nationalResults);

                nationalResponse.VacancySearch = search;

                var nonNationalResponse = _apprenticeshipSearchMapper.Map <SearchResults <ApprenticeshipSearchResponse, ApprenticeshipSearchParameters>, ApprenticeshipSearchResponseViewModel>(nonNationalResults);

                nonNationalResponse.VacancySearch = search;

                if (search.LocationType == ApprenticeshipLocationType.NonNational)
                {
                    nonNationalResponse.TotalLocalHits    = nonNationalResults.Total;
                    nonNationalResponse.TotalNationalHits = nationalResults.Total;
                    nonNationalResponse.PageSize          = search.ResultsPerPage;

                    if (nonNationalResults.Total == 0 && nationalResults.Total > 0)
                    {
                        nonNationalResponse.Vacancies = nationalResponse.Vacancies;

                        var vacancySearch = nonNationalResponse.VacancySearch;

                        if (vacancySearch.SearchAction == SearchAction.Search || vacancySearch.SortType == VacancySearchSortType.Distance)
                        {
                            vacancySearch.SortType = string.IsNullOrWhiteSpace(vacancySearch.Keywords) ? VacancySearchSortType.ClosingDate : VacancySearchSortType.Relevancy;
                        }

                        vacancySearch.LocationType = ApprenticeshipLocationType.National;
                        SetAggregationResults(nonNationalResponse, nationalResults.AggregationResults, unfilteredResults.AggregationResults);
                    }
                    else
                    {
                        SetAggregationResults(nonNationalResponse, nonNationalResults.AggregationResults, unfilteredResults.AggregationResults);
                    }

                    return(nonNationalResponse);
                }

                nationalResponse.TotalLocalHits    = nonNationalResults.Total;
                nationalResponse.TotalNationalHits = nationalResults.Total;
                nationalResponse.PageSize          = search.ResultsPerPage;

                if (nationalResults.Total == 0 && nonNationalResults.Total > 0)
                {
                    nationalResponse.Vacancies = nonNationalResponse.Vacancies;
                    SetAggregationResults(nonNationalResponse, nonNationalResults.AggregationResults, unfilteredResults.AggregationResults);
                }
                else
                {
                    SetAggregationResults(nonNationalResponse, nationalResults.AggregationResults, unfilteredResults.AggregationResults);
                }

                return(nationalResponse);
            }
            catch (CustomException ex)
            {
                _logger.Error("Find apprenticeship vacancies failed. Check inner details for more info", ex);
                return(new ApprenticeshipSearchResponseViewModel(VacancySearchResultsPageMessages.VacancySearchFailed));
            }
            catch (Exception e)
            {
                _logger.Error("Find apprenticeship vacancies failed. Check inner details for more info", e);
                throw;
            }
        }