Exemple #1
0
        public ActionResult DeleteConfirmed(int id)
        {
            Product product = db.Products.Find(id);

            db.Products.Remove(product);
            db.SaveChanges();
            UpdateProductsHub.BroadcastData();
            return(RedirectToAction("Index"));
        }
Exemple #2
0
        public ActionResult Edit([Bind(Include = "ID, Name, Description, ProductImage, Price, OldPrice, CategoryID, ManufacturerID, Offer, Discount")] Product product, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                ViewBag.CategoryID     = new SelectList(db.Categories, "ID", "Name", product.CategoryID);
                ViewBag.ManufacturerID = new SelectList(db.Manufacturers, "ID", "Name", product.ManufacturerID);
                if (product.Discount == null && product.Offer)
                {
                    product.Offer = false;
                    ModelState.AddModelError("Offer", "Discount cannot be empty.");
                    return(View(product));
                }

                if (file != null && file.ContentLength > 0)
                {
                    string completePath = Server.MapPath(product.ProductImage);
                    string path         = Path.Combine(Server.MapPath("~/Images"),
                                                       Path.GetFileName(file.FileName));
                    file.SaveAs(path);
                    product.ProductImage = path.Replace(path, ("~/Images/") + file.FileName);

                    if (path != completePath)
                    {
                        System.IO.File.Delete(completePath);
                    }
                }

                // OM: Switch prices between old and new according to discount and in case price is editied while on discount
                if (product.Offer && (product.OldPrice == 0))
                {
                    product.OldPrice = product.Price;
                    product.Price    = (decimal)(product.Price - (product.Price * (decimal)product.Discount / 100m));
                }
                // OM: if offer is true, then the old price property is taken in the View
                else if (product.Offer && product.Price / (decimal)(product.Discount / 100m) != product.OldPrice)
                {
                    product.Price = (decimal)(product.OldPrice - (product.OldPrice * (decimal)product.Discount / 100));
                }
                else if (!product.Offer && product.OldPrice > 0)
                {
                    product.Price    = product.OldPrice;
                    product.OldPrice = 0;
                }
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();

                // OM: update prices that are already in cart
                UpdateCartPrices(product);

                UpdateProductsHub.BroadcastData();
                return(RedirectToAction("Details", new { id = product.ID }));
            }
            ViewBag.CategoryID     = new SelectList(db.Categories, "ID", "Name", product.CategoryID);
            ViewBag.ManufacturerID = new SelectList(db.Manufacturers, "ID", "Name", product.ManufacturerID);
            return(View(product));
        }
Exemple #3
0
        public ActionResult DeleteConfirmed(int id)
        {
            Product product      = db.Products.Find(id);
            string  completePath = Server.MapPath(product.ProductImage);

            if (System.IO.File.Exists(completePath))
            {
                System.IO.File.Delete(completePath);
            }
            db.Products.Remove(product);
            db.SaveChanges();
            UpdateProductsHub.BroadcastData();
            return(RedirectToAction("Index"));
        }
Exemple #4
0
        public ActionResult Create([Bind(Include = "ID, Name, Description, ProductImage, Price, CategoryID, ManufacturerID")] Product product)
        {
            product.ProductImage = "~/Images/" + product.ProductImage;
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();
                UpdateProductsHub.BroadcastData();
                return(RedirectToAction("Index"));
            }

            ViewBag.CategoryID     = new SelectList(db.Categories, "ID", "Name", product.CategoryID);
            ViewBag.ManufacturerID = new SelectList(db.Manufacturers, "ID", "Name", product.ManufacturerID);
            return(View(product));
        }
Exemple #5
0
        public ActionResult Edit([Bind(Include = "ID, Name, Description, ProductImage, Price, OldPrice, CategoryID, ManufacturerID, Offer, Discount")] Product product)
        {
            if (ModelState.IsValid)
            {
                ViewBag.CategoryID     = new SelectList(db.Categories, "ID", "Name", product.CategoryID);
                ViewBag.ManufacturerID = new SelectList(db.Manufacturers, "ID", "Name", product.ManufacturerID);
                if (product.Discount == null && product.Offer)
                {
                    product.Offer = false;
                    ModelState.AddModelError("Offer", "Discount cannot be empty.");
                    return(View(product));
                }

                // OM: Switch prices between old and new according to discount and in case price is editied while on discount
                if (product.Offer && (product.OldPrice == 0))
                {
                    product.OldPrice = product.Price;
                    product.Price    = (decimal)(product.Price - (product.Price * (decimal)product.Discount / 100m));
                }
                // OM: if offer is true, then the old price property is taken in the View
                else if (product.Offer && product.Price / (decimal)(product.Discount / 100m) != product.OldPrice)
                {
                    product.Price = (decimal)(product.OldPrice - (product.OldPrice * (decimal)product.Discount / 100));
                }
                else if (!product.Offer && product.OldPrice > 0)
                {
                    product.Price    = product.OldPrice;
                    product.OldPrice = 0;
                }

                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();

                // OM: update prices that are already in cart
                UpdateCartPrices(product);

                UpdateProductsHub.BroadcastData();
                return(RedirectToAction("Details", new { id = product.ID }));
            }
            ViewBag.CategoryID     = new SelectList(db.Categories, "ID", "Name", product.CategoryID);
            ViewBag.ManufacturerID = new SelectList(db.Manufacturers, "ID", "Name", product.ManufacturerID);
            return(View(product));
        }
Exemple #6
0
        public ActionResult Create([Bind(Include = "Name, Description, ProductImage, Price, CategoryID, ManufacturerID")] Product product, HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                string path = Path.Combine(Server.MapPath("~/Images"),
                                           Path.GetFileName(file.FileName));
                file.SaveAs(path);
                product.ProductImage = path.Replace(path, ("~/Images/") + file.FileName);
            }
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();
                UpdateProductsHub.BroadcastData();
                return(RedirectToAction("Index"));
            }

            ViewBag.CategoryID     = new SelectList(db.Categories, "ID", "Name", product.CategoryID);
            ViewBag.ManufacturerID = new SelectList(db.Manufacturers, "ID", "Name", product.ManufacturerID);
            return(View(product));
        }