Ejemplo n.º 1
0
        // POST: api/Meals
        public IHttpActionResult PostMeal(AddMealBindingModel model)
        {
            var userId = this.User.Identity.GetUserId();
            if (userId == null)
            {
                return this.Unauthorized();
            }

            var restaurant = db.Restaurants.FirstOrDefault(r => r.Id == model.RestaurantId);
            if (restaurant == null)
            {
                return this.NotFound();
            }

            if (userId != restaurant.OwnerId)
            {
                return this.Unauthorized();
            }

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

            var meal = new Meal()
            {
                Name = model.Name,
                Price = model.Price,
                RestaurantId = model.RestaurantId,
                TypeId = model.TypeId
            };

            db.Meals.Add(meal);
            db.SaveChanges();

            var mvm = new MealViewModel()
            {
                Id = meal.Id,
                Name = meal.Name,
                Price = meal.Price,
                Type = db.MealTypes
                    .Where(mt => mt.Id == meal.TypeId)
                    .Select(mt => mt.Name)
                    .FirstOrDefault()
            };

            return CreatedAtRoute("DefaultApi", new { id = mvm.Id }, mvm);
        }
        public IHttpActionResult EditExistingMeal(int id, ExistingMealBindingModel model)
        {
            if (model == null)
            {
                return BadRequest();
            }

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

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

            var currUserId = User.Identity.GetUserId();
            if (meal.Restaurant.OwnerId != currUserId)
            {
                return Unauthorized();
            }

            var currType = this.db.MealTypes.All().FirstOrDefault(t => t.Id == model.TypeId);
            meal.Name = model.Name;
            meal.Price = model.Price;
            meal.Type = currType;

            this.db.Meals.Update(meal);
            this.db.SaveChanges();

            var mealViewModel = new MealViewModel()
            {
                Id = meal.Id,
                Name = meal.Name,
                Price = meal.Price,
                Type = meal.Type.Name
            };

            return this.Created("http://localhost:1337/api/meals/" + meal.Id, mealViewModel);
        }
        public IHttpActionResult PostMeal(MealBindingModel model)
        {
            if (model == null)
            {
                return BadRequest();
            }

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

            var currRestaurant = this.db.Restaurants.All().FirstOrDefault(r => r.Id == model.RestaurantId);
            var currMealType = this.db.MealTypes.All().FirstOrDefault(m => m.Id == model.TypeId);

            var meal = new Meal()
            {
                Name = model.Name,
                Price = model.Price,
                Restaurant = currRestaurant,
                Type = currMealType
            };

           
            db.Meals.Add(meal);
            db.SaveChanges();

            var mealViewModel = new MealViewModel()
            {
                Id = meal.Id,
                Name = meal.Name,
                Price = meal.Price,
                Type = meal.Type.Name
            };

            return this.Created("http://localhost:1337/api/meals/" + meal.Id, mealViewModel);
        }
Ejemplo n.º 4
0
        // PUT: api/Meals/5
        public IHttpActionResult PutMeal(int id, EditMealBindingModel model)
        {
            var userId = this.User.Identity.GetUserId();
            var restaurant = db.Restaurants.FirstOrDefault(r => r.OwnerId == userId);

            if (userId == null || restaurant == null)
            {
                return this.Unauthorized();
            }

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

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

            meal.Name = model.Name;
            meal.Price = model.Price;
            meal.TypeId = model.TypeId;
            db.SaveChanges();

            var mvm = new MealViewModel()
            {
                Id = meal.Id,
                Name = meal.Name,
                Price = meal.Price,
                Type = db.MealTypes
                    .Where(mt => mt.Id == meal.TypeId)
                    .Select(mt => mt.Name)
                    .FirstOrDefault()
            };

            return this.Ok(mvm);            
        }