Exemple #1
0
        public IHttpActionResult Post(DishCreate dish)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            if (!dishService.CreateDish(dish))
            {
                return(InternalServerError());
            }


            return(Ok());
        }
        public bool CreateDish(DishCreate model)
        {
            try
            {
                bool allWentWell = false;
                var  entity      =
                    new Dish()
                {
                    Name = model.Name,
                };

                using (var ctx = new ApplicationDbContext())
                {
                    ctx.Dishes.Add(entity);
                    var success = ctx.SaveChanges() == 1;

                    foreach (var ingredientInDish in model.IngredientsInDish)
                    {
                        var ingredient =
                            new IngredientInDishCreate()
                        {
                            DishID       = entity.DishID,
                            IngredientID = Convert.ToInt32(ingredientInDish)
                        };
                        var succeeded = CreateIngredientInDish(ingredient);
                        //break the code or decide what you'll do if succeeded == false;
                        // if succeeded is false, return allWentWell
                    }
                    allWentWell = true;
                }
                return(allWentWell);
            }
            catch (Exception e)
            {
                return(false);
            }
        }