public ActionResult AddNewAuction()
        {
            //Getting list of categories
            List <Category> categories = db.Categories.Where(x => x.Visible).ToList();
            //Getting list of currencies
            List <Currency> currencies = db.Currencies.Where(x => x.Visible).ToList();

            ViewBag.Currency = new SelectList(currencies, "Id", "Name");

            //Displaying categories according to current website language
            switch (SiteLanguages.GetCurrentLanguageCulture())
            {
            case "en-US":
                ViewBag.Category = new SelectList(categories.OrderBy(x => x.Category_Name).ToList(), "Id", "Category_Name");
                break;

            case "ar-SA":
                ViewBag.Category = new SelectList(categories.OrderBy(x => x.Category_Name_Ar).ToList(), "Id", "Category_Name_Ar");
                break;

            default:
                ViewBag.Category = new SelectList(categories.OrderBy(x => x.Category_Name).ToList(), "Id", "Category_Name");
                break;
            }

            AddAuctionModel model = new AddAuctionModel()
            {
                AccountNumber = db.Users.Find(User.Identity.GetUserId()).AccountNumber
            };


            return(View(model));
        }
        public async Task <ActionResult> Create(AddAuctionModel auctionModel)
        {
            if (ModelState.IsValid)
            {
                var    folder = Guid.NewGuid();
                string photos = null;
                foreach (var file in auctionModel.Photos)
                {
                    if (file != null && file.ContentLength > 0)
                    {
                        var    filename = Guid.NewGuid();
                        string ext      = Path.GetExtension(file.FileName);
                        string path     = $"/upload/{folder}/";
                        Directory.CreateDirectory(Server.MapPath(path));
                        path   = $"{path}{filename}{ext}";
                        photos = $"{photos}:{path}";
                        file.SaveAs(Server.MapPath(path));
                    }
                }
                photos = photos?.Remove(0, 1);

                BllLot lot = new BllLot()
                {
                    AuctionEndDate = auctionModel.AuctionEndDate,
                    Categorie      = int.Parse(auctionModel.Categorie),
                    CreationDate   = DateTime.Now,
                    CurrentPrice   = auctionModel.StartPrice,
                    Description    = auctionModel.Description,
                    LastUpdateDate = DateTime.Now,
                    Photos         = photos,
                    Seller         = await userService.GetId(User.Identity.Name),
                    Title          = auctionModel.Title
                };

                int auctionId = await auctionService.Create(lot);

                return(RedirectToAction("Details", new { id = auctionId }));
            }
            return(View(auctionModel));
        }
예제 #3
0
        public async Task <IActionResult> Create(AddAuctionModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            Image image;

            using (BinaryReader reader = new BinaryReader(model.file.OpenReadStream())) {
                image = new Image()
                {
                    data = reader.ReadBytes(Convert.ToInt32(reader.BaseStream.Length))
                };
            }

            DateTime creationDateTime = DateTime.Now;

            if (DateTime.Compare(model.openingDateTime, model.closingDateTime) >= 0)
            {
                ModelState.AddModelError("", "Opening and/or closing date are not valid");
                return(View(model));
            }

            if (DateTime.Compare(creationDateTime, model.openingDateTime) >= 0 || DateTime.Compare(creationDateTime, model.closingDateTime) >= 0)
            {
                ModelState.AddModelError("", "Opening and/or closing date are not valid");
                return(View(model));
            }

            User user = await this.userManager.GetUserAsync(base.User);

            Auction auction = new Auction()
            {
                name             = model.name,
                description      = model.description,
                imageId          = image.id,
                image            = image,
                startingPrice    = model.startingPrice,
                creationDateTime = creationDateTime,
                openingDateTime  = model.openingDateTime,
                closingDateTime  = model.closingDateTime,
                state            = Auction.AuctionState.DRAFT,
                accession        = 0,
                userId           = user.Id,
                user             = user
            };

            if (user.auctionList == null)
            {
                user.auctionList = new List <Auction>();
            }

            user.auctionList.Add(auction);

            this._context.Users.Update(user);
            await this._context.images.AddAsync(image);

            await this._context.auctions.AddAsync(auction);

            await this._context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
        public ActionResult AddNewAuction(AddAuctionModel model, HttpPostedFileBase[] uploadFile)
        {
            try
            {
                //If no image uploaded, then save a default image
                string path     = "~/Images/Items/no-thumbnail.png";
                string fileName = "";

                //Creating a list of images
                List <ProductPhoto> Images = new List <ProductPhoto>();

                //Uploading multiple images
                if (uploadFile[0] != null)
                {
                    try
                    {
                        foreach (HttpPostedFileBase file in uploadFile)
                        {
                            if (file != null && checkFileType(file.FileName))
                            {
                                fileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + "_" + file.FileName;
                                path     = "~/Images/Items/" + fileName;
                                file.SaveAs(Server.MapPath(path));
                                Images.Add(new ProductPhoto()
                                {
                                    Path = path
                                });
                            }
                        }
                    }
                    catch
                    {
                        ViewBag.ImageSizeError = Resource.ImageUploadError;
                        return(View(model));
                    }
                }
                else
                {
                    Images.Add(new ProductPhoto()
                    {
                        Path = path
                    });
                }
                //Creating a new product
                Product product = new Product()
                {
                    Name        = model.Product.Name,
                    Description = model.Product.Description,
                    Currency    = db.Currencies.Find(Convert.ToInt32(Request["Currency"].ToString())),
                    Price       = model.Product.Price,
                    Category    = db.Categories.Find(Convert.ToInt32(Request["Category"].ToString())),
                    Status      = Request["StatusRadio"].ToString()
                };
                //Setting the product images
                foreach (var p in Images)
                {
                    p.Product = product;
                }

                product.Images = Images;

                //Adding the new product to DB
                db.Products.Add(product);


                ApplicationUser seller = db.Users.Find(User.Identity.GetUserId());
                //Creating a new auction
                Auction auction = new Auction()
                {
                    Seller      = seller,
                    Buyer       = null,
                    Product     = product,
                    Current_Bid = model.Product.Price,
                    Start_Date  = DateTime.Now,
                    Finish_Date = DateTime.Now.AddDays(model.DurationDays).AddHours(model.DurationHrs),
                    IsPaid      = false
                };
                seller.AccountNumber = model.AccountNumber;

                //Adding the auction to DB
                db.Auctions.Add(auction);

                db.SaveChanges();

                return(RedirectToAction("MyAuctions", "Auction"));
            }
            catch { }

            List <Category> categories = db.Categories.ToList();
            List <Currency> currencies = db.Currencies.ToList();

            //Displaying categories according to current website language
            switch (SiteLanguages.GetCurrentLanguageCulture())
            {
            case "en-US":
                ViewBag.Category = new SelectList(categories.OrderBy(x => x.Category_Name).ToList(), "Id", "Category_Name");
                break;

            case "ar-SA":
                ViewBag.Category = new SelectList(categories.OrderBy(x => x.Category_Name_Ar).ToList(), "Id", "Category_Name_Ar");
                break;

            default:
                ViewBag.Category = new SelectList(categories.OrderBy(x => x.Category_Name).ToList(), "Id", "Category_Name");
                break;
            }
            ViewBag.Currency = new SelectList(currencies, "Id", "Name");

            return(View(model));
        }