示例#1
0
        public IActionResult AddProductPage()
        {
            AddProductPageW vMod = new AddProductPageW();

            vMod.ListOfCategories = dbContext.Categories.ToList();
            return(View(vMod));
        }
示例#2
0
        public IActionResult ProductDetailsPage(int id)
        {
            AddProductPageW vMod = new AddProductPageW();

            vMod.ListOfCategories = dbContext.Categories.ToList();
            vMod.Product          = dbContext.Products.FirstOrDefault(p => p.ProductId == id);
            return(View(vMod));
        }
示例#3
0
        public IActionResult addProduct(AddProductPageW formData)
        {
            AddProductPageW vMod = new AddProductPageW();

            vMod.ListOfCategories = dbContext.Categories.ToList();
            if (ModelState.IsValid)
            {
                if (dbContext.Products.Any(p => p.ProductName == formData.Product.ProductName))
                {
                    ModelState.AddModelError("Product.ProductName", "You cannot create a Product with the same name as an existing Product.");
                    return(View("AddProductPage", vMod));
                }
                //getting category id
                int catId = Int32.Parse(formData.CategoryName);
                if (formData.Product.Image != null)
                {
                    //pulling right gategory from DB
                    Category cat = dbContext.Categories.FirstOrDefault(c => c.CategoryId == catId);
                    //saving initial filename
                    var uniqueFileName = formData.Product.Image.FileName;
                    //Setting my path to start from wwwroot
                    string myPath = hostingEnvironment.WebRootPath.Substring(hostingEnvironment.WebRootPath.Length - 7);
                    //Setting path to images folder and right category folder
                    var uploads = Path.Combine(myPath, "Images", cat.Name);
                    //if there is no such folder, crate it
                    if (!Directory.Exists(uploads))
                    {
                        Directory.CreateDirectory(uploads);
                    }
                    //finalizing path and filename
                    var filePath = Path.Combine(uploads, uniqueFileName);
                    //saving to local folder and creatin a stream
                    var stream = new FileStream(filePath, FileMode.Create);
                    formData.Product.Image.CopyTo(stream);
                    //saving in db only needed part of path for example "Images\Hats\<FileName>.jpg"
                    formData.Product.ImageUrl = filePath.Substring(8);
                    //Closing stream to not to have an error with FileOI system
                    stream.Close();
                }
                else
                {
                    ModelState.AddModelError("Product.Image", "Image cannot be empty");
                    return(View("AddProductPage", vMod));
                }
                formData.Product.CategoryId = catId;
                dbContext.Products.Add(formData.Product);
                dbContext.SaveChanges();

                return(RedirectToAction("AdminDash"));
            }
            System.Console.WriteLine("================================");
            // only if there is an error

            return(View("AddProductPage", vMod));
        }
示例#4
0
        public IActionResult EditProduct(int id, AddProductPageW fromForm)
        {
            System.Console.WriteLine("============================");
            System.Console.WriteLine(id);
            System.Console.WriteLine("============================");
            int catId = fromForm.Product.CategoryId;

            if (ModelState.IsValid)
            {
                if (dbContext.Products.Any(b => b.ProductName == fromForm.Product.ProductName && b.ProductId != id))
                {
                    ModelState.AddModelError("Name", "You cannot create a Product with the same name as an existing Product.");
                    return(View("ProductDetailsPage", fromForm));
                }

                if (fromForm.Product.Image != null)
                {
                    //pulling right gategory from DB
                    Category cat = dbContext.Categories.FirstOrDefault(c => c.CategoryId == catId);
                    //saving initial filename
                    var uniqueFileName = fromForm.Product.Image.FileName;
                    //Setting my path to start from wwwroot
                    string myPath = hostingEnvironment.WebRootPath.Substring(hostingEnvironment.WebRootPath.Length - 7);
                    //Setting path to images folder and right category folder
                    var uploads = Path.Combine(myPath, "Images", cat.Name);
                    //if there is no such folder, crate it
                    if (!Directory.Exists(uploads))
                    {
                        Directory.CreateDirectory(uploads);
                    }
                    //finalizing path and filename
                    var filePath = Path.Combine(uploads, uniqueFileName);
                    //saving to local folder and creatin a stream
                    var stream = new FileStream(filePath, FileMode.Create);
                    fromForm.Product.Image.CopyTo(stream);
                    //saving in db only needed part of path for example "Images\Hats\<FileName>.jpg"
                    fromForm.Product.ImageUrl = filePath.Substring(8);
                    //Closing stream to not to have an error with FileOI system
                    stream.Close();
                }
                if (fromForm.Product.Image == null)
                {
                    dbContext.Entry(fromForm.Product).Property("ImageUrl").IsModified = false;
                }


                Product toUpdate = dbContext.Products.FirstOrDefault(p => p.ProductId == id);
                toUpdate.CategoryId  = fromForm.Product.CategoryId;
                toUpdate.ProductName = fromForm.Product.ProductName;
                toUpdate.Description = fromForm.Product.Description;
                toUpdate.Quantity    = fromForm.Product.Quantity;
                toUpdate.ProductId   = id;
                if (fromForm.Product.Image != null)
                {
                    toUpdate.ImageUrl = fromForm.Product.ImageUrl;
                }
                // fromForm.Product.ProductId = id;
                // dbContext.Products.Update(fromForm.Product);
                // dbContext.Entry(fromForm.Product).Property("CreatedAt").IsModified = false;
                dbContext.Products.Update(toUpdate);
                dbContext.SaveChanges();

                return(RedirectToAction("AdminDash", fromForm));
            }

            AddProductPageW vMod = new AddProductPageW();

            vMod.ListOfCategories = dbContext.Categories.ToList();
            vMod.Product          = dbContext.Products.FirstOrDefault(p => p.ProductId == id);
            return(View("ProductDetailsPage", vMod));
        }