public ActionResult Index()
        {
            var vm = new SchoolSearchViewModel(base.ExtractSchoolComparisonListFromCookie(), null);

            vm.Authorities = _laService.GetLocalAuthorities();
            return(View(vm));
        }
        public List <SchoolsViewModel> GetSchoolsByName([FromBody] SchoolSearchViewModel model)
        {
            var schools = from s in _context.Schools
                          where s.Name.Contains(model.Name) || s.Representative.Contains(model.Name)
                          select new SchoolsViewModel()
            {
                Name           = s.Name,
                Representative = s.Name
            };

            return(schools.ToList());
        }
        public async Task <ActionResult> Search(
            string nameId,
            string trustName,
            string searchType,
            string suggestionUrn,
            string locationorpostcode,
            string locationCoordinates,
            string laCodeName,
            decimal?radius,
            string orderby = "",
            int page       = 1,
            string tab     = "list")
        {
            dynamic searchResp = null;
            string  errorMessage;

            ViewBag.tab = tab;

            switch (searchType)
            {
            case SearchTypes.SEARCH_BY_NAME_ID:
                var nameIdSanitized = Regex.Replace(nameId, @"(-|/)", "");
                if (IsNumeric(nameIdSanitized))
                {
                    errorMessage = _valService.ValidateSchoolIdParameter(nameIdSanitized);
                    if (string.IsNullOrEmpty(errorMessage))
                    {
                        searchResp = IsLaEstab(nameId)
                                ? _contextDataService.GetSchoolByLaEstab(nameIdSanitized)
                                : _contextDataService.GetSchoolByUrn(nameIdSanitized);

                        if (searchResp == null)
                        {
                            return(View("EmptyResult",
                                        new SchoolSearchViewModel(base.ExtractSchoolComparisonListFromCookie(),
                                                                  SearchTypes.SEARCH_BY_NAME_ID)));
                        }

                        nameId = ((Microsoft.Azure.Documents.Document)searchResp).GetPropertyValue <string>("URN");

                        return(RedirectToAction("Detail", "School", new { urn = nameId }));
                    }
                    else
                    {
                        var searchVM = new SchoolSearchViewModel(base.ExtractSchoolComparisonListFromCookie(), searchType)
                        {
                            SearchType   = searchType,
                            ErrorMessage = errorMessage,
                            Authorities  = _laService.GetLocalAuthorities()
                        };

                        return(View("../Home/Index", searchVM));
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(_valService.ValidateSchoolIdParameter(suggestionUrn)))
                    {
                        return(RedirectToAction("Detail", "School", new { urn = suggestionUrn }));
                    }

                    errorMessage = _valService.ValidateNameParameter(nameId);
                    if (string.IsNullOrEmpty(errorMessage))
                    {
                        // first see if we get a match on the word
                        searchResp = await GetSearchResults(nameId, searchType, null, null, null, null, radius,
                                                            orderby, page);

                        if (searchResp.NumberOfResults == 0)
                        {
                            return(RedirectToActionPermanent("SuggestSchool", "SchoolSearch",
                                                             new RouteValueDictionary {
                                { "nameId", nameId }
                            }));
                        }
                    }
                    else
                    {
                        var searchVM = new SchoolSearchViewModel(base.ExtractSchoolComparisonListFromCookie(), searchType)
                        {
                            SearchType   = searchType,
                            ErrorMessage = errorMessage,
                            Authorities  = _laService.GetLocalAuthorities()
                        };

                        return(View("../Home/Index", searchVM));
                    }
                }
                break;

            case SearchTypes.SEARCH_BY_TRUST_NAME:

                errorMessage = _valService.ValidateTrustNameParameter(trustName);
                if (string.IsNullOrEmpty(errorMessage))
                {
                    return(RedirectToAction("Search", "Trust", new { name = trustName }));
                }
                else
                {
                    var searchVM = new SchoolSearchViewModel(base.ExtractSchoolComparisonListFromCookie(), searchType)
                    {
                        SearchType   = searchType,
                        ErrorMessage = errorMessage,
                        Authorities  = _laService.GetLocalAuthorities()
                    };

                    return(View("../Home/Index", searchVM));
                }

            case SearchTypes.SEARCH_BY_LA_CODE_NAME:
                if (!IsNumeric(laCodeName))
                {
                    errorMessage = _valService.ValidateLaNameParameter(laCodeName);
                    if (string.IsNullOrEmpty(errorMessage))
                    {
                        var exactMatch = _laSearchService.SearchExactMatch(laCodeName);
                        if (exactMatch != null)
                        {
                            laCodeName = exactMatch.id;
                            return(await Search(nameId, trustName, searchType, suggestionUrn, locationorpostcode,
                                                locationCoordinates, laCodeName, radius, orderby, page, tab));
                        }
                        return(RedirectToAction("Search", "La", new { name = laCodeName }));
                    }
                    else
                    {
                        var searchVM = new SchoolSearchViewModel(base.ExtractSchoolComparisonListFromCookie(), searchType)
                        {
                            SearchType   = searchType,
                            ErrorMessage = errorMessage,
                            Authorities  = _laService.GetLocalAuthorities()
                        };

                        return(View("../Home/Index", searchVM));
                    }
                }
                else
                {
                    errorMessage = _valService.ValidateLaCodeParameter(laCodeName);
                    if (string.IsNullOrEmpty(errorMessage))
                    {
                        searchResp = await GetSearchResults(nameId, searchType, null, locationorpostcode,
                                                            locationCoordinates, laCodeName, radius, orderby, page);

                        int resultCount = searchResp.NumberOfResults;
                        switch (resultCount)
                        {
                        case 0:
                            return(View("EmptyResult",
                                        new SchoolSearchViewModel(base.ExtractSchoolComparisonListFromCookie(), searchType)));

                        case 1:
                            return(RedirectToAction("Detail", "School",
                                                    new
                            {
                                urn = ((Domain.Models.QueryResultsModel)searchResp).Results.First()["URN"]
                            }));
                        }
                    }
                    else
                    {
                        var searchVM = new SchoolSearchViewModel(base.ExtractSchoolComparisonListFromCookie(), searchType)
                        {
                            SearchType   = searchType,
                            ErrorMessage = errorMessage,
                            Authorities  = _laService.GetLocalAuthorities()
                        };

                        return(View("../Home/Index", searchVM));
                    }
                }

                break;

            case SearchTypes.SEARCH_BY_LOCATION:
                errorMessage = _valService.ValidateLocationParameter(locationorpostcode);
                if (string.IsNullOrEmpty(errorMessage))
                {
                    searchResp = await GetSearchResults(nameId, searchType, null, locationorpostcode,
                                                        locationCoordinates, laCodeName, radius, orderby, page);

                    int resultCnt = searchResp.NumberOfResults;
                    switch (resultCnt)
                    {
                    case 0:
                        return(View("EmptyResult",
                                    new SchoolSearchViewModel(base.ExtractSchoolComparisonListFromCookie(), searchType)));

                    case 1:
                        return(RedirectToAction("Detail", "School",
                                                new { urn = ((Domain.Models.QueryResultsModel)searchResp).Results.First()["URN"] }));
                    }
                }
                else
                {
                    var searchVM = new SchoolSearchViewModel(base.ExtractSchoolComparisonListFromCookie(), searchType)
                    {
                        SearchType   = searchType,
                        ErrorMessage = errorMessage,
                        Authorities  = _laService.GetLocalAuthorities()
                    };

                    return(View("../Home/Index", searchVM));
                }
                break;
            }

            var laName = _laService.GetLaName(laCodeName);

            return(View("SearchResults", GetSchoolViewModelList(searchResp, orderby, page, searchType, nameId, locationorpostcode, laName)));
        }
        public List <SchoolSearchReturnViewModel> SchoolsSearch([FromBody] SchoolSearchViewModel model)
        {
            List <SchoolSearchReturnViewModel> schoolsList = new List <SchoolSearchReturnViewModel>();

            try
            {
                _context.Database.OpenConnection();

                using (DbCommand cmd = _context.Database.GetDbConnection().CreateCommand())
                {
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.CommandText = "sp_SchoolsSearch";

                    List <SqlParameter> sp = new List <SqlParameter>()
                    {
                        new SqlParameter()
                        {
                            ParameterName = "@PeriodId", SqlDbType = SqlDbType.Int, Value = _IPeriods.PeriodId
                        },
                        new SqlParameter()
                        {
                            ParameterName = "@PeriodYear", SqlDbType = SqlDbType.Int, Value = _IPeriods.PeriodYear
                        },
                    };

                    cmd.Parameters.AddRange(sp.ToArray());

                    using (var reader = cmd.ExecuteReader())
                    {
                        schoolsList = reader.MapToList <SchoolSearchReturnViewModel>();
                    }
                }

                _context.Database.CloseConnection();
            }
            catch (Exception ex)
            {
                var error = ex.InnerException;
                return(new List <SchoolSearchReturnViewModel>());
            }

            if (model.Name != null)
            {
                schoolsList = schoolsList.Where(s => s.Name.ToUpper().Contains(model.Name.ToUpper())).ToList();
            }

            if (model.Category != null)
            {
                schoolsList = schoolsList.Where(s => s.CategoryCode == model.Category).ToList();
            }

            if (model.Type != null)
            {
                schoolsList = schoolsList.Where(s => s.TypeCode == model.Type).ToList();
            }

            if (model.Province != null)
            {
                schoolsList = schoolsList.Where(s => s.ProvinceCode == model.Province).ToList();
            }

            if (model.Status != null)
            {
                schoolsList = schoolsList.Where(s => s.StatusCode == model.Status).ToList();
            }

            return(schoolsList);
        }