Beispiel #1
0
        private async Task <Recipe> _mapSentIntoRecipe(Recipe dbRecipe, SentRecipe sentRecipe)
        {
            dbRecipe.RecipeDescription = sentRecipe.RecipeDescription;
            dbRecipe.RecipeName        = sentRecipe.RecipeName;
            dbRecipe.RecipeImage       = sentRecipe.RecipeImage;
            var        sentTags = sentRecipe.tags;
            List <Tag> dbTags   = await _repo.GetAllTags();

            List <RecipeTag> dbRecipeTags = await _repo.GetAllRecipeTags();

            sentTags.ToList().ForEach(tag =>
            {
                var savedTag = dbTags.Where(t => t.TagId == tag.TagId).FirstOrDefault();
                if (savedTag == null)
                {
                    System.Console.WriteLine("tag not found: " + tag.TagName);
                    Tag dbTag = _repo.saveNewTag(tag);
                    _createRecipeTag(dbRecipe, dbTag);
                }
                else
                {
                    System.Console.WriteLine("tag found: " + tag.TagName);
                    var savedRecipeTag = dbRecipeTags.Where(rt => rt.TagId == savedTag.TagId && rt.RecipeId == dbRecipe.RecipeId).FirstOrDefault();
                    if (savedRecipeTag == null)
                    {
                        System.Console.WriteLine("added to recipe");
                        _createRecipeTag(dbRecipe, savedTag);
                    }
                }
            });
            dbRecipe.RecipeTags.ToList().ForEach(rt =>
            {
                if (!sentRecipe.tags.Any(t => t.TagName == rt.Tag.TagName && t.TagDescription == rt.Tag.TagDescription))
                {
                    System.Console.WriteLine("we gonna try not to remove: " + rt.Tag.TagName);
                    _repo.RemoveRecipeTag(rt);
                }
            });
            await _repo.SaveChanges();

            var sentIngredients             = sentRecipe.ingredients;
            List <Ingredient> dbIngredients = await _repo.GetAllIngredients();

            List <RecipeIngredient> dbRecipeIng = await _repo.GetAllRecipeIngredients();

            sentIngredients.ToList().ForEach(ingredient =>
            {
                var savedIngredient = dbIngredients.Where(t => t.IngredientId == ingredient.IngredientId).FirstOrDefault();
                if (savedIngredient == null)
                {
                    System.Console.WriteLine("ingredient not found: " + ingredient.IngredientName);
                    Ingredient dbIngredient = _repo.saveNewIngredient(ingredient);
                    _createRecipeIngredient(dbRecipe, dbIngredient);
                }
                else
                {
                    System.Console.WriteLine("ingredient found: " + ingredient.IngredientName);
                    var savedRecipeIngredient = dbRecipeIng.Where(ri => ri.IngredientId == savedIngredient.IngredientId && ri.RecipeId == dbRecipe.RecipeId).FirstOrDefault();
                    if (savedRecipeIngredient == null)
                    {
                        System.Console.WriteLine("added to recipe");
                        _createRecipeIngredient(dbRecipe, savedIngredient);
                    }
                }
            });
            dbRecipe.RecipeIngredients.ToList().ForEach(rt =>
            {
                if (!sentRecipe.ingredients.Any(t => t.IngredientName == rt.Ingredient.IngredientName && t.IngredientDescription == rt.Ingredient.IngredientDescription))
                {
                    System.Console.WriteLine("we gonna try not to remove: " + rt.Ingredient.IngredientName);
                    _repo.RemoveRecipeIngredient(rt);
                }
            });
            await _repo.SaveChanges();

            // List<Step> dbSteps = await _repo.GetAllSteps();
            var         dboldRecipeSteps = dbRecipe.Steps.ToList();
            var         newSteps         = sentRecipe.Steps.ToList();
            List <Step> savedSteps       = new List <Step>();

            for (int i = 0; i < newSteps.Count; i++)
            {
                var dbStep = dboldRecipeSteps.Where(s => s.RecipeId == newSteps[i].RecipeId && s.RecipeStepNo == newSteps[i].RecipeStepNo && s.StepDescription == newSteps[i].StepDescription);
                if (dbStep == null)
                {
                    System.Console.WriteLine("step not found: " + newSteps[i].RecipeStepNo + " " + newSteps[i].StepDescription);
                    Step savedStep = _repo.SaveNewStep(newSteps[i]);
                    savedSteps.Add(savedStep);
                }
                else
                {
                    System.Console.WriteLine("step found: " + newSteps[i].RecipeStepNo + " " + newSteps[i].StepDescription);
                    savedSteps.Add(newSteps[i]);
                }
            }
            dboldRecipeSteps.ForEach(step =>
            {
                if (!savedSteps.Any(s => s.StepId == step.StepId))
                {
                    System.Console.WriteLine("old step deleted: " + step.RecipeStepNo + " " + step.StepDescription);
                    _repo.DeleteStep(step);
                }
            });
            dbRecipe.Steps = savedSteps;
            await _repo.SaveChanges();

            return(dbRecipe);
        }