Esempio n. 1
0
        public IActionResult ListAjax([FromBody] AjaxSearch ajaxData)
        {
            List <ProductWithImages> productAndImages = new List <ProductWithImages>();
            List <Product>           products;

            if (ajaxData.SubCategoryId != null)
            {
                products = new List <Product>(_repository.Products.Where(pScId => pScId.SubCategoryID == ajaxData.SubCategoryId).Where(h => h.HideFromUsers == false).OrderBy(pId => pId.ProductID));
            }
            else if (ajaxData.CategoryId != null)
            {
                List <SubCategory> subCategories =
                    new List <SubCategory>(_repository.SubCategories.Where(i => i.CategoryID == ajaxData.CategoryId).ToList());
                products = new List <Product>(_repository.Products
                                              .Where(pScId => subCategories.Any(sCId => pScId.SubCategoryID == sCId.SubCategoryID))
                                              .Where(h => h.HideFromUsers == false)
                                              .OrderBy(pId => pId.ProductID));
            }
            else
            {
                products = new List <Product>(_repository.Products.Where(h => h.HideFromUsers == false).OrderBy(p => p.ProductID));
            }

            List <Category> categories = _repository.GetCategoriesWithSubCategories();

            if (!string.IsNullOrEmpty(ajaxData.SearchString))
            {
                List <Product> searchByNameList        = new List <Product>(products.Where(n => n.Name.ToLower().Contains(ajaxData.SearchString.ToLower())));
                List <Product> searchByDescriptionList = new List <Product>(products.Where(d => d.Description.ToLower().Contains(ajaxData.SearchString.ToLower())));
                List <Product> searchBySubCategoryList = new List <Product>(products.Where(p =>
                                                                                           categories.Any(c => c.SubCategories.Any(sc => sc.SubCategoryID == p.SubCategoryID && sc.SubCategoryName.ToLower().Contains(ajaxData.SearchString.ToLower()))))).ToList();
                List <Product> searchByCategoryList = new List <Product>(products.Where(p =>
                                                                                        categories.Any(c => c.SubCategories.Any(sc => sc.SubCategoryID == p.SubCategoryID && c.CategoryName.ToLower().Contains(ajaxData.SearchString.ToLower()))))).ToList();
                products = searchByNameList.Union(searchByDescriptionList).Union(searchBySubCategoryList).Union(searchByCategoryList).ToList();
            }

            products = products.OrderBy(i => i.ProductID).Skip(ajaxData.FirstInOrder).Take(10).ToList();

            foreach (var product in products)
            {
                var images = new List <Image>(_repository.Images.Where(index => index.ProductID == product.ProductID));
                productAndImages.Add(new ProductWithImages
                {
                    Product = product,
                    Images  = images
                });
            }

            List <Share> shares = _shareRepository.Shares.Where(i =>
                                                                productAndImages.Any(id => i.ShareId == id.Product.ShareID)).ToList();
            AjaxProductList ajaxProductList = new AjaxProductList {
                Categories = categories, ProductAndImages = productAndImages.ToList(), CategoryId = ajaxData.CategoryId, SearchString = ajaxData.SearchString, SubCategoryId = ajaxData.SubCategoryId, Shares = shares
            };

            return(Json(ajaxProductList));
        }
Esempio n. 2
0
        public IActionResult SharesAjaxSearch([FromBody] AjaxSearch searchShaves)
        {
            if (searchShaves != null)
            {
                List <Product> products;
                if (searchShaves.SubCategoryId != null && searchShaves.SubCategoryId != 0)
                {
                    products = _productRepository.Products.Where(i => i.SubCategoryID == searchShaves.SubCategoryId)
                               .ToList();
                }
                else if (searchShaves.CategoryId != null && searchShaves.CategoryId != 0)
                {
                    List <Category> categories = _productRepository.GetCategoriesWithSubCategories();
                    products = _productRepository.Products.Where(p =>
                                                                 categories.Any(c =>
                                                                                c.CategoryID == searchShaves.CategoryId &&
                                                                                c.SubCategories.Any(sc => sc.SubCategoryID == p.SubCategoryID))).ToList();
                }
                else
                {
                    products = _productRepository.Products.ToList();
                }

                if (!string.IsNullOrEmpty(searchShaves.SearchString) && !string.IsNullOrWhiteSpace(searchShaves.SearchString))
                {
                    products = products.Where(p => p.Name.Contains(searchShaves.SearchString) || p.Description.Contains(searchShaves.SearchString)).ToList();
                }
                foreach (var product in products)
                {
                    product.Description   = null;
                    product.SubCategoryID = null;
                    product.Color         = null;
                    product.HideFromUsers = false;
                    product.Images        = null;
                    product.Price         = 0;
                    product.NewProduct    = false;
                    product.Size          = null;
                }
                return(Json(products));
            }
            return(Json(new List <Product>()));
        }