private async Task NewRecipe()
        {
            ConsoleHelper.ColorWrite("What recipe would you like to add: ");
            var name = Console.ReadLine();

            RecipeDTO recipe = new RecipeDTO {
                Name = name
            };

            bool another = true;
            List <IngredientDTO> ingredients = new List <IngredientDTO>();

            while (another)
            {
                ConsoleHelper.ColorWrite("What ingredient would you like to add: ");
                var input = Console.ReadLine();

                try
                {
                    var ingredient = await _ingredientService.GetIngredientDTOByNameAsync(input);

                    var ingredientToAdd = new IngredientDTO {
                        Name = ingredient.Name
                    };
                    ingredients.Add(ingredientToAdd);
                }
                catch (KeyNotFoundException)
                {
                    ConsoleHelper.ColorWriteLine(ConsoleColor.DarkYellow, "The ingredient does not exist!");
                    ConsoleHelper.ColorWrite("Would you like to add it? (Y/n): ");
                    var add = Console.ReadLine();

                    if (Char.ToUpperInvariant(add[0]) == 'N')
                    {
                        ConsoleHelper.ColorWriteLine(ConsoleColor.Red, "Recipe not added.");
                        Console.WriteLine();
                        return;
                    }

                    ingredients.Add(new IngredientDTO {
                        Name = input
                    });
                }

                ConsoleHelper.ColorWrite("Would you like to add another ingredient? (y/N): ");
                var addAnother = Console.ReadLine();

                if (Char.ToUpperInvariant(addAnother[0]) != 'Y')
                {
                    another = false;
                }
            }

            try
            {
                await _recipeService.AddRecipe(recipe);

                ConsoleHelper.ColorWriteLine(ConsoleColor.Green, $"'{recipe.Name}' has been added.");
            }
            catch (KeyNotFoundException)
            {
                ConsoleHelper.ColorWriteLine(ConsoleColor.DarkYellow, $"{name} already exists.");
            }

            foreach (var ingredient in ingredients)
            {
                try
                {
                    await _recipeService.AddIngredientToRecipe(ingredient, recipe.Name);
                }
                catch (KeyNotFoundException)
                {
                    ConsoleHelper.ColorWriteLine($"'{recipe.Name}' does not exist.");
                }
            }

            Console.WriteLine();
            await this.Show();
        }
Esempio n. 2
0
        public async Task <IActionResult> PostIngredient(Guid id, Ingredient ingredient)
        {
            var updatedRecipe = await _recipes.AddIngredientToRecipe(id, ingredient);

            return(Ok(updatedRecipe));
        }