Пример #1
0
        public static IQueryable <Models.Site> SortSitesBy(this IQueryable <Models.Site> sites, SitesSorts sortBy)
        {
            switch (sortBy)
            {
            case SitesSorts.NoSort:
                return(sites);

            case SitesSorts.AltitudeTakeOff:
                return(sites
                       .OrderBy(s => s.AltitudeTakeOff)
                       .ThenBy(s => s.Name));

            case SitesSorts.LevelRequired:
                return(sites.OrderByDescending(s => s.Level.DifficultyIndex).ThenBy(s => s.Name));

            case SitesSorts.Name:
                return(sites.OrderBy(s => s.Name));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        // GET: Sites
        public async Task <IActionResult> Index(SitesSorts pSiteSort, SitesFilters filter, string filterInfo, SitesSearchingBy search, string searchInfo)
        {
            IEnumerable <SiteDto> listSites = null;
            string textToSort   = "";
            string textToSearch = "";

            if (filter == SitesFilters.Orientation)
            {
                textToSort = "Orientation";
            }
            if (filter == SitesFilters.Altitude)
            {
                textToSort = "AltitudeTakeOff";
            }

            if (search == SitesSearchingBy.Name)
            {
                textToSearch = "SiteName";
            }
            if (search == SitesSearchingBy.ApproachManeuver)
            {
                textToSearch = "SiteApproach";
            }
            using (var httpClient = new HttpClient())
            {
                using (var response = await httpClient.GetAsync($"http://localhost:50106/api/v1/sites?SortBy={pSiteSort}&FilterBy={filter}&{textToSort}={filterInfo}&SearchBy={search}&{textToSearch}={searchInfo}"))
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();

                        listSites = JsonConvert.DeserializeObject <List <SiteDto> >(apiResponse);
                    }
                    else
                    {
                        listSites = Enumerable.Empty <SiteDto>();
                    }
                }
            }

            var siteFilters = Enum.GetValues(typeof(SitesFilters))
                              .Cast <SitesFilters>()
                              .Select(d => new SelectListItem
            {
                Text  = d.ToString(),
                Value = ((int)d).ToString()
            }).ToList();

            ViewData["siteFilterItems"] = new SelectList(siteFilters, "Value", "Text");

            var siteSearch = Enum.GetValues(typeof(SitesSearchingBy))
                             .Cast <SitesSearchingBy>()
                             .Select(d => new SelectListItem
            {
                Text  = d.ToString(),
                Value = ((int)d).ToString()
            }).ToList();

            ViewData["siteSearchItems"] = new SelectList(siteSearch, "Value", "Text");

            return(View(listSites));
        }