Example #1
0
        public IHttpActionResult CreateBidToExistingOffer(int offerId, BidBindingModel bidModel)
        {
            if (bidModel == null)
            {
                return(this.BadRequest("Invalid bid price."));
            }

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

            var currentUserId = User.Identity.GetUserId();

            if (currentUserId == null)
            {
                return(this.Unauthorized());
            }

            var offer = db.Offers.FirstOrDefault(o => o.Id == offerId);

            if (offer == null)
            {
                return(this.NotFound());
            }

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

            if (offer.Bids.Count > 0)
            {
                var maxBidForOffer = offer.Bids.Max(b => b.OfferedPrice);
                if (bidModel.BidPrice <= maxBidForOffer)
                {
                    return(this.BadRequest("Your bid should be > " + maxBidForOffer));
                }
            }
            else
            {
                if (bidModel.BidPrice <= offer.InitialPrice)
                {
                    return(this.BadRequest("Your bid should be > " + offer.InitialPrice));
                }
            }

            var bid = new Bid()
            {
                BidderId     = currentUserId,
                DateCreated  = DateTime.Now,
                OfferId      = offer.Id,
                OfferedPrice = bidModel.BidPrice,
                Comment      = bidModel.Comment != null ? bidModel.Comment : null
            };

            offer.Bids.Add(bid);
            db.SaveChanges();

            return(this.Ok(new
            {
                Id = bid.Id,
                Bidder = User.Identity.GetUserName(),
                Message = "Bid created."
            }));
        }
Example #2
0
        public IHttpActionResult BidForOffer(int id, BidBindingModel model)
        {
            var loggedUserId = this.User.Identity.GetUserId();

            if (loggedUserId == null)
            {
                return(this.Unauthorized());
            }

            var userInDb = this.Data.Users.All()
                           .FirstOrDefault(u => u.Id == loggedUserId);

            if (userInDb == null)
            {
                return(this.Unauthorized());
            }

            var offerInDb = this.Data.Offers.All()
                            .FirstOrDefault(o => o.Id == id);

            if (offerInDb == null)
            {
                return(this.NotFound());
            }

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

            if (DateTime.Now > offerInDb.ExpirationDate)
            {
                return(this.Content(
                           HttpStatusCode.BadRequest,
                           new { Message = "Offer has expired." }));
            }

            if (model.BidPrice < offerInDb.InitialPrice)
            {
                return(this.Content(
                           HttpStatusCode.BadRequest,
                           new { Message = "Your bid should be > " + offerInDb.InitialPrice }));
            }

            if (offerInDb.Bids.Count > 0 && model.BidPrice <= offerInDb.Bids.Max(b => b.BidPrice))
            {
                return(this.Content(
                           HttpStatusCode.BadRequest,
                           new { Message = "Your bid should be > " + offerInDb.Bids.Max(b => b.BidPrice) }));
            }

            var newBid = new Bid
            {
                BidPrice = model.BidPrice,
                Comment  = model.Comment,
                BidderId = loggedUserId,
                Date     = DateTime.Now,
                OfferId  = offerInDb.Id
            };

            this.Data.Bids.Add(newBid);
            this.Data.SaveChanges();

            return(this.Ok(new
            {
                Id = newBid.Id,
                Bidder = newBid.Bidder.UserName,
                Message = "Bid created."
            }));
        }