Esempio n. 1
0
        public Object Post([FromBody] ProductFilters filters)
        {
            if (_productRepository.GetProducts().Count() == 0)
            {
                PopulateProducts();
            }

            List <Product> ps = _productRepository.GetProducts().ToList();

            if (filters.Title.Length > 0)
            {
                ps = ps.Where(p => p.Title.ToLower().Contains(filters.Title.ToLower())).ToList();
            }

            if (filters.CategoryId > 0)
            {
                ps = ps.Where(p => p.CategoryId == filters.CategoryId).ToList();
            }

            if (filters.PriceFrom > 0)
            {
                ps = ps.Where(p => p.Price >= filters.PriceFrom).ToList();
            }

            if (filters.PriceTo > 0)
            {
                ps = ps.Where(p => p.Price <= filters.PriceTo).ToList();
            }

            if (filters.ASC)
            {
                ps = ps.OrderBy(p => p.Price).ToList();
            }
            else
            {
                ps = ps.OrderByDescending(p => p.Price).ToList();
            }

            int pageCount = (int)Math.Ceiling((double)(ps.Count() / ItemsPerPage)) + 1;

            int skip = ItemsPerPage * (filters.Page - 1);

            bool canPage = skip < ps.Count();

            if (!canPage)
            {
                filters.Page = 1;
            }

            ps = ps.Skip(skip).Take(ItemsPerPage).ToList();

            return(Ok(new { Page = filters.Page, PageCount = pageCount, Products = ps }));
        }
        public Object Get()
        {
            var products = _productRepository.GetProducts();

            return(Ok(products));
        }