public ActionResult CreateAuction() { if (!hasAccess("user")) { return(new HttpNotFoundResult()); } var parameters = _context.SystemParameters.First(); var auction = new ViewModels.NewAuction { Duration = parameters.DefaultAuctionDuration }; return(View(auction)); }
public ActionResult CreateAuction(ViewModels.NewAuction auction) { if (!IsImage(auction.ImageFile.InputStream)) { ModelState.AddModelError("ImageFile", "You have to provide the correct image file."); return(View()); } if (auction.Price < 1 || auction.Duration < 1) { ModelState.AddModelError("", "Value of some field is either zero or negative."); return(View()); } auction.ImageFile.InputStream.Position = 0; byte[] imageData = null; using (var binaryReader = new BinaryReader(auction.ImageFile.InputStream)) { imageData = binaryReader.ReadBytes(auction.ImageFile.ContentLength); } var userID = Convert.ToInt32(Session["userID"].ToString()); var user = _context.Users.Where(m => m.ID == userID).FirstOrDefault(); var newAuction = new Models.Auction { AuctionImage = new Models.AuctionImage { Image = imageData }, DateCreated = System.DateTime.Now, Duration = auction.Duration, Name = auction.Name, Price = auction.Price, StartingPrice = auction.Price, TimeCreated = System.DateTime.Now.TimeOfDay, PostedByUser = user.ID, State = "Ready" }; _context.Auctions.Add(newAuction); _context.SaveChanges(); ViewData["success"] = true; return(View()); }