コード例 #1
0
        public IHttpActionResult CrateOffer(OfferInputModel offerData)
        {
            if (offerData == null)
            {
                return(BadRequest("Missing offer data."));
            }

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

            var currentUserId = User.Identity.GetUserId();
            var seller        = this.db.UserStore.FindByIdAsync(currentUserId).Result;

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

            var offer = new Offer()
            {
                Title              = offerData.Title,
                Description        = offerData.Description,
                DatePublished      = DateTime.Now,
                SellerId           = currentUserId,
                InitialPrice       = offerData.InitialPrice,
                ExpirationDateTime = offerData.ExpirationDateTime
            };

            db.Offers.Add(offer);
            db.SaveChanges();

            return(this.CreatedAtRoute("OfferDetails",
                                       new { id = offer.Id },
                                       new { offer.Id, Seller = seller.UserName, Message = "Offer created." }));
        }
コード例 #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." }));
        }