public IHttpActionResult CreateOrder(int id, [FromBody] CreateOrderBindingModel model)
        {
            var meal = this.Data.Meals.Find(id);
            if (meal == null)
            {
                return this.NotFound();
            }
            if (model == null)
            {
                return this.BadRequest("Model cannot be null");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }
            string loggedUserId = this.User.Identity.GetUserId();
            var order = new Order()
            {
                Quantity = (int)model.Quantity,
                MealId = id,
                UserId = loggedUserId,
                CreatedOn = DateTime.Now,
                OrderStatus = OrderStatus.Delivered
            };

            this.Data.Orders.Add(order);
            this.Data.SaveChanges();

            return this.Ok();
        }
        public IHttpActionResult CreateOrder(int id, [FromBody] OrderBindingModel model)
        {
            if (model == null)
            {
                return this.BadRequest("Model cannot be null (no data in request)");
            }

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

            var meal = this.Data.Meals.Find(id);
            if (meal == null)
            {
                return this.NotFound();
            }

            var loggedUserId = this.User.Identity.GetUserId();
            var user = this.Data.Users.Find(loggedUserId);
            if (user == null)
            {
                return this.Unauthorized();
            }

            var order = new Order()
            {
                Meal = meal,
                MealId = id,
                CreatedOn = DateTime.Now,
                Quantity = model.Quantity,
                User = user,
                UserId = loggedUserId
            };

            this.Data.Orders.Add(order);
            this.Data.SaveChanges();

            return this.Ok();
        }
        public IHttpActionResult PostOrder(int id, OrderBindingModel orderModel)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return Unauthorized();
            }

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

            if (!db.Meals.All().Any(r => r.Id == id))
            {
                return NotFound();
            }

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

            Order order = new Order()
            {
                Meal = db.Meals.Find(id),
                MealId = id,
                CreatedOn = DateTime.Now,
                User = currentUser,
                UserId = currentUserId,
                Quantity = orderModel.Quantity,
                OrderStatus = OrderStatus.Pending
            };

            db.Orders.Add(order);
            db.SaveChanges();

            return this.Ok();
        }
        public IHttpActionResult CreateOrder(int id, OrderBindingModel orderBindingModel)
        {
            var userId = this.User.Identity.GetUserId();

            if (orderBindingModel == null)
            {
                return this.BadRequest("Data is missing");
            }

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

            var meal = this.Data.Meals.GetAll().FirstOrDefault(m => m.Id == id);
            if (meal == null)
            {
                return this.NotFound();
            }

            var newOrder = new Order
                {
                    MealId = meal.Id, 
                    Quantity = orderBindingModel.Quantity, 
                    UserId = userId, 
                    CreatedOn = DateTime.Now, 
                    OrderStatus = OrderStatus.Pending
                };

            this.Data.Orders.Add(newOrder);
            this.Data.SaveChanges();

            return this.Ok(string.Format("Your order number is #{0}", newOrder.Id));
        }
        public IHttpActionResult CreateOrder(int mealId, OrderBindingModel orderModel)
        {
            if (orderModel == null)
            {
                return this.BadRequest("Order data cannot be null.");
            }

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

            var existingMeal = this.Data.Meals.All()
                .FirstOrDefault(r => r.Id == mealId);
            if (existingMeal == null)
            {
                return this.NotFound();
            }

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

            var newOrder = new Order
            {
                Quantity = orderModel.Quantity,
                MealId = mealId,
                Meal = existingMeal,
                UserId = loggedUserId,
                CreatedOn = DateTime.Now,
                OrderStatus = OrderStatus.Pending
            };

            this.Data.Orders.Add(newOrder);
            this.Data.SaveChanges();

            return this.Ok(string.Format("Order with Id {0} is now pending.", newOrder.Id));
        }
        public IHttpActionResult CreateOrder(int id, [FromBody]OrderBindingModel model)
        {
            if (model == null)
            {
                return BadRequest("Missing order data.");
            }

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

            var meal = db.Meals.Find(id);

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

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

            var order = new Order()
            {
                MealId = meal.Id,
                Quantity = model.Quantity,
                UserId = currentUserId,
                OrderStatus = OrderStatus.Pending,
                CreatedOn = DateTime.Now,
            };

            db.Orders.Add(order);
            db.SaveChanges();

            return this.Ok();

        }
Ejemplo n.º 7
0
        public IHttpActionResult PostOrderByMealId(int id, CreateOrderBindingModel model)
        {
            var userId = this.User.Identity.GetUserId();
            if (userId == null)
            {
                return this.Unauthorized();
            }

            var meal = db.Meals.Find(id);
            if (meal == null)
            {
                return this.NotFound();
            }

            if (!this.ModelState.IsValid || model == null)
            {
                return this.BadRequest(this.ModelState);
            }

            var order = new Order()
            {
                MealId = id,
                Quantity = model.Quantity,
                UserId = userId,
                OrderStatus = OrderStatus.Pending,
                CreatedOn = DateTime.Now
            };

            db.Orders.Add(order);
            db.SaveChanges();

            return this.Ok();
        }
        public IHttpActionResult CreateOrders(int id, OrderBindingModel m)
        {
            if (User == null)
                return this.Unauthorized();

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

            if (!this.Data.Meals.Any(me => me.Id == id))
                return this.NotFound();

            var order = new Order
            {
                MealId = id,
                Quantity = m.Quantity,
                UserId = User.Identity.GetUserId(),
                CreatedOn = DateTime.Now,
                OrderStatus = OrderStatus.Pending
            };

            this.Data.Orders.Add(order);

            this.Data.SaveChanges();

            return this.Ok();
        }