public ActionResult Index(JobOpportunityPagingParameter model)
        {
            var viewModel = GetSearchViewModel(model);

            var jobOpportunities = _jobOpportunityService.GetAllJobOpportunitiesPagedByFilters(model);

            viewModel.Result = jobOpportunities;

            return View(viewModel);
        }
        public ActionResult Index(JobOpportunityPagingParameter model)
        {
            var viewModel = GetSearchViewModel(model);

            if (!string.IsNullOrWhiteSpace(viewModel.SelectedLocationName) &&
                string.IsNullOrWhiteSpace(viewModel.SelectedLocationPlaceId))
            {
                ModelState.AddModelError("SelectedLocationName", "");
                return View(viewModel).WithError("Debe seleccionar una Localidad para buscar.");
            }

            var jobOpportunities = _jobOpportunityService.GetAllJobOpportunitiesPagedByFilters(model);

            viewModel.Result = jobOpportunities;

            return View(viewModel);
        }
        public void Index_LocationNameNotNull_PlaceIdNull_ReturnsViewWithError()
        {
            // Arrange
            var param = new JobOpportunityPagingParameter {
                SelectedLocationPlaceId = null,
                SelectedLocationName = "Guachupita",
                JobCategory = JobCategory.All,
                Keyword = "myKeyword",
                IsRemote = true
            };

            var jobCategoriesCount = new[] { new JobCategoryCountDto() }.ToList();
            _jobOpportunityService.GetMainJobCategoriesCount().Returns(jobCategoriesCount);

            // Act
            var result = (AlertDecoratorResult)_sut.Index(param);

            // Assert
            _jobOpportunityService.Received(1).GetMainJobCategoriesCount();
            _jobOpportunityService.DidNotReceiveWithAnyArgs().GetAllJobOpportunitiesPagedByFilters(param);

            _sut.ModelState.IsValid.Should().BeFalse();
            _sut.ModelState.Should().ContainKey("SelectedLocationName");

            result.AlertClass.Should().Be("alert-danger");
            result.Message.Should().Be("Debe seleccionar una Localidad para buscar.");
            var viewResult = (ViewResult)result.InnerResult;
            var model = (JobOpportunitySearchViewModel)viewResult.Model;

            model.SelectedLocationLatitude.Should().Be(param.SelectedLocationLatitude);
            model.SelectedLocationLongitude.Should().Be(param.SelectedLocationLongitude);
            model.SelectedLocationPlaceId.Should().BeNull();
            model.SelectedLocationName.Should().Be(param.SelectedLocationName);
            model.JobCategory.Should().Be(param.JobCategory);
            model.Keyword.Should().Be(param.Keyword);
            model.IsRemote.Should().Be(param.IsRemote);
            model.CategoriesCount.Should().BeSameAs(jobCategoriesCount);
            model.Result.Should().BeEmpty();
        }
        /// <summary>
        /// Transform JobOpportunityPagingParameter into JobOpportunitySearchViewModel with Locations
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private JobOpportunitySearchViewModel GetSearchViewModel(JobOpportunityPagingParameter model)
        {
            if (string.IsNullOrWhiteSpace(model.SelectedLocationName))
            {
                model.SelectedLocationLatitude = string.Empty;
                model.SelectedLocationLongitude = string.Empty;
                model.SelectedLocationPlaceId = string.Empty;
            }

            var viewModel = new JobOpportunitySearchViewModel
            {
                SelectedLocationPlaceId = model.SelectedLocationPlaceId,
                SelectedLocationName = model.SelectedLocationName,
                SelectedLocationLongitude = model.SelectedLocationLongitude,
                SelectedLocationLatitude = model.SelectedLocationLatitude,
                JobCategory = model.JobCategory,
                Keyword = model.Keyword,
                IsRemote = model.IsRemote,
                CategoriesCount = _jobOpportunityService.GetMainJobCategoriesCount(),
            };

            return viewModel;
        }
        public void Index_ReturnsRequestedDataCorrectly_LocationEmpty()
        {
            // Arrange
            var param = new JobOpportunityPagingParameter {
                SelectedLocationName = string.Empty,
                SelectedLocationLatitude = "18.686868",
                SelectedLocationLongitude = "-69.454545",
                SelectedLocationPlaceId = "111",
                JobCategory = JobCategory.All,
                Keyword = "myKeyword",
                IsRemote = true
            };

            var jobCategoriesCount = new[] { new JobCategoryCountDto() }.ToList();
            var jobOpportunityList = new[] { new JobOpportunity() }.ToPagedList(1, 1);

            _jobOpportunityService.GetMainJobCategoriesCount().Returns(jobCategoriesCount);
            _jobOpportunityService.GetAllJobOpportunitiesPagedByFilters(param).Returns(jobOpportunityList);

            // Act
            var result = (ViewResult)_sut.Index(param);

            // Assert
            _sut.ModelState.IsValid.Should().BeTrue();

            _jobOpportunityService.Received(1).GetMainJobCategoriesCount();
            _jobOpportunityService.Received(1).GetAllJobOpportunitiesPagedByFilters(param);

            var model = (JobOpportunitySearchViewModel)result.Model;
            model.SelectedLocationLatitude.Should().BeEmpty();
            model.SelectedLocationLongitude.Should().BeEmpty();
            model.SelectedLocationPlaceId.Should().BeEmpty();
            model.SelectedLocationName.Should().BeEmpty();
            model.JobCategory.Should().Be(param.JobCategory);
            model.Keyword.Should().Be(param.Keyword);
            model.IsRemote.Should().Be(param.IsRemote);
            model.CategoriesCount.Should().BeSameAs(jobCategoriesCount);
            model.Result.Should().BeSameAs(jobOpportunityList);
        }
 public IPagedList<JobOpportunity> GetAllJobOpportunitiesPagedByFilters(JobOpportunityPagingParameter parameter)
 {
     return _jobOpportunityRepository.GetAllJobOpportunitiesPagedByFilters(parameter);
 }
        /// <summary>
        /// Transform JobOpportunityPagingParameter into JobOpportunitySearchViewModel with Locations
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private JobOpportunitySearchViewModel GetSearchViewModel(JobOpportunityPagingParameter model)
        {
            var locations = _locationService.GetLocationsWithDefault();

            var viewModel = new JobOpportunitySearchViewModel
            {
                Locations = locations.ToSelectList(l => l.Id, l => l.Name, model.SelectedLocation),
                SelectedLocation = model.SelectedLocation,
                JobCategory = model.JobCategory,
                Keyword = model.Keyword,
                IsRemote = model.IsRemote
            };

            return viewModel;
        }