예제 #1
0
        public IActionResult UpdateProduct(ExtentedProductModel model)
        {
            var product = unitOfWork.Products.GetAll()
                          .Include(p => p.ProductCategories)
                          .ThenInclude(pc => pc.Category)
                          .Where(p => p.ProductId == model.Product.ProductId)
                          .FirstOrDefault();

            if (product != null)
            {
                product.ProductName      = model.Product.ProductName;
                product.Price            = model.Product.Price;
                product.SoldAmount       = model.Product.SoldAmount;
                product.AvailableAmount  = model.Product.AvailableAmount;
                product.Gender           = model.Product.Gender;
                product.ShortDescription = model.Product.ShortDescription;
                product.Description      = model.Product.Description;
                product.DateAdded        = model.Product.DateAdded;
                product.IsHome           = model.Product.IsHome;
                product.BrandId          = model.BrandId;

                //foreach (var item in product.ProductCategories)
                //    unitOfWork.RemoveFromProductCategory(product.ProductId, item.CategoryId);

                product.ProductCategories.RemoveAll(pc => pc.ProductId == product.ProductId);

                List <ProductCategory> newProductCategories = new List <ProductCategory>();
                foreach (var id in model.CatgoriesIdArray)
                {
                    newProductCategories.Add(new ProductCategory()
                    {
                        Product    = product,
                        CategoryId = id
                    });
                }

                product.ProductCategories = newProductCategories;


                if (model.ProfilImageFile != null)
                {
                    var fileName = Path.Combine(hostingEnvironment.WebRootPath, "images/products/profil", Path.GetFileName(model.ProfilImageFile.FileName));
                    model.ProfilImageFile.CopyTo(new FileStream(fileName, FileMode.Create));
                    product.ProfilImage = Path.GetFileName(model.ProfilImageFile.FileName);
                }

                unitOfWork.SaveChanges();

                return(RedirectToAction("ProductList"));
            }
            else
            {
                ModelState.AddModelError("", "Product was not found");
            }

            FillSelectListsForUpdateProduct(model.Product, model.BrandSelectList, model.CategorySelectList);

            return(View(model));
        }
예제 #2
0
        public IActionResult AddProduct()
        {
            ExtentedProductModel model = new ExtentedProductModel();

            model.Product = new Product();
            model.Product.ProductCategories = new List <ProductCategory>();

            model.BrandSelectList    = new List <SelectListItem>();
            model.CategorySelectList = new List <SelectListItem>();

            FillSelectListsForUpdateProduct(model.Product, model.BrandSelectList, model.CategorySelectList);

            return(View(model));
        }
예제 #3
0
        public IActionResult AddProduct(ExtentedProductModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.ProfilImageFile != null)
                {
                    var fileName = Path.Combine(hostingEnvironment.WebRootPath, "images/products/profil", Path.GetFileName(model.ProfilImageFile.FileName));
                    model.ProfilImageFile.CopyTo(new FileStream(fileName, FileMode.Create));
                    model.Product.ProfilImage = Path.GetFileName(model.ProfilImageFile.FileName);

                    model.Product.BrandId           = model.BrandId;
                    model.Product.ProductCategories = new List <ProductCategory>();
                    model.Product.DateAdded         = DateTime.Now;

                    unitOfWork.Products.Add(model.Product);
                    unitOfWork.Products.Save();

                    List <ProductCategory> productCategories = new List <ProductCategory>();
                    foreach (var categoryId in model.CatgoriesIdArray)
                    {
                        productCategories.Add(new ProductCategory()
                        {
                            Product    = model.Product,
                            CategoryId = categoryId
                        });
                    }

                    model.Product.ProductCategories = productCategories;

                    model.Product.Features = new List <ProductFeature>();
                    unitOfWork.SaveChanges();

                    ViewBag.ProductId   = model.Product.ProductId;
                    ViewBag.ProductName = model.Product.ProductName;
                    return(RedirectToAction("ProductList"));
                }
                else
                {
                    ModelState.AddModelError("", "Profil image is required");
                }
            }
            return(View(model));
        }
예제 #4
0
        public IActionResult UpdateProduct(int id)
        {
            Product product = unitOfWork.Products.GetAll()
                              .Include(p => p.ProductCategories)
                              .ThenInclude(pc => pc.Category)
                              .Where(p => p.ProductId == id).FirstOrDefault();

            ExtentedProductModel model = new ExtentedProductModel()
            {
                Product            = product,
                BrandId            = product.BrandId,
                ProfilImageFile    = new FormFile(null, 0, 0, null, product.ProfilImage),
                BrandSelectList    = new List <SelectListItem>(),
                CategorySelectList = new List <SelectListItem>()
            };

            FillSelectListsForUpdateProduct(product, model.BrandSelectList, model.CategorySelectList);

            return(View(model));
        }