Exemplo n.º 1
0
        public async Task <IActionResult> Sort(SortInputModel sortModel, AllCarsModel input, SearchInputModel searchModle, int page = 1)
        {
            ICollection <CarAdsViewModel> ads;

            // this is true only when you go through pages
            if (searchModle.Make != 0)
            {
                input.SortInputModel = new SortInputModel {
                    OrderByYear = sortModel.OrderByYear, OrderByPrice = sortModel.OrderByPrice, SearchInputModel = searchModle
                };
            }

            var sortInputModel = new SortInputModel();

            if (input.SortInputModel.SearchInputModel == null)
            {
                ads = await this.homeService.GetAllAdsAsync();
            }
            else
            {
                ads = await this.homeService.GetAdsByCriteriaAsync(input.SortInputModel.SearchInputModel);

                this.ViewData["searchModel"]    = input.SortInputModel.SearchInputModel as SearchInputModel;
                this.ViewData["orderByYear"]    = input.SortInputModel.OrderByYear;
                this.ViewData["orderByPrice"]   = input.SortInputModel.OrderByPrice;
                sortInputModel.SearchInputModel = new SearchInputModel();
            }

            if (input.SortInputModel.OrderByYear == "2")
            {
                ads = ads.OrderBy(x => x.YearOfProduction).ToList();
            }
            else if (input.SortInputModel.OrderByYear == "1")
            {
                ads = ads.OrderByDescending(x => x.YearOfProduction).ToList();
            }
            else if (input.SortInputModel.OrderByPrice == "2")
            {
                ads = ads.OrderBy(x => x.Price).ToList();
            }
            else if (input.SortInputModel.OrderByPrice == "1")
            {
                ads = ads.OrderByDescending(x => x.Price).ToList();
            }

            var result = new AllCarsModel
            {
                AllCars        = ads,
                SortInputModel = sortInputModel,
                CurrentPage    = page,
                Action         = "Sort",
            };

            var count = result.AllCars.Count;

            result.PagesCount = (int)Math.Ceiling((double)count / GlobalConstants.ItemsPerPage);
            if (result.PagesCount == 0)
            {
                result.PagesCount = 1;
            }

            var output = this.homeService.Paging(result, GlobalConstants.ItemsPerPage, (page - 1) * GlobalConstants.ItemsPerPage);

            return(this.View("All", output));
        }
        public IActionResult Index(SortInputModel model, [FromForm] string specialData)
        {
            var repo = new MovieRepository();
            var data = repo.GetMovies();

            if (!string.IsNullOrEmpty(model.sort))
            {
                switch (model.sort)
                {
                case "id":
                    if (model.order == "asc")
                    {
                        data = data.OrderBy(m => m.Id).ToList();
                    }
                    else
                    {
                        data = data.OrderByDescending(m => m.Id).ToList();
                    }
                    break;

                case "title":
                    if (model.order == "asc")
                    {
                        data = data.OrderBy(m => m.Title).ToList();
                    }
                    else
                    {
                        data = data.OrderByDescending(m => m.Title).ToList();
                    }
                    break;

                case "releaseDate":
                    if (model.order == "asc")
                    {
                        data = data.OrderBy(m => m.ReleaseDate).ToList();
                    }
                    else
                    {
                        data = data.OrderByDescending(m => m.ReleaseDate).ToList();
                    }
                    break;

                case "genre":
                    if (model.order == "asc")
                    {
                        data = data.OrderBy(m => m.Genre).ToList();
                    }
                    else
                    {
                        data = data.OrderByDescending(m => m.Genre).ToList();
                    }
                    break;

                case "price":
                    if (model.order == "asc")
                    {
                        data = data.OrderBy(m => m.Price).ToList();
                    }
                    else
                    {
                        data = data.OrderByDescending(m => m.Price).ToList();
                    }
                    break;

                default:
                    break;
                }
            }
            return(View(data));
        }