Пример #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 });
        }
Пример #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();
                }
            }
        }