// GET: Courses
        public async Task <IActionResult> Index(TraineeShipSorts pTraineeshipSort, TraineeshipFilters filter, TraineeshipSearchs search, string searchInfo)
        {
            IEnumerable <TraineeShipDto> listTraineeships = null;

            string textToSearch = "";

            if (search == TraineeshipSearchs.License)
            {
                textToSearch = "License";
            }
            if (search == TraineeshipSearchs.Price)
            {
                textToSearch = "Price";
            }

            using (var httpClient = new HttpClient())
            {
                using (var response = await httpClient.GetAsync($"http://localhost:50106/api/v1/Traineeships?SortBy={pTraineeshipSort}&FilterBy={filter}&SearchBy={search}&{textToSearch}={searchInfo}"))
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();

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

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

            ViewData["traineeshipFilterItems"] = new SelectList(traineeshipFilter, "Value", "Text");

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

            ViewData["traineeshipSearchItems"] = new SelectList(traineeshipSearch, "Value", "Text");

            return(View(listTraineeships));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// A static method that sorts a query of traineeship given by the user.
        /// </summary>
        /// <param name="traineeships">A query of traineeships to sort</param>
        /// <param name="sortBy">>One of the sort available in a list (enum) of possibilities.</param>
        /// <returns>
        /// A custom-sorted query respecting the user's requests if the sort exist.
        /// An exception if a sort does not exist.</returns>
        public static IQueryable <Models.Traineeship> SortTraineeshipBy(this IQueryable <Models.Traineeship> traineeships, TraineeShipSorts sortBy)
        {
            switch (sortBy)
            {
            case TraineeShipSorts.NoFilter:
                return(traineeships);

            case TraineeShipSorts.Price:
                return(traineeships.OrderBy(t => t.Price));

            case TraineeShipSorts.StartDate:
                return(traineeships.OrderBy(t => t.StartDate));

            case TraineeShipSorts.EndDate:
                return(traineeships.OrderBy(t => t.EndDate));

            default:
                throw new ArgumentOutOfRangeException
                          (nameof(sortBy), sortBy, null);
            }
        }