public virtual JsonResult SetQuantity(int id, int quantity)
        {
            var prod = _dbContext.Products.FirstOrDefault(x => x.ProductId == id);

            if (prod != null)
            {
                var emails = prod.ProductDemands;
                var oldQ   = prod.Quantity;
                prod.Quantity = quantity;
                _dbContext.Products.AddOrUpdate(prod);
                if (oldQ < quantity)
                {
                    foreach (var item in emails)
                    {
                        var email = new ProductDemandEmail
                        {
                            CallbackUrl = Url.Action(MVC.Sklep.SzczegółyProduktu(prod.ProductId), Request.Url.Scheme),
                            IconName    = prod.IconName,
                            Name        = prod.Name,
                            To          = item.Email
                        };
                        email.Send();
                    }
                    _dbContext.ProductDemands.RemoveRange(emails);
                }
                _dbContext.SaveChanges();
                return(Json(true, JsonRequestBehavior.AllowGet));
            }
            return(Json(false, JsonRequestBehavior.AllowGet));
        }
        public virtual ActionResult Edytuj(ProductEditModel model)
        {
            var product = _appRepository.GetSingle <Products>(x => x.ProductId == model.Id);

            if (ModelState.IsValid)
            {
                var category = model.Category == 0 ? null : model.Category;
                var price    = model.Price == null ? (decimal?)null : Convert.ToDecimal(model.Price.Replace(".", ","));
                var weight   = model.Weight == null
                    ? (decimal?)null
                    : Math.Floor((Convert.ToDecimal(model.Weight.Replace(".", ","))) * 100) / 100;
                if (price == null || category == null || model.Packing == null || weight == null)
                {
                    if (model.PublishAfterCreate)
                    {
                        ModelState.AddModelError("PublishAfterCreate",
                                                 "Cena, kategoria, waga oraz sposób pakowania muszą być podane przed publikacją!");
                        GetCategoryListEdit(product);
                        return(View(model));
                    }
                }
                if (price != null)
                {
                    price = Math.Floor(((decimal)price) * 100) / 100;
                }
                if (model.SubCategory != 0)
                {
                    category = model.SubCategory;
                }


                product.Price       = price;
                product.CategoryId  = category;
                product.Description = model.Description;
                product.Name        = model.Title;
                product.Weight      = weight;
                if (product.Quantity < model.Quantity)
                {
                    var emails = _appRepository.GetAll <ProductDemands>(x => x.ProductId == product.ProductId).ToList();
                    foreach (var item in emails)
                    {
                        var email = new ProductDemandEmail
                        {
                            CallbackUrl = Url.Action(MVC.Sklep.SzczegółyProduktu(product.ProductId), Request.Url.Scheme),
                            IconName    = product.IconName,
                            Name        = product.Name,
                            To          = item.Email
                        };
                        email.Send();
                    }
                    _dbContext.ProductDemands.RemoveRange(emails);
                }
                product.Quantity  = model.Quantity;
                product.Packing   = model.Packing;
                product.Published = model.PublishAfterCreate;
                product.Promotion = model.Promotion;
                if (model.TrueImageDeleted)
                {
                    product.IconName  = null;
                    product.ImageName = null;
                }

                if (model.ImageBig != null && model.ImageBig.ContentLength != 0)
                {
                    var pathForSaving = Server.MapPath("~/Content/Images/Shop/");
                    var imageBig      = new ImageJob(model.ImageBig, pathForSaving + product.ProductId + "_normal",
                                                     new Instructions("maxwidth=700&maxheight=700&format=jpg"))
                    {
                        CreateParentDirectory = true,
                        AddFileExtension      = true
                    };
                    imageBig.Build();
                    var imageSmall = new ImageJob(model.ImageBig, pathForSaving + product.ProductId + "_small",
                                                  new Instructions("maxwidth=127&maxheight=127&format=jpg"))
                    {
                        CreateParentDirectory = true,
                        AddFileExtension      = true
                    };
                    imageSmall.Build();
                    product.IconName  = product.ProductId + "_small";
                    product.ImageName = product.ProductId + "_normal";
                }
                product.WrongModel = false;
                product.IsComplete = true;
                foreach (
                    var prop in product.GetType().GetProperties().Where(prop => prop.GetValue(product, null) == null))
                {
                    product.IsComplete = false;
                }
                if (product.Price == null || product.CategoryId == null || product.Packing == null)
                {
                    product.WrongModel = true;
                    product.IsComplete = false;
                    product.Published  = false;
                }
                _dbContext.Products.AddOrUpdate(product);
                _dbContext.SaveChanges();
                return(RedirectToAction(MVC.Admin.Produkty.Lista(product.Name)));
            }
            ViewBag.SelectedOpt = 1;
            GetCategoryListEdit(product);
            return(View(model));
        }