コード例 #1
0
        public MediatorResponse <TraineeshipSearchResponseViewModel> Results(TraineeshipSearchViewModel model)
        {
            UserDataProvider.Pop(CandidateDataItemNames.VacancyDistance);

            if (model.ResultsPerPage == 0)
            {
                model.ResultsPerPage = GetResultsPerPage();
            }

            UserDataProvider.Push(UserDataItemNames.ResultsPerPage, model.ResultsPerPage.ToString(CultureInfo.InvariantCulture));

            model.Distances = GetDistances(false);
            model.ResultsPerPageSelectList = GetResultsPerPageSelectList(model.ResultsPerPage);

            var clientResult = _searchRequestValidator.Validate(model);

            if (!clientResult.IsValid)
            {
                return(GetMediatorResponse(TraineeshipSearchMediatorCodes.Results.ValidationError,
                                           new TraineeshipSearchResponseViewModel {
                    VacancySearch = model
                },
                                           clientResult));
            }

            if (!HasGeoPoint(model))
            {
                // User did not select a location from the dropdown list, provide suggested locations.
                if (model.Location != null)
                {
                    var suggestedLocations = _searchProvider.FindLocation(model.Location.Trim());

                    if (suggestedLocations.HasError())
                    {
                        return(GetMediatorResponse(TraineeshipSearchMediatorCodes.Results.HasError, new TraineeshipSearchResponseViewModel {
                            VacancySearch = model
                        }, suggestedLocations.ViewModelMessage, UserMessageLevel.Warning));
                    }

                    if (suggestedLocations.Locations.Any())
                    {
                        var location = suggestedLocations.Locations.First();

                        model.Location  = location.Name;
                        model.Latitude  = location.Latitude;
                        model.Longitude = location.Longitude;

                        model.LocationSearches = suggestedLocations.Locations.Skip(1).Select(each =>
                        {
                            var vsvm = new TraineeshipSearchViewModel
                            {
                                Location       = each.Name,
                                Latitude       = each.Latitude,
                                Longitude      = each.Longitude,
                                PageNumber     = model.PageNumber,
                                SortType       = model.SortType,
                                WithinDistance = model.WithinDistance,
                                ResultsPerPage = model.ResultsPerPage
                            };

                            vsvm.Hash = vsvm.LatLonLocHash();

                            return(vsvm);
                        }).ToArray();
                    }
                }
            }

            var locationResult = _searchLocationValidator.Validate(model);

            if (!locationResult.IsValid)
            {
                return(GetMediatorResponse(TraineeshipSearchMediatorCodes.Results.Ok, new TraineeshipSearchResponseViewModel {
                    VacancySearch = model
                }));
            }

            UserDataProvider.Push(UserDataItemNames.LastSearchedLocation, string.Join("|", model.Location, model.Latitude, model.Longitude));

            var traineeshipSearchResponseViewModel = _traineeshipVacancyProvider.FindVacancies(model);

            traineeshipSearchResponseViewModel.VacancySearch = model;
            if (traineeshipSearchResponseViewModel.ExactMatchFound)
            {
                var id = traineeshipSearchResponseViewModel.Vacancies.Single().Id;
                return(GetMediatorResponse <TraineeshipSearchResponseViewModel>(TraineeshipSearchMediatorCodes.Results.ExactMatchFound, parameters: new { id }));
            }

            if (traineeshipSearchResponseViewModel.VacancySearch != null)
            {
                traineeshipSearchResponseViewModel.VacancySearch.SortTypes = GetSortTypes(model.SortType);
            }

            if (traineeshipSearchResponseViewModel.HasError())
            {
                return(GetMediatorResponse(TraineeshipSearchMediatorCodes.Results.HasError, new TraineeshipSearchResponseViewModel {
                    VacancySearch = model
                }, traineeshipSearchResponseViewModel.ViewModelMessage, UserMessageLevel.Warning));
            }

            return(GetMediatorResponse(TraineeshipSearchMediatorCodes.Results.Ok, traineeshipSearchResponseViewModel));
        }