public bool UpdateDish(DishEdit model) { using (var ctx = new ApplicationDbContext()) { try { var entity = ctx .Dishes .Single(e => e.DishID == model.DishID); if (entity == null) { return(false); } var iidEntity = ctx .IngredientsInDish .Select(e => e.DishID == model.DishID); foreach (var oldIngredients in ctx.IngredientsInDish) { var happy = ctx.IngredientsInDish.Remove(oldIngredients); } ctx.SaveChanges(); var succeed = ctx.IngredientsInDish.Count() == 0; entity.Name = model.Name; foreach (var newIngredients in model.IngredientsInDish) { var ingredient = new IngredientInDishCreate() { DishID = entity.DishID, IngredientID = newIngredients }; var succeeded = CreateIngredientInDish(ingredient); } ctx.SaveChanges(); } catch (Exception e) { return(false); } } return(true); }
public bool CreateIngredientInDish(IngredientInDishCreate ingredientModel) { var entity = new IngredientInDish() { DishID = ingredientModel.DishID, IngredientID = ingredientModel.IngredientID, }; using (var ctx = new ApplicationDbContext()) { ctx.IngredientsInDish.Add(entity); return(ctx.SaveChanges() == 1); } }
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); } }