コード例 #1
0
        public ActionResult UpdateProduct(int Id)
        {
            //Declare AuctionVM

            AuctionVM model;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                //Get the product

                Auction_Product product = db.Auction_Product.Find(Id);

                //make sure product exists
                if (product == null)
                {
                    return(Content("This product does not exist"));
                }

                //init model

                model = new AuctionVM(product);



                //get all gallery images

                model.GalleryImages = Directory.EnumerateFiles(Server.MapPath("~/Images/Uploads/Products/" + Id))
                                      .Select(fn => Path.GetFileName(fn));
            }
            return(View(model));
        }
コード例 #2
0
        public JsonResult ItemBids(int?id)
        {
            AuctionVM vm = new AuctionVM
            {
                //Find the Item with the id
                VmItem = db.Items.Find(id)
            };

            string jsonObj = "";

            //Checks if the Item has a bid.
            if (vm.VmItem.Bids.Count > 0)
            {
                //Item ID that has a bid
                int pid = vm.VmItem.Bids.FirstOrDefault().ItemID;

                //Assign it to the bid
                vm.VMBid = db.Items.SelectMany(x => x.Bids)
                           .Where(i => i.ItemID == pid)
                           .OrderBy(t => t.TimeStamp)
                           .ToList();

                jsonObj = JsonConvert.SerializeObject(vm.VMBid);
            }

            //Send it back
            return(Json(jsonObj, JsonRequestBehavior.AllowGet));
        }
コード例 #3
0
        public ActionResult Add_Auction_Product()
        {
            //Intial Model

            AuctionVM model = new AuctionVM();


            return(View(model));
        }
コード例 #4
0
        private AuctionVM CreateAuctionVM()
        {
            AuctionVM auction = new AuctionVM
            {
                Seller = db.Sellers.ToList(),
                Item   = db.Items.ToList(),
                Buyer  = db.Buyers.ToList(),
                Bid    = db.Bids.ToList()
            };

            return(auction);
        }
コード例 #5
0
        // GET: Auction/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            AuctionVM vm = new AuctionVM()
            {
                VmItem = db.Items.Find(id)
            };

            if (vm.VmItem == null)
            {
                return(HttpNotFound());
            }

            return(View(vm));
        }
コード例 #6
0
        public ActionResult AuctionDetailsProduct(int Id)
        {
            AuctionVM model;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Auction_Product auction_Product = db.Auction_Product.Find(Id);

                if (auction_Product.LastPrice == null)
                {
                    auction_Product.LastPrice = auction_Product.StartingPrice;
                }
                else
                {
                    auction_Product.LastPrice = auction_Product.LastPrice;
                }


                //make sure product exists
                if (auction_Product == null)
                {
                    return(Content("That product does not exist"));
                }

                //init model

                model = new AuctionVM(auction_Product);


                //get all gallery images

                model.GalleryImages = Directory.EnumerateFiles(Server.MapPath("~/Images/Uploads/Products/" + Id))
                                      .Select(fn => Path.GetFileName(fn));
            }
            return(View(model));
        }
コード例 #7
0
        public ActionResult Add_Auction_Product(AuctionVM model, HttpPostedFileBase file)
        {
            //check model state
            if (!ModelState.IsValid)
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    return(View(model));
                }
            }

            //declare product id
            int id;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Auction_Product product = new Auction_Product();

                //product.ProductCode = model.ProductCode;
                product.Name            = model.Name;
                product.Description     = model.Description;
                product.CountryOfOrgin  = model.CountryOfOrgin;
                product.Details         = model.Details;
                product.StartingPrice   = model.StartingPrice;
                product.LastPrice       = model.LastPrice;
                product.AdminId         = User.Identity.GetUserId();
                product.Auction_Started = DateTime.Now;
                product.Auction_Ended   = model.Auction_Ended;
                db.Auction_Product.Add(product);
                db.SaveChanges();
                //get id
                id = product.Id;
            }

            TempData["SM"] = "You have addedd a product sussecfully";


            #region
            //Create necessary directiories

            var OriginalDirectorey = new DirectoryInfo(string.Format("{0}Images\\Uploads", Server.MapPath(@"\")));

            var PathString1 = Path.Combine(OriginalDirectorey.ToString(), "Products");
            var PathString2 = Path.Combine(OriginalDirectorey.ToString(), "Products\\" + id.ToString());
            var PathString3 = Path.Combine(OriginalDirectorey.ToString(), "Products\\" + id.ToString() + "\\Thumbs");
            var PathString4 = Path.Combine(OriginalDirectorey.ToString(), "Products\\" + id.ToString() + "\\Gallery");
            var PathString5 = Path.Combine(OriginalDirectorey.ToString(), "Products\\" + id.ToString() + "\\Gallery\\Thumbs");



            if (!Directory.Exists(PathString1))
            {
                Directory.CreateDirectory(PathString1);
            }


            if (!Directory.Exists(PathString2))
            {
                Directory.CreateDirectory(PathString2);
            }


            if (!Directory.Exists(PathString3))
            {
                Directory.CreateDirectory(PathString3);
            }


            if (!Directory.Exists(PathString4))
            {
                Directory.CreateDirectory(PathString4);
            }


            if (!Directory.Exists(PathString5))
            {
                Directory.CreateDirectory(PathString5);
            }


            //Create if a file was upload
            if (file != null && file.ContentLength > 0)
            {
                //Get file Extention
                string ext = file.ContentType.ToLower();


                if (ext != "image/jpg" &&
                    ext != "image/jpeg" &&
                    ext != "image/pjpeg" &&
                    ext != "image/gif" &&
                    ext != "image/x-png" &&
                    ext != "image/png")
                {
                    using (ApplicationDbContext db = new ApplicationDbContext())
                    {
                        ModelState.AddModelError("", "The Image was not uplaoded - wrong image extintion");
                        return(View(model));
                    }
                }

                string ImageName = file.FileName;

                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    Auction_Product prod = db.Auction_Product.Find(id);
                    prod.ImageName = ImageName;
                    db.SaveChanges();
                }

                //set original and thumb image pathss
                var path  = string.Format("{0}\\{1}", PathString2, ImageName);
                var path2 = string.Format("{0}\\{1}", PathString3, ImageName);

                //save original
                file.SaveAs(path);

                //create and save thumbs
                WebImage image = new WebImage(file.InputStream);

                image.Resize(200, 200);
                image.Save(path2);
            }
            #endregion
            //redirect
            return(RedirectToAction("ListOfAuctionProducts"));
        }
コード例 #8
0
        public ActionResult UpdateProduct(AuctionVM model, HttpPostedFileBase file)
        {
            //get product id
            int id = model.Id;


            model.GalleryImages = Directory.EnumerateFiles(Server.MapPath("~/Images/Uploads/Products/" + id))
                                  .Select(fn => Path.GetFileName(fn));


            //check model state

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            //make sure product name isunique

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                if (db.Auction_Product.Where(x => x.Id != id).Any(x => x.Name == model.Name))
                {
                    ModelState.AddModelError("", " Product name is taken Please Enter Another One!");
                    return(View(model));
                }
            }
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Auction_Product product = db.Auction_Product.Find(id);

                product.Name            = model.Name;
                product.Description     = model.Description;
                product.CountryOfOrgin  = model.CountryOfOrgin;
                product.Details         = model.Details;
                product.StartingPrice   = model.StartingPrice;
                product.LastPrice       = model.LastPrice;
                product.Auction_Started = DateTime.Now;
                product.Auction_Ended   = model.Auction_Ended;



                if (model.ImageName != null)
                {
                    product.ImageName = model.ImageName;
                }

                db.SaveChanges();
            }

            //set tempdata message
            TempData["MM"] = "Product Updated Please Reload The Page ";


            #region Image Upload
            //Create if a file was upload
            if (file != null && file.ContentLength > 0)
            {
                //Get file Extention
                string ext = file.ContentType.ToLower();


                if (ext != "image/jpg" &&
                    ext != "image/jpeg" &&
                    ext != "image/pjpeg" &&
                    ext != "image/gif" &&
                    ext != "image/x-png" &&
                    ext != "image/png")
                {
                    using (ApplicationDbContext db = new ApplicationDbContext())
                    {
                        ModelState.AddModelError("", "The Image was not uplaoded - wrong image extintion");
                        return(View(model));
                    }
                }


                var OriginalDirectorey = new DirectoryInfo(string.Format("{0}Images\\Uploads", Server.MapPath(@"\")));

                var PathString1 = Path.Combine(OriginalDirectorey.ToString(), "Products\\" + id.ToString());
                var PathString2 = Path.Combine(OriginalDirectorey.ToString(), "Products\\" + id.ToString() + "\\Thumbs");


                //Delete files from directories

                DirectoryInfo di1 = new DirectoryInfo(PathString1);
                DirectoryInfo di2 = new DirectoryInfo(PathString2);

                foreach (FileInfo file1 in di1.GetFiles())
                {
                    file1.Delete();
                }

                foreach (FileInfo file2 in di2.GetFiles())
                {
                    file2.Delete();
                }


                //save images name

                string imageName = file.FileName;

                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    Auction_Product product = db.Auction_Product.Find(id);
                    product.ImageName = imageName;
                    db.SaveChanges();
                }
                //save original and thumb images

                var path  = string.Format("{0}\\{1}", PathString1, imageName);
                var path2 = string.Format("{0}\\{1}", PathString2, imageName);

                //save original
                file.SaveAs(path);


                //create and save thumbs
                WebImage image = new WebImage(file.InputStream);

                image.Resize(200, 200);
                image.Save(path2);
            }
            #endregion
            //Redirect

            return(RedirectToAction(nameof(ListOfAuctionProducts)));
        }