private PublicRegisterLicenceListViewModel HandlePostActions( PublicRegisterLicenceListViewModel publicRegisterLicenceListViewModel, string submit) { PublicRegisterSearchCriteria newPublicRegisterSearchCriteria = null; var searchViewModel = publicRegisterLicenceListViewModel.PublicRegisterSearchCriteria; if (publicRegisterLicenceListViewModel.AvailableCountries == null) { publicRegisterLicenceListViewModel.AvailableCountries = publicRegisterViewModelBuilder.BuildAvailableCountries(); } switch (submit) { case "add": newPublicRegisterSearchCriteria = AddCountry(publicRegisterLicenceListViewModel).PublicRegisterSearchCriteria; break; case "search": // we want to update teh _currentPublicRegisterSearchCriteria = searchViewModel; searchViewModel.SearchActive = true; break; default: newPublicRegisterSearchCriteria = RemoveCountry(publicRegisterLicenceListViewModel, submit).PublicRegisterSearchCriteria; break; } if (searchViewModel.SearchActive) { publicRegisterLicenceListViewModel = publicRegisterViewModelBuilder.BuildSearchForLicences(_currentPublicRegisterSearchCriteria); publicRegisterLicenceListViewModel.PublicRegisterSearchCriteria = newPublicRegisterSearchCriteria ?? searchViewModel; } else { searchViewModel.SearchActive = false; } //Ensure we remove any countries that are selected, from the list than are selectable. if (searchViewModel.CountriesSelected != null) { publicRegisterLicenceListViewModel.AvailableCountries = publicRegisterLicenceListViewModel.AvailableCountries .Where(x => !searchViewModel.CountriesSelected.Contains(x.Value)).ToList(); } else { publicRegisterLicenceListViewModel.AvailableCountries = publicRegisterLicenceListViewModel.AvailableCountries.ToList(); } return(publicRegisterLicenceListViewModel); }
public PublicRegisterLicenceListViewModel BuildSearchForLicences( PublicRegisterSearchCriteria publicRegisterSearchCriteria) { var ukCountryNames = UkCountries.Select(x => x.Text); var licences = _licenceRepository.GetAllLicencesForPublicRegister() .Where(x => string.IsNullOrWhiteSpace(publicRegisterSearchCriteria.BusinessName) || x.BusinessName.Contains(publicRegisterSearchCriteria.BusinessName)); var countries = _referenceDataProvider.GetCountries(); if (publicRegisterSearchCriteria.CountriesSelected != null && publicRegisterSearchCriteria.CountriesSelected.Count > 0) { switch (Enum.Parse <SupplierWho>(publicRegisterSearchCriteria.SupplierWho)) { case SupplierWho.Supply: if (publicRegisterSearchCriteria.CountriesSelected.Any(x => x == "UK")) { licences = licences.Where(x => ukCountryNames.Any(y => x.OperatingCountries.Any(z => countries.Single(c => int.Parse(c.Value) == z.WorkerCountryId).Text == y))); } else if (publicRegisterSearchCriteria.CountriesSelected.Any(x => x == "Outside UK")) { licences = licences.Where(x => ukCountryNames.Any(y => x.OperatingCountries.All(z => countries.Single(c => int.Parse(c.Value) == z.WorkerCountryId).Text != y))); } else { licences = licences.Where(x => x.OperatingCountries.Any(y => y != null && publicRegisterSearchCriteria.CountriesSelected.Any(z => countries.Single(c => int.Parse(c.Value) == y.WorkerCountryId).Text.Contains(z)))); } break; case SupplierWho.AreLocated: if (publicRegisterSearchCriteria.CountriesSelected.Any(x => x == "UK")) { licences = licences.Where(x => x.Address != null && ukCountryNames.Any(y => countries.Single(c => int.Parse(c.Value) == x.Address.CountryId).Text.Contains(y))); } else if (publicRegisterSearchCriteria.CountriesSelected.Any(x => x == "Outside UK")) { licences = licences.Where(x => x.Address != null && ukCountryNames.Any(y => !x.Address.Country.IsUk)); } else { licences = licences.Where(x => x.Address != null && publicRegisterSearchCriteria.CountriesSelected.Any(y => y == countries.Single(c => int.Parse(c.Value) == x.Address.CountryId).Text)); } break; default: throw new InvalidEnumArgumentException(nameof(publicRegisterSearchCriteria.SupplierWho)); } } return(new PublicRegisterLicenceListViewModel { Licences = licences.Distinct().Select(BuildSummary), PublicRegisterSearchCriteria = publicRegisterSearchCriteria, AvailableCountries = BuildAvailableCountries(), Countries = countries, Counties = _referenceDataProvider.GetCounties() }); }