コード例 #1
0
        public IHttpActionResult CreateOffer(OfferBindingModel offerModel)
        {
            if (offerModel == null)
            {
                return this.BadRequest("Offer model cannot be null.");
            }

            var loggedUserId = this.User.Identity.GetUserId();
            var loggedUser = this.Data.Users.Find(loggedUserId);

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

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

            var newOffer = new Offer
            {
                Title = offerModel.Title,
                Description = offerModel.Description,
                InitialPrice = offerModel.InitialPrice,
                ExpirationDateTime = offerModel.ExpirationDateTime,
                SellerId = loggedUserId,
                Seller = loggedUser,
                PublishData = DateTime.Now,
                BidsCount = 0,
                Bids = new HashSet<Bid>()
            };

            loggedUser.Offers.Add(newOffer);
            this.Data.Users.Update(loggedUser);

            this.Data.Offers.Add(newOffer);
            this.Data.SaveChanges();

            return this.CreatedAtRoute(
                "DefaultApi",
                new
                {
                    Id = newOffer.Id,
                    Seller = loggedUser.UserName,
                    Message = "Successfully created offer."
                },
                new
                {
                    newOffer.Id,
                    newOffer.Title,
                    newOffer.Description,
                    newOffer.InitialPrice,
                    newOffer.ExpirationDateTime
                });
        }
コード例 #2
0
        public IHttpActionResult PostOffer(OfferBindingModel model)
        {
            if (model == null)
            {
                return BadRequest("Missing offer data.");
            }

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

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

            var offerToPost = new Offer()
            {
                Title = model.Title,
                Description = model.Description,
                SellerId = currentUserId,
                PublishDate = DateTime.Now,
                InitialPrice = model.InitialPrice,
                ExpirationDateTime = model.ExpirationDateTime
            };

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


            return this.CreatedAtRoute(
                                "DefaultApi",
                                new { controller = "offers/details", id = offerToPost.Id },
                                new
                                {
                                    Id = offerToPost.Id,
                                    Seller = offerToPost.Seller.UserName,
                                    Message = "Offer created."
                                });
            //return this.CreatedAtRoute("OfferDetails",
            //    new { id = offer.Id },
            //    new { offer.Id, Seller = seller.UserName, Message = "Offer created." });
        }