public IActionResult Index()
        {
            dataContext.Attributes.Include(x => x.AttributeSceme).Include(x => x.Product).Load();
            dataContext.Products.Include(x => x.Tags).Include(x => x.Attributes).Include(x => x.Pictures).Include(x => x.Category).Load();

            string[] wishlistMetanames = new string[0];
            if (HttpContext.Request.Cookies.ContainsKey("wishlist"))
            {
                wishlistMetanames = Request.Cookies["wishlist"].Split(",");
            }

            List <ProductModel> products = dataContext.Products.Where(x => wishlistMetanames.Any(n => n == x.MetaName)).ToList();

            JProductModel[] jdata = new JProductModel[products.Count];
            int             index = 0;

            foreach (ProductModel product in products)
            {
                jdata[index]          = new JProductModel();
                jdata[index].Name     = product.Name;
                jdata[index].Key      = product.MetaName;
                jdata[index].Tags     = string.Join(" ,", product.Tags);
                jdata[index].Prise    = product.Prise.ToString("#.##");
                jdata[index].InStock  = product.InStock;
                jdata[index].Category = product.Category.Name;
                jdata[index].Icon     = product.Pictures.First().ImageSource;
                jdata[index].Url      = "/catalog/" + product.Category.MetaName + "/" + product.MetaName;
                jdata[index].IsNew    = product.IsNew;

                if (product.Discount > 0)
                {
                    jdata[index].Discount = new JDiscountModel()
                    {
                        Amount = (int)product.Discount,
                        Prise  = product.DicountPrise.ToString("#.##")
                    };
                }

                index++;
            }

            return(Json(jdata));
        }
Example #2
0
        public IActionResult Products(string category, string s, int page, [FromQuery(Name = "filters[]")] string[] filters, decimal?minPrise, decimal?maxPrise, int pageCount = 18, int sort = 0)
        {
            var data = dataContext.Products.Include(x => x.Tags).Include(x => x.Comments).Include(x => x.Attributes).Include(x => x.Pictures).Include(x => x.Category).ToList();

            if (!string.IsNullOrEmpty(category) && category != "all")
            {
                data = data.Where(x => x.Category.MetaName == category).ToList();
            }

            decimal pminPrise = 0;
            decimal pmaxPrise = 0;

            if (data.Count > 0)
            {
                pminPrise = data.Min(x => x.DicountPrise);
                pmaxPrise = data.Max(x => x.DicountPrise);
            }

            if (filters.Length > 0)
            {
                data = data.Where(x => filters.Any(f => x.Attributes.Any(a => a.Value == f))).ToList();
            }

            if (!string.IsNullOrEmpty(s))
            {
                data = data.Where(
                    x => x.Name.Replace(" ", "").ToUpper().Contains(s.Replace(" ", "").ToUpper()) ||
                    x.Tags.Any(x => x.Name.Replace(" ", "").ToUpper().Contains(s.Replace(" ", "").ToUpper()))
                    ).ToList();
            }

            foreach (ProductModel p in data)
            {
                p.ResetCache();
            }

            if (sort == 0)
            {
                data = data.OrderByDescending(x => x.AvgRate).ToList();
            }

            if (minPrise is not null && maxPrise is not null)
            {
                data = data.Where(x => x.DicountPrise >= minPrise && x.DicountPrise <= maxPrise).ToList();
            }

            JCatalogModel catalogModel = new JCatalogModel();

            catalogModel.TotalPages    = (int)MathF.Ceiling(data.Count / (float)pageCount);
            catalogModel.ProductsCount = pageCount;
            catalogModel.TotalProducts = data.Count;
            catalogModel.MinPrise      = pminPrise;
            catalogModel.MaxPrise      = pmaxPrise;

            data = data.Skip(pageCount * (page - 1)).Take(pageCount).ToList();

            JProductModel[] jdata = new JProductModel[data.Count];
            int             index = 0;

            foreach (ProductModel product in data)
            {
                jdata[index]          = new JProductModel();
                jdata[index].Name     = product.Name;
                jdata[index].Key      = product.MetaName;
                jdata[index].Tags     = string.Join(" ,", product.Tags);
                jdata[index].Prise    = product.Prise.ToString("#.##");
                jdata[index].InStock  = product.InStock;
                jdata[index].Category = product.Category.Name;
                jdata[index].Icon     = product.Pictures.First().ImageSource;
                jdata[index].Url      = "/catalog/" + product.Category.MetaName + "/" + product.MetaName;
                jdata[index].IsNew    = product.IsNew;
                jdata[index].AvgRate  = product.AvgRate;

                if (product.Discount > 0)
                {
                    jdata[index].Discount = new JDiscountModel()
                    {
                        Amount = (int)product.Discount,
                        Prise  = product.DicountPrise.ToString("#.##")
                    };
                }

                index++;
            }

            catalogModel.Products    = jdata;
            catalogModel.CurrentPage = page;

            return(Json(catalogModel));
        }