Esempio n. 1
0
        public IHttpActionResult PutMeal(int id, MealBindingModel mealModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

            if (!User.Identity.IsAuthenticated)
            {
                return(Unauthorized());
            }

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

            meal.Name  = mealModel.Name;
            meal.Price = mealModel.Price;
            meal.Type  = db.MealTypes.Find(mealModel.TypeId);

            db.SaveChanges();

            return(this.Ok(new MealViewModel()
            {
                Id = meal.Id,
                Name = meal.Name,
                Price = meal.Price,
                Type = meal.Type.Name
            }));
        }
Esempio n. 2
0
        public IHttpActionResult CreateNewMeal(MealBindingModel mealModel)
        {
            if (mealModel == null)
            {
                return(this.BadRequest("Invalid meal data."));
            }

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

            var mealType = db.MealTypes.FirstOrDefault(mt => mt.Id == mealModel.TypeId);

            if (mealType == null)
            {
                return(this.BadRequest("Invalid meal type id."));
            }

            var restaurant = db.Restaurants.FirstOrDefault(r => r.Id == mealModel.RestaurantId);

            if (restaurant == null)
            {
                return(this.BadRequest("Invalid restaurant id."));
            }

            var currentUserId = User.Identity.GetUserId();

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

            var meal = new Meal()
            {
                Name       = mealModel.Name,
                Price      = mealModel.Price,
                Restaurant = restaurant,
                Type       = mealType
            };

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

            return(CreatedAtRoute(
                       "DefaultApi",
                       new
            {
                id = meal.Id
            },
                       new MealOutputModel()
            {
                Id = meal.Id,
                Name = meal.Name,
                Price = meal.Price,
                Type = meal.Type.Name
            }));
        }
Esempio n. 3
0
        public IHttpActionResult CreateMeal([FromBody] MealBindingModel 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 restaurantId = model.RestaurantId;
            var restaurant   = this.Data.Restaurants.Find(restaurantId);
            var typeId       = model.TypeId;
            var mealType     = this.Data.MealTypes.Find(typeId);

            if (restaurant == null || mealType == null)
            {
                return(this.BadRequest("Both restaurant and type should exist in the database."));
            }

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

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


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

            var meal = new Meal()
            {
                Name         = model.Name,
                Price        = model.Price,
                Restaurant   = restaurant,
                RestaurantId = restaurantId,
                Type         = mealType,
                TypeId       = typeId
            };

            this.Data.Meals.Add(meal);
            this.Data.SaveChanges();

            return(this.CreatedAtRoute(
                       "DefaultApi",
                       new { controller = "meals", id = meal.Id },
                       new { id = meal.Id, name = meal.Name, price = meal.Price, type = meal.Type.Name }));
        }
        public IHttpActionResult EditMeal([FromUri] int id, [FromBody] MealBindingModel mealModel)
        {
            var meal = this.Data.Meals.Find(id);

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

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

            var type = this.Data.MealTypes.Find(mealModel.TypeId);

            if (type == null)
            {
                return(this.BadRequest("Invalid type id"));
            }

            var             userId = this.User.Identity.GetUserId();
            ApplicationUser user   = null;

            if (userId != null)
            {
                user = this.Data.Users.Find(userId);
            }

            if (user == null || userId != meal.Restaurant.OwnerId)
            {
                return(this.Unauthorized());
            }

            meal.Name   = mealModel.Name;
            meal.Price  = mealModel.Price;
            meal.TypeId = mealModel.TypeId;
            meal.Type   = type;

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

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

            return(this.Ok(mealView));
        }
Esempio n. 5
0
        public IHttpActionResult EditExistingMeal(int id, [FromBody] MealBindingModel 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();

            if (meal.Restaurant.OwnerId != loggedUserId)
            {
                return(this.Unauthorized());
            }

            var typeId   = model.TypeId;
            var mealType = this.Data.MealTypes.Find(typeId);

            if (mealType == null)
            {
                return(this.BadRequest("The meal type should exist in the database."));
            }

            meal.Name   = model.Name;
            meal.Price  = model.Price;
            meal.TypeId = model.TypeId;
            this.Data.SaveChanges();

            var result = this.Data.Meals.All()
                         .Where(m => m.Id == meal.Id)
                         .Select(m => new
            {
                id    = m.Id,
                name  = m.Name,
                price = m.Price,
                type  = m.Type.Name
            });

            return(this.Ok(result));
        }
Esempio n. 6
0
        public async Task <int> CreateMeal(MealBindingModel model)
        {
            try
            {
                var food = this.Mapper.Map <Food>(model);
                this.AssignProductsToMeal(food, model.Products);

                this.DbContext.Foods.Add(food);
                await this.DbContext.SaveChangesAsync();
            }
            catch
            {
                //failure
                return(0);
            }
            //success
            return(1);
        }
Esempio n. 7
0
        public async Task <IActionResult> AddMeal(MealBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                var products = this.storageService.GetAllProductsNames();
                this.ViewBag.Products = products;
                return(this.View());
            }
            var result = await this.storageService.CreateMeal(model);

            if (result == 0)
            {
                this.TempData[WebConstants.BadMessage] = string.Format(Messages.AddingFailureMessage, "Meal", model.Name);
            }
            else
            {
                this.TempData[WebConstants.GoodMessage] = string.Format(Messages.AddingSuccessMessage, "Meal", model.Name);
            }
            return(RedirectToAction("AddMeal", "Storage", new { Area = WebConstants.AdminArea }));
        }
Esempio n. 8
0
        public IHttpActionResult PostMeal(MealBindingModel mealModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (!db.Restaurants.All().Any(r => r.Id == mealModel.RestaurantId))
            {
                return(BadRequest());
            }

            if (!User.Identity.IsAuthenticated)
            {
                return(Unauthorized());
            }

            Meal newMeal = new Meal()
            {
                Name         = mealModel.Name,
                Price        = mealModel.Price,
                Restaurant   = db.Restaurants.Find(mealModel.RestaurantId),
                RestaurantId = mealModel.RestaurantId,
                Type         = db.MealTypes.Find(mealModel.TypeId),
                TypeId       = mealModel.TypeId
            };

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

            return(CreatedAtRoute(
                       "DefaultApi",
                       new { id = newMeal.Id },
                       new MealViewModel()
            {
                Id = newMeal.Id,
                Name = newMeal.Name,
                Price = newMeal.Price,
                Type = newMeal.Type.Name
            }));
        }
Esempio n. 9
0
        public IHttpActionResult PostNewMeal([FromBody] MealBindingModel model)
        {
            if (model == null)
            {
                return(this.BadRequest());
            }

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

            var restaurant = this.Data.Restaurants.GetById(model.RestaurantId);

            if (restaurant == null)
            {
                return(this.BadRequest("Restaurant with id " + model.RestaurantId + " does not exist!"));
            }

            var mealType = this.Data.MealTypes.GetById(model.TypeId);

            if (mealType == null)
            {
                return(this.BadRequest("Meal type with id " + model.TypeId + " does not exist!"));
            }

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

            restaurant.Meals.Add(meal);
            this.Data.SaveChanges();

            var viewModel = Mapper.Map <MealViewModel>(meal);

            return(this.Created("/api/meals/" + meal.Id, viewModel));
        }