public IHttpActionResult PostOffer(PostOfferBindingModel model)
        {
            if (model == null)
            {
                return this.BadRequest("Missing offer data.");
            }

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

            var sellerId = this.User.Identity.GetUserId();
            var seller = this.Data.Users.All().FirstOrDefault(u => u.Id == sellerId);
            if (seller == null)
            {
                return this.Unauthorized();
            }

            var newOffer = new Offer
            {
                Title = model.Title,
                Description = model.Description,
                DatePublished = DateTime.Now,
                InitialPrice = model.InitialPrice,
                ExpirationDate = model.ExpirationDateTime,
                SellerId = sellerId
            };

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

            return this.CreatedAtRoute(
                "DefaultApi",
                new
                {
                    Controller = "offers/details",
                    Id = newOffer.Id
                },
                new
                {
                    Id = newOffer.Id, 
                    Seller = newOffer.Seller.UserName, 
                    Message = "Offer created."
                });
        }
        public IHttpActionResult PostOffer(OfferInputModel offerData)
        {
            if (!ModelState.IsValid || offerData == null)
            {
                return this.BadRequest(ModelState);
            }

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


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

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

            var currentUserName = User.Identity.GetUserName();
            return this.CreatedAtRoute(
                "PostOffer",
                new { id = offer.Id },
                new { id = offer.Id, Seller = currentUserName, Message = "Offer created." });
        }
        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
                });
        }
        public IHttpActionResult PostOffer(PostOfferBindigModel model)
        {
            if (model == null)
            {
                return this.BadRequest("Title, InitialPrice and ExpirationDate cannot be empty");
            }

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

            var currentUserId = this.User.Identity.GetUserId();
            var offer = new Offer()
            {
                Title = model.Title,
                Description = model.Description,
                InitialPrice = model.InitialPrice,
                DateCreated = DateTime.Now,
                ExpirationDate = model.ExpirationDate,
                SellerId = currentUserId
            };

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

            return this.CreatedAtRoute(
                    "DefaultApi",
                    new { id = offer.Id },
                    new
                    {
                        offer.Id,
                        Seller = this.User.Identity.GetUserName(),
                        Message = "Offer created."
                    });
        }
        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." });
        }
        public IHttpActionResult PutOffer(int id, Offer offer)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != offer.Id)
            {
                return BadRequest();
            }

            

            return StatusCode(HttpStatusCode.NoContent);
        }
        public IHttpActionResult PostOffer(OfferBindingModel model)
        {
            if (model == null)
            {
                return BadRequest();
            }

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

            var currUserId = User.Identity.GetUserId();
            var currUser = db.Users.Find(currUserId);

            var offer = new Offer()
            {
                Title = model.Title,
                Description = model.Description,
                InitialPrice = model.InitialPrice,
                ExpirationDateTime = model.ExpirationDateTime,
                DatePublished = DateTime.Now,
                Saller = currUser
            };

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


            return this.Created("http://localhost:8888/api/offers/" + offer.Id, new
            {
                offer.Id,
                Saller = offer.Saller.UserName,
                Message = "Offer created."
            });
        }
        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." });
        }
        public IHttpActionResult PostOffer(OfferBindingModel offer)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (offer == null)
            {
                return BadRequest();
            }

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

            var newOffer = new Offer()
            {
                Title = offer.Title,
                ExpirationDate = offer.ExpirationDateTime,
                InitialPrice = offer.InitialPrice,
                PublishDate = DateTime.Now,
                Seller = currentUser,
                Description = offer.Description
            };

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

            return CreatedAtRoute("DefaultApi", new { id = newOffer.Id },
                new
                {
                    Id = newOffer.Id,
                    Seller = currentUser.UserName,
                    Message = "Offer created."
                });
        }
 public IHttpActionResult CreateNewOffer([FromBody]CreateNewOfferBindingModel model)
 {
     var loggedUserId = User.Identity.GetUserId();
     var user = this.Data.Users.Find(loggedUserId);
     if (!this.ModelState.IsValid)
     {
         return this.BadRequest();
     }
     if (model == null)
     {
         return this.BadRequest();
     }
     var newOffer = new Offer()
     {
         Title = model.Title,
         Description = model.Description,
         InitialPrice = model.InitialPrice,
         ExpirationhDate = model.ExpirationDate,
         PublishDate = DateTime.Now,
         SellerId = loggedUserId
     };
     this.Data.Offers.Add(newOffer);
     this.Data.SaveChanges();
     return this.CreatedAtRoute("OfferDetails", new { id = newOffer.Id },
             new { id = newOffer.Id, Seller = user.UserName, message = "Offer created." });
 }