Exemplo n.º 1
0
        public ProductsListJsonViewModel GetSpecifyProductIntro(string category, string searchString = "", int page = 1)
        {
            IEnumerable <Product> products = repository.Products;

            ProcessCategoryString(ref category);
            //if (category != "ALL")    //為了單元測試,使用CategoryID來判斷
            //    products = products.Where(p => p.Category.Name == category);
            if (category != "ALL")
            {
                products = products.Where(p => p.CategoryID == GetIdOfCategory(category));
            }
            if (searchString != null && searchString != "")
            {
                products = products.Where(p => p.Name.ToLower().Contains(searchString.ToLower()));
            }
            int totalPage = (int)Math.Ceiling((decimal)products.Count() / PageSize);

            products = products.Skip((page - 1) * PageSize).Take(PageSize);

            List <string> cateList = new List <string>()
            {
                "ALL"
            };

            cateList.AddRange(repository.Categories.Select(c => c.Name));

            ProductsListJsonViewModel data = new ProductsListJsonViewModel {
                Products = products.Select(p => new ProductIntroViewModel {
                    ProductUrl     = "Home/ProductDetail/productID=" + p.ID.ToString(),
                    Name           = p.Name,
                    Price          = p.Price.ToString(),
                    CoverImagePath = p.CoverImagePath,
                }).ToList(),
                CategoryList = cateList,
                TotalPage    = totalPage
            };

            return(data);
        }