Beispiel #1
0
        public async Task <int> AddFull(CreateRecipeDto createDto)
        {
            var recipe = new Recipe
            {
                Name        = createDto.Name,
                Description = createDto.Description
            };

            Repository.Add(recipe);

            await DbContextProvider.SaveChangesAsync();

            // Add ingredients
            foreach (var recipeIngredientDto in createDto.Ingredients)
            {
                var ingredientDto = await _ingredientsService.GetOrCreate(recipeIngredientDto.Name);

                var recipeIngredient = new RecipeIngredient
                {
                    IngredientId = ingredientDto.Id,
                    Amount       = recipeIngredientDto.Amount,
                    Unit         = recipeIngredientDto.Unit
                };

                recipe.RecipeIngredients.Add(recipeIngredient);
            }

            // Add preperation steps
            foreach (var preparationStepDto in createDto.PreparationSteps)
            {
                var preparationStep = new PreparationStep
                {
                    Description = preparationStepDto.Description
                };

                recipe.PreparationSteps.Add(preparationStep);
            }

            return(await DbContextProvider.SaveChangesAsync());
        }