//Function used for post/put recipe. //checks all the ingredient user selected with the database, if it does not exist in db, it adds //the ingredient to the db. public bool AddNewIngredientAndUnit(PostRecipeVM recipeVM) { var checkIngredientExists = _unitOfWork.RecipeIngredients.GetAll().Result.ToList(); var checkMeasurementExists = _unitOfWork.RecipeMeasurements.GetAll().Result.ToList(); foreach (var x in recipeVM.RecipeIngredientGroupVM) { var checkIngredientExist = checkIngredientExists.Find(y => y.RecipeIngredientName == x.ingredientName); if (checkIngredientExist == null) { _unitOfWork.RecipeIngredients.Add(new RecipeIngredient() { RecipeIngredientName = x.ingredientName }); } var checkMeasurementExist = checkMeasurementExists.Find(y => y.RecipeMeasurementName == x.measurementName); if (checkMeasurementExist == null) { _unitOfWork.RecipeMeasurements.Add(new RecipeMeasurement() { RecipeMeasurementName = x.measurementName }); } } //save the change so that ingredient group can be added to the db below. //_unitOfWork.Complete(); return(true); }
public async Task <RecipeInformationVM> PutRecipe(PostRecipeVM recipeVM) { //for editting a recipeVM angular will send over a recipeVM object with //editted informations. Need to remember to add functionality to add and remove //ingredients of a recipe. before any functionality is performed we need to retrieve the original //recipe information so it can be compared with the editted recipe. //_context.Entry(recipeVM).State = EntityState.Modified; var editRecipe = await _businessL.PutRecipe(recipeVM); return(editRecipe); //return CreatedAtAction("GetRecipe", new { id = editRecipe.RecipeInfos.Id }, editRecipe); }
public async Task <Recipe> PostRecipe(PostRecipeVM recipeVM) { AddNewIngredientAndUnit(recipeVM); var recipeOrigins = await _unitOfWork.RecipeOrigins.GetAll(); var recipeOrigin = recipeOrigins.ToList().Find(x => x.RecipeOriginName == recipeVM.RecipeOriginName); if (recipeOrigin == null) { recipeOrigin = new RecipeOrigin() { RecipeOriginName = recipeVM.RecipeOriginName }; _unitOfWork.RecipeOrigins.Add(recipeOrigin); } var user = _unitOfWork.Users.Get(recipeVM.UserId).Result; await _unitOfWork.Complete(); Recipe recipe = new Recipe() { User = user, RecipeOrigin = recipeOrigin, RecipeName = recipeVM.RecipeName, RecipeCookTime = recipeVM.RecipeCookTime, RecipeInstruction = recipeVM.RecipeInstruction }; List <RecipeIngredientGroups> recipeIngredientGroups = new List <RecipeIngredientGroups>(); var ingredientId = _unitOfWork.RecipeIngredients.GetAll().Result.ToList(); var measurementId = _unitOfWork.RecipeMeasurements.GetAll().Result.ToList(); foreach (var x in recipeVM.RecipeIngredientGroupVM) { var ingredientIds = ingredientId.Find(y => y.RecipeIngredientName == x.ingredientName).Id; var measurementIds = measurementId.Find(y => y.RecipeMeasurementName == x.measurementName).Id; recipeIngredientGroups.Add(new RecipeIngredientGroups() { Recipe = recipe, RecipeIngredient = _unitOfWork.RecipeIngredients.Get(ingredientIds).Result, RecipeMeasurement = _unitOfWork.RecipeMeasurements.Get(measurementIds).Result, RecipeIngredientAmount = int.Parse(x.ingredientAmount) }); } _unitOfWork.Recipes.Add(recipe); _unitOfWork.RecipeIngredientGroups.AddRange(recipeIngredientGroups); await _unitOfWork.Complete(); //var newRecipe = _unitOfWork.Recipes.GetAll().Result.Last(); return(recipe); }
public async Task <RecipeInformationVM> PutRecipe(PostRecipeVM recipeVM) { //var oldRecipe = await _unitOfWork.RecipeIngredientGroups.GetEager(recipeVM.RecipeId); var updateRecipe = await PostRecipe(recipeVM); var updatedReviews = await _unitOfWork.RecipeReviews.UpdateReviews((int)recipeVM.RecipeId, updateRecipe.Id); var deletedRecipe = await DeleteRecipe((int)recipeVM.RecipeId); var newRecipe = await GetRecipeDetail(updateRecipe.Id); await _unitOfWork.Complete(); return(newRecipe); }
public ActionResult <Recipe> PostRecipe(PostRecipeVM recipepost) { Recipe recipe = new Recipe(); if (recipepost.RecipeID != "") { recipe.RecipeId = int.Parse(recipepost.RecipeID); recipe.DateAdded = DateTime.Now; } recipe.RecipeTitle = recipepost.RecipeTitle; recipe.LevelId = recipepost.LevelID; recipe.Image1 = recipepost.Image1; recipe.Image2 = recipepost.Image2; recipe.Image3 = recipepost.Image3; if (recipepost.RecipeID == "") { _context.Recipe.Add(recipe); } else { _context.Entry(recipe).State = EntityState.Modified; _context.Steps.RemoveRange(_context.Steps.Where(s => s.RecipeId == recipe.RecipeId)); _context.Ingredients.RemoveRange(_context.Ingredients.Where(s => s.RecipeId == recipe.RecipeId)); } _context.SaveChanges(); foreach (Steps stp in recipepost.Steps) { Steps step = new Steps(); step.stepName = stp.stepName; step.RecipeId = recipe.RecipeId; _context.Steps.Add(step); } foreach (Ingredients ingr in recipepost.Ingredients) { Ingredients ingredients = new Ingredients(); ingredients.IngredientName = ingr.IngredientName; ingredients.RecipeId = recipe.RecipeId; _context.Ingredients.Add(ingredients); } _context.SaveChanges(); return(CreatedAtAction("GetRecipe", new { id = recipe.RecipeId }, recipe)); }
public async Task <ActionResult <Recipe> > PostRecipe(PostRecipeVM recipeVM) { //PostRecipeVM recipeVM //For Creating a new Recipe will accept PostRecipeVM object from Angular //need to translate that object into query readable to update db try { var recipeCreated = await _businessL.PostRecipe(recipeVM); return(CreatedAtAction("GetRecipe", new { id = recipeCreated.Id }, recipeCreated)); //return null; } catch (Exception e) { _logger.LogError($"Error: Exception thrown in RecipesController.PostRecipe: {e}"); return(StatusCode(500)); } }