private async Task <Returns <EstablishmentSearchPayload> > GetEstablishmentSearchPayload( EstablishmentSearchViewModel model) { var retVal = new Returns <EstablishmentSearchPayload>(); var payload = new EstablishmentSearchPayload(model.StartIndex, model.PageSize); var filters = payload.Filters; if (model.SearchType == eSearchType.Text || model.SearchType == eSearchType.EstablishmentAll) { if (model.TextSearchType == EstablishmentSearchViewModel.eTextSearchType.UKPRN) { filters.UKPRN = model.TextSearchModel.Text; } else if (model.TextSearchType == EstablishmentSearchViewModel.eTextSearchType.LAESTAB) { var laestab = LAESTAB.TryParse(model.TextSearchModel.Text).Value; var localAuthorityId = (await _lookupService.LocalAuthorityGetAllAsync()) .FirstOrDefault(x => x.Code == laestab.LocalAuthorityCode)?.Id; if (localAuthorityId.HasValue) { filters.LocalAuthorityIds = new int[] { localAuthorityId.Value } } ; filters.EstablishmentNumber = laestab.EstablishmentNumber; } else if ((model.TextSearchType == EstablishmentSearchViewModel.eTextSearchType.EstablishmentName && model.TextSearchModel.Text != null) || model.SearchType == eSearchType.EstablishmentAll) { payload.Text = model.TextSearchModel.Text; } else { retVal.ErrorMessage = "The LAESTAB, UKPRN or URN was invalid."; } } else if (model.SearchType == eSearchType.Location) { var coord = model.LocationSearchCoordinate; if (coord != null) { payload.GeoSearchLocation = coord; payload.RadiusInMiles = model.GetRadiusOption(); } else { retVal.ErrorMessage = "The co-ordinate could not be parsed."; } } else if (model.SearchType == eSearchType.ByLocalAuthority) { if (!model.SelectedLocalAuthorityIds.Any()) { retVal.ErrorMessage = "No local authority was selected"; } } filters.EducationPhaseIds = model.SelectedEducationPhaseIds.ToArray(); filters.StatusIds = model.SelectedEstablishmentStatusIds.ToArray(); filters.TypeIds = model.SelectedEstablishmentTypeIds.ToArray(); if (!filters.LocalAuthorityIds.Any()) { filters.LocalAuthorityIds = model.SelectedLocalAuthorityIds.ToArray(); } filters.ReligiousCharacterIds = model.SelectedReligiousCharacterIds.ToArray(); filters.AdmissionsPolicyIds = model.SelectedAdmissionsPolicyIds.ToArray(); filters.ProvisionBoardingIds = model.SelectedBoardingProvisionIds.ToArray(); filters.DioceseIds = model.SelectedDioceseIds.ToArray(); filters.AdministrativeDistrictIds = model.SelectedDistrictIds.ToArray(); filters.FurtherEducationTypeIds = model.SelectedFETypeIds.ToArray(); filters.GenderIds = model.SelectedGenderIds.ToArray(); filters.GovernmentOfficeRegionIds = model.SelectedGORIds.ToArray(); filters.ProvisionNurseryIds = model.SelectedNurseryProvisionIds.ToArray(); filters.ParliamentaryConstituencyIds = model.SelectedParliamentaryConstituencyIds.ToArray(); filters.ReligiousEthosIds = model.SelectedReligiousEthosIds.ToArray(); filters.RSCRegionIds = model.SelectedRSCRegionIds.ToArray(); filters.Section41ApprovedIds = model.SelectedSection41Ids.ToArray(); filters.ProvisionOfficialSixthFormIds = model.SelectedSixthFormProvisionIds.ToArray(); filters.ProvisionSpecialClassesIds = model.SelectedSpecialClassesProvisionIds.ToArray(); filters.SENIds = model.SelectedTypeOfSENProvisionIds.ToArray(); filters.UrbanRuralIds = model.SelectedUrbanRuralIds.ToArray(); filters.CloseDateMin = model.CloseDateFrom?.ToDateTime(); filters.CloseDateMax = model.CloseDateTo?.ToDateTime(); filters.OpenDateMin = model.OpenDateFrom?.ToDateTime(); filters.OpenDateMax = model.OpenDateTo?.ToDateTime(); filters.StatutoryLowAgeMin = model.AgeRangeLow?.From; filters.StatutoryLowAgeMax = model.AgeRangeLow?.To; filters.StatutoryHighAgeMin = model.AgeRangeHigh?.From; filters.StatutoryHighAgeMax = model.AgeRangeHigh?.To; filters.OfstedRatingIds = model.SelectedOfstedRatingIds.ToArray(); payload.SortBy = model.GetSortOption(); return(retVal.Set(payload)); }