コード例 #1
0
        public IHttpActionResult Bid(int id, BidInputModel bidData)
        {
            if (bidData == null)
            {
                return BadRequest("Missing bid data.");
            }

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var offer = this.db.Offers.All()
                .Where(o => o.Id == id)
                .Select(o => new
                {
                    InitialPrice = o.InitialPrice,
                    ExpirationDateTime = o.ExpirationDateTime,
                    Bids = o.Bids.Select(b => b.OfferedPrice)
                })
                .FirstOrDefault();
            if (offer == null)
            {
                return this.NotFound();
            }

            if (offer.ExpirationDateTime < DateTime.Now)
            {
                return this.BadRequest("Offer has expired.");
            }

            var maxBidPrice = offer.InitialPrice;
            if (offer.Bids.Any())
            {
                maxBidPrice = offer.Bids.Max();
            }

            if (bidData.BidPrice <= maxBidPrice)
            {
                return this.BadRequest("Your bid should be > " + maxBidPrice);
            }

            var currentUserId = User.Identity.GetUserId();
            var user = this.db.UserStore.FindByIdAsync(currentUserId).Result;
            if (user == null)
            {
                return this.Unauthorized();
            }

            var bid = new Bid()
            {
                OfferId = id,
                BidderId = currentUserId,
                OfferedPrice = bidData.BidPrice,
                Comment = bidData.Comment,
                DateCreated = DateTime.Now
            };
            db.Bids.Add(bid);
            db.SaveChanges();

            return this.Ok(
                new { bid.Id, Bidder = user.UserName, Message = "Bid created." });
        }
コード例 #2
0
        public IHttpActionResult PostBidById(int id, BidInputModel bidData)
        {
            if (!ModelState.IsValid || bidData == null)
            {
                return this.BadRequest(ModelState);
            }

            var bid = this.db.Offers.Find(id);
            if (bid == null)
            {
                return this.NotFound();
            }

            var currentUserId = User.Identity.GetUserId();
            if (currentUserId == null)
            {
                return this.Unauthorized();
            }

            var offerDate = this.db.Offers.Where(o => o.Id == id).Select(o => o.ExpirationDate).First();

            if (offerDate < DateTime.Now)
            {
                return this.BadRequest("Offer has expired.");
            }

            var bestBidPrice = this.db.Bids
                .OrderByDescending(b => b.Id)
                .Where(b => b.OfferId == id)
                .Select(b => b.BidPrice)
                .FirstOrDefault();


            if (bestBidPrice >= bidData.BidPrice)
            {
                return this.BadRequest("Your bid should be > " + bestBidPrice);
            }

            var bit = new Bid()
                          {
                              BidderId = currentUserId,
                              BidPrice = bidData.BidPrice,
                              Comment = bidData.Comment,
                              Date = DateTime.Now,
                              OfferId = id
                          };

            this.db.Bids.Add(bit);
            this.db.SaveChanges();

            return this.Ok(new
            {
                Id = bit.Id,
                Author = User.Identity.GetUserName(),
                Message = "Bid created."
            });


        }