Ejemplo n.º 1
0
        // GET: Item
        //[HttpGet]
        public IActionResult Index(ToyListViewModel model, string showAs, string display, string search, string sortBy,
                                   int?filterBy, int?page)
        {
            model.Categories = FormHelper.GetFilterFormCategories(_categoryRepository.GetCategories());
            model.Sort       = FormHelper.GetFormSortBy();

            ViewData["showAs"]   = showAs;
            ViewData["search"]   = search;
            ViewData["display"]  = display;
            ViewData["filterBy"] = filterBy;
            ViewData["sortBy"]   = sortBy;

            IQueryable <Toy> Toys;

            if (!String.IsNullOrEmpty(search))
            {
                page = 1;
                Toys = _toyRepository.GetToysContaining(search);
            }
            else
            {
                switch (display)
                {
                case "wanted":
                    ViewBag.Section = "Wanted toys";
                    Toys            = _toyRepository.GetWantedToys();
                    break;

                case "collection":
                default:
                    ViewBag.Section = "My collection";
                    Toys            = _toyRepository.GetMyToys();
                    break;
                }
            }

            if (filterBy != null && filterBy != 0)
            {
                Toys = Toys.Where(x => x.Category.Id == filterBy);

                if (HttpContext.Request.Method == "POST")
                {
                    page = 1;
                }
            }

            if (!String.IsNullOrEmpty(sortBy))
            {
                FormHelper.SortBy sort = (FormHelper.SortBy)Enum.Parse(typeof(FormHelper.SortBy), sortBy);
                switch (sort)
                {
                default:
                case FormHelper.SortBy.Name:
                    Toys = Toys.OrderBy(s => s.Name);
                    break;

                case FormHelper.SortBy.Producer:
                    Toys = Toys.OrderBy(s => s.Producer.Name);
                    break;

                case FormHelper.SortBy.Category:
                    Toys = Toys.OrderBy(s => s.Category.Name);
                    break;
                }
                if (HttpContext.Request.Method == "POST")
                {
                    page = 1;
                }
            }

            ViewData["Page"]       = page;
            ViewData["TotalItems"] = Toys.Count();

            page = (page - 1) ?? 0;

            model.Toys       = new PaginatedList <Toy>(Toys, page.Value);
            model.TotalPrice = Toys.Sum(x => x.Price);
            return(View(model));
        }