Ejemplo n.º 1
0
        public ActionResult Create(CreateAuctionForm form)
        {
            // Validate the model. If that fails, return the user to the original form.
            if (!TryValidateModel(form))
            {
                return View(form);
            }

            Auction newAuction = null;

            // Save the new auction in the database
            using (var context = new AuctioneerContext())
            {
                newAuction = new Auction
                {
                    Name = form.Name,
                    EndDate = form.EndDate,
                    Description = form.Description
                };

                context.Auctions.Add(newAuction);
                context.SaveChanges();
            }

            // Redirect to the details page
            return RedirectToAction("Details", new { id = newAuction.Id });
        }
Ejemplo n.º 2
0
        private static void SaveBid(int auctionId, decimal amount)
        {
            using (var context = new AuctioneerContext())
            {
                var foundAuction = context.Auctions.FirstOrDefault(auction => auction.Id == auctionId);

                // Can't accept bids for auctions that don't exist anymore!
                if (foundAuction == null)
                {
                    throw new ArgumentException("The specified auction does not exist", "auctionId");
                }

                // Can't accept bids for auctions that have been closed or sold
                if (foundAuction.EndDate < DateTime.Now)
                {
                    throw new InvalidOperationException("This auction has already been closed.");
                }

                decimal highestBid = 0;

                if (foundAuction.Bids.Count > 0)
                {
                    highestBid = foundAuction.Bids.Max(bid => bid.Amount);
                }

                // Only higher bids are allowed for the auction
                if (highestBid >= amount)
                {
                    throw new ArgumentException("The placed bid isn't higher than the previous bid", "amount");
                }

                Bid newBid = new Bid()
                {
                    Amount = amount,
                    Auction = foundAuction,
                    DatePosted = DateTime.Now,
                    PostedBy = HttpContext.Current.User.Identity.Name
                };

                // Save the new bid in the database
                context.Bids.Add(newBid);
                foundAuction.Bids.Add(newBid);

                using (var transaction = new TransactionScope())
                {
                    context.SaveChanges();
                    transaction.Complete();
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Displays the details for an existing auction
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(int id)
        {
            Auction foundAuction = null;

            // Retrieve the auction from the database
            using (var context = new AuctioneerContext())
            {
                foundAuction = context.Auctions
                    .Include("Bids")
                    .FirstOrDefault(auction => auction.Id == id);
            }

            if (foundAuction == null)
            {
                // A true website does not display data that does not exist.
                // So instead throw an error and let the rest of the infrastructure handle the mess.
                throw new HttpException(404, "Auction not found");
            }

            return View(foundAuction);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Displays all the running auctions
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            List<Auction> auctions = null;

            using (var context = new AuctioneerContext())
            {
                auctions = context.Auctions
                    .OrderByDescending(auction => auction.EndDate)
                    .ToList();
            }

            return View(auctions);
        }