public IHttpActionResult BidForOffer(int id, [FromBody]BidBindingModel model)
        {
            if (model == null)
            {
                return BadRequest("Missing bid data.");
            }

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

            Offer offerDB = db.Offers.FirstOrDefault(o => o.Id == id);

            if (offerDB == null)
            {
                return NotFound();
            }

            var currentUserId = User.Identity.GetUserId();
            var currentUser = this.db.Users.Find(currentUserId);

            var maxOfferPrice = offerDB.InitialPrice;

            if (offerDB.Bids.Any())
            {
                maxOfferPrice = offerDB.Bids.Max(b => b.BidPrice);
            }

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

            if (offerDB.ExpirationDateTime < DateTime.Now)
            {
                return this.Content(HttpStatusCode.BadRequest,
                  new { Message = "Offer has expired." });
            }

            var bid = new Bid()
            {
                BidPrice = model.BidPrice,
                OfferId = offerDB.Id,
                BidderId = currentUserId,
                DateCreated = DateTime.Now,
                Comment = model.Comment
            };

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

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

        }
Ejemplo n.º 2
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." });
        }
        public IHttpActionResult BidForOffer(int id, BidBindingModel bidModel)
        {
            if (bidModel == null)
            {
                return this.BadRequest("Bid data is null.");
            }

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

            var loggedUserId = this.User.Identity.GetUserId();
            var loggedUser = this.Data.Users.All()
                .FirstOrDefault(u => u.Id.Equals(loggedUserId));

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

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

            if (existingOffer.ExpirationDateTime < DateTime.Now)
            {
                return this.BadRequest("Cannot bid for expired offer.");
            }

            if (bidModel.BidPrice < existingOffer.InitialPrice)
            {
                return this.BadRequest(string.Format("Your bid should be > {0}", existingOffer.InitialPrice));
            }

            if (existingOffer.Bids.Any())
            {
                var highestBidPrice = existingOffer.Bids.Max(b => b.Price);
                if (bidModel.BidPrice < highestBidPrice)
                {
                    return this.BadRequest(string.Format("Your bid should be > {0}", highestBidPrice));
                }
            }

            var newBid = new Bid
            {
                Price = bidModel.BidPrice,
                BidDate = DateTime.Now,
                Bidder = loggedUser,
                BidderId = loggedUserId,
                Comment = bidModel.Comment,
                Offer = existingOffer,
                OfferId = existingOffer.Id
            };

            this.Data.Bids.Add(newBid);
            existingOffer.Bids.Add(newBid);
            this.Data.Offers.Update(existingOffer);

            this.Data.SaveChanges();

            return this.Ok(new
            {
                Id = newBid.Id,
                Bidder = loggedUser.UserName,
                Message = string.Format("Successfully bidded for offer with Id: {0}.", existingOffer.Id)
            });
        }
        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."
            });
        }
        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."
            });


        }
        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."
            });
        }
        public IHttpActionResult PostBidByOfferId(int id, PostBidBindingModel model)
        {
            if (model == null)
            {
                return this.BadRequest("Data cannot be empty.");
            }

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

            var offer = this.Data.Offers
                .Where(o => o.Id == id)
                .Select(o => new
                {
                    Id = o.Id,
                    InitialPrice = o.InitialPrice,
                    ExpirationDate = o.ExpirationDate,
                    Bids = o.Bids.Select(b => b.Price)
                })
                .FirstOrDefault();

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

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

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

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

            if (!this.User.Identity.IsAuthenticated)
            {
                return this.Unauthorized();
            }

            var currentUserId = this.User.Identity.GetUserId();
            var bid = new Bid()
            {
                BidderId = currentUserId,
                Price =  model.BidPrice,
                Comment = model.Comment,
                DateCreated = DateTime.Now,
                OfferId = offer.Id

            };

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

            return this.Ok(new
            {
                Id = bid.Id,
                Bidder = this.User.Identity.GetUserName(),
                Message = "Bid created."
            });
        }
        public IHttpActionResult CreateNewBidForExistingOffer([FromUri] int id, [FromBody] BidBindingModel model)
        {
            if (model == null)
            {
                return BadRequest();
            }

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

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

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

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

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

            var currUserId = User.Identity.GetUserId();
            var bid = new Bid()
            {
                OfferedPrice = model.BidPrice,
                Comment = model.Comment,
                DateCreate = DateTime.Now,
                OfferId = offer.Id,
                BidderId = currUserId
            };


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

            return Ok(new
            {
                bid.Id,
                Bidder = bid.Bidder.UserName,
                Message = "Bid created."
            });
        }
 public IHttpActionResult AddNewBid(int id, [FromBody]AddBidBindingModel model)
 {
     var loggedUserId = User.Identity.GetUserId();
     var user = this.Data.Users.Find(loggedUserId);
     var offer = this.Data.Offers.Find(id);
     if (offer == null)
     {
         return this.NotFound();
     }
     if (!this.ModelState.IsValid || model == null)
     {
         return this.BadRequest();
     }
     if (offer.ExpirationhDate < DateTime.Now)
     {
         return this.BadRequest("Offer has expired.");
     }
     var maxBidPrice = offer.Bids.Where(b => b.BidPrice > model.BidPrice).Max(b=>b.BidPrice);
     if (model.BidPrice <= offer.InitialPrice)
     {
         return this.BadRequest(string.Format("Your bid should be > {0}.", offer.InitialPrice));
     }
     if (maxBidPrice>0)
     {
         return this.BadRequest(string.Format("Your bid should be > {0}.", maxBidPrice));
     }
     var newBid = new Bid()
     {
         BidPrice = model.BidPrice,
         Comment = model.Comment,
         BidDate = DateTime.Now,
         BidderId = loggedUserId,
         OfferId = id
     };
     this.Data.Bids.Add(newBid);
     this.Data.SaveChanges();
     return this.Ok(new { Id = newBid.Id, Bidder = user.UserName, message = "Bid created." });
 }