예제 #1
0
        public IActionResult DeleteIngredient(int ingredientId)
        {
            if (!_ingredientRepository.IngredientExists(ingredientId))
            {
                return(NotFound());
            }

            var ingredientToDelete = _ingredientRepository.GetIngredient(ingredientId);

            if (_ingredientRepository.GetIngredientsOfARecipe(ingredientId).Count > 0)
            {
                ModelState.AddModelError("", $"Ingredient {ingredientToDelete.Name}  " +
                                         "cannot be deleted because it is associated with at least one recipe");
                return(StatusCode(409, ModelState));
            }

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

            if (!_ingredientRepository.DeleteIngredient(ingredientToDelete))
            {
                ModelState.AddModelError("", $"Something went wrong deleting " +
                                         $"{ingredientToDelete.Name}");
                return(StatusCode(500, ModelState));
            }

            return(NoContent());
        }
예제 #2
0
        public void CanDeleteIngredient()
        {
            var before = _repo.GetAllIngredients().Count;

            _repo.DeleteIngredient(1);
            var after = _repo.GetAllIngredients().Count;

            Assert.AreEqual(before, after + 1);
        }
예제 #3
0
        public void DeleteMeal(Meal meal)
        {
            foreach (Ingredient ingredient in meal.Ingredients)
            {
                _ingredientRepository.DeleteIngredient(ingredient.Id);
            }

            _mealRepository.DeleteMeal(meal.Id);
        }
        public IActionResult Delete(int ingredientId)
        {
            Ingredient deletedIngredient = repository.DeleteIngredient(ingredientId);

            if (deletedIngredient != null)
            {
                TempData["message"] = $"{deletedIngredient.Name} was deleted";
            }

            return(RedirectToAction(nameof(List)));
        }
        public ActionResult DeleteIngredient(int ingredientId)
        {
            var ingredientFromRepo = _ingredientRepository.GetIngredient(ingredientId);

            if (ingredientFromRepo == null)
            {
                return(NotFound());
            }

            _ingredientRepository.DeleteIngredient(ingredientFromRepo);
            _ingredientRepository.Save();

            return(NoContent());
        }
예제 #6
0
 public void DeleteIngredient(int ingredientId)
 {
     _ingredientsRepository.DeleteIngredient(ingredientId);
 }
예제 #7
0
 public int DeleteIngredient(int ingredientId)
 {
     return(_iIngredientRepository.DeleteIngredient(ingredientId));
 }
        public IActionResult DeleteIngredient(int id)
        {
            _repository.DeleteIngredient(id);

            return(RedirectToAction("Index"));
        }
예제 #9
0
 public void DeleteIngredient(int id)
 {
     _ingredientRepository.DeleteIngredient(id);
 }
예제 #10
0
 public ActionResult DeleteIngredient(int Id)
 {
     _ingredientRepository.DeleteIngredient(Id);
     return(Json(new { success = true, responseText = "Delete sucessfully!" }, JsonRequestBehavior.AllowGet));
 }
예제 #11
0
        public IActionResult EditRecipe(EditRecipeViewModel model)
        {
            if (ModelState.IsValid)
            {
                List <Ingredient> existingIngredients = _ingredientRepository.GetAllIngredients().Where(i => i.RecipeId == model.Id).ToList();
                foreach (Ingredient ingredient in existingIngredients)
                {
                    _ingredientRepository.DeleteIngredient(ingredient.Id);
                }

                Recipe recipe = _recipeRepository.GetRecipe(model.Id);

                recipe.Id           = model.Id;
                recipe.Title        = model.Title;
                recipe.Description  = model.Description;
                recipe.Instructions = model.Instructions;
                recipe.MealType     = model.MealType;
                recipe.Servings     = model.Servings;
                recipe.PrepTime     = model.PrepTime;
                recipe.CookTime     = model.CookTime;
                recipe.Category     = model.Category;
                recipe.SpiceLevel   = model.SpiceLevel;
                recipe.Ingredients  = model.Ingredients;

                /*foreach (var newIngredient in model.Ingredients)
                 * {
                 *  Ingredient ingredient = new Ingredient()
                 *  {
                 *      RecipeId = recipe.Id,
                 *      Measure = newIngredient.Measure,
                 *      Unit = newIngredient.Unit,
                 *      Name = newIngredient.Name
                 *  };
                 *
                 *  _ingredientRepository.AddIngredient(ingredient);
                 *
                 * }*/

                if (model.Image != null)
                {
                    if (model.ExistingImagePath != null)
                    {
                        string deleteFilePath = Path.Combine("wwwroot", "images", model.ExistingImagePath);
                        System.IO.File.Delete(deleteFilePath);
                    }

                    string uploadFolder = Path.Combine("wwwroot", "images");
                    recipe.ImagePath = Guid.NewGuid().ToString() + "_" + model.Image.FileName;
                    string filePath = Path.Combine(uploadFolder, recipe.ImagePath);
                    using (var fileStream = new FileStream(filePath, FileMode.Create))
                    {
                        model.Image.CopyTo(fileStream);
                    }
                }

                _recipeRepository.EditRecipe(recipe);

                return(RedirectToAction("userrecipes", "home", new { userId = recipe.ApplicationUserId }));
            }

            return(View());
        }