public ActionResult Update(CreateProductModel model, int id)
        {
            bool isNotExist = _productsRepository.FindDuplicateByNameAndID(model.Name, id);

            if (isNotExist)
            {
                var categoryId = Convert.ToInt32(model.Category);
                var manufacturerId = Convert.ToInt32(model.Manufacturer);
                var price = model.Price.Contains(".")
                    ? Convert.ToDecimal(model.Price.Replace('.', ','))
                    : Convert.ToDecimal(model.Price);

                var newProduct = new Products(model.Name, model.Description, price, _categoryRepository.Get(categoryId),
                    _manufacturerRepository.Get(manufacturerId), model.Quantity, model.IsFeatured, model.IsRecent,
                    model.IsBestseller, model.ShortDescription) {ID = id};
                try
                {
                    _productsRepository.Update(newProduct);
                    TempData["success"] = String.Format("Produkt {0} został edytowany pomyślnie", newProduct.Name);
                }
                catch (Exception)
                {
                    throw new Exception();
                }
            }
            else
            {
                TempData["error"] = String.Format("Produkt o nazwie {0} już istnieje", model.Name);
            }

            return RedirectToAction("Update", new {id = id});
        }
        public ActionResult Create(CreateProductModel model)
        {
            bool isNotExist = _productsRepository.FindDuplicateByName(model.Name);

            if (isNotExist)
            {
                var categoryId = Convert.ToInt32(model.Category);
                var manufacturerId = Convert.ToInt32(model.Manufacturer);
                var price = model.Price.Contains(".")
                    ? Convert.ToDecimal(model.Price.Replace('.', ','))
                    : Convert.ToDecimal(model.Price);

                var newProduct = new Products(model.Name, model.Description ?? String.Empty, price,
                    _categoryRepository.Get(categoryId), _manufacturerRepository.Get(manufacturerId), model.Quantity,
                    model.IsFeatured, model.IsRecent, model.IsBestseller, model.ShortDescription);

                try
                {
                    _productsRepository.Save(newProduct);
                    if (model.Photo != null)
                    {
                        var target = new MemoryStream();
                        model.Photo.InputStream.CopyTo(target);
                        var data = target.ToArray();
                        var productImage = new ProductImages(model.Photo.FileName, data, newProduct);
                        _productImagesRepository.Save(productImage);
                    }

                    TempData["success"] = String.Format("Produkt {0} została utworzona pomyślnie",
                        newProduct.Name);
                }
                catch (Exception)
                {
                    throw new Exception();
                }

                return RedirectToAction("List");
            }
            TempData["error"] = String.Format("Produkt o nazwie {0} już istnieje", model.Name);
            return RedirectToAction("List");
        }
        public ActionResult Update(int id)
        {
            var product = _productsRepository.Get(id);
            var viewModel = new CreateProductModel()
            {
                Category = product.Category.Name,
                IsBestseller = product.IsBestSeller,
                Name = product.Name,
                Price = product.Price.ToString(),
                IsFeatured = product.IsFeatured,
                IsRecent = product.IsRecent,
                Manufacturer = product.Manufacturer.Name,
                Description = product.Description,
                Quantity = product.Quantity,
                ShortDescription = product.ShortDescription
            };

            var manufacturers =
                _manufacturerRepository.GetAll()
                    .Select(x => new SelectListItem() {Text = x.Name, Value = x.ID.ToString()})
                    .ToList();

            foreach (var manufacturer in manufacturers)
            {
                if (manufacturer.Text == viewModel.Manufacturer)
                    manufacturer.Selected = true;
            }

            ViewBag.Manufacturer = manufacturers;

            var categories =
                _categoryRepository.GetAll()
                    .Select(
                        x =>
                            new SelectListItem()
                            {
                                Text = x.Name,
                                Value = x.ID.ToString(),
                                Selected = x.Name == viewModel.Manufacturer
                            })
                    .ToList();

            foreach (var category in categories)
            {
                if (category.Text == viewModel.Category)
                    category.Selected = true;
            }
            ViewBag.ProductId = id;
            ViewBag.Category = categories;

            return View(viewModel);
        }