/// <summary> /// This method adds the new Cocktail to the DataBase after checking if it does not exists already /// </summary> /// <param name="cocktail">This is the newly created Cocktail object</param> /// <returns>Task</returns> public async Task CreateCocktail(CocktailDTO cocktail, List <string> ingredients) { var cocktailToFind = _context.Cocktails.SingleOrDefault(c => c.Name == cocktail.Name); if (cocktailToFind != null) { throw new InvalidOperationException("Cocktail already exists in the database"); } var cocktailToAdd = _cocktailFactory.CreateNewCocktail(cocktail.Name, cocktail.Picture); cocktailToAdd.CocktailIngredient = new List <CocktailIngredient>(); foreach (var ingredient in ingredients) { var findIngredient = await _ingredientManager.FindIngredientByNameAsync(ingredient); if (findIngredient != null) { cocktailToAdd.CocktailIngredient.Add(new CocktailIngredient() { Cocktail = cocktailToAdd, Ingredient = findIngredient.ToEntity() }); } } cocktailToAdd.Information = CreateCocktailsRecipe(ingredients); await _context.Cocktails.AddAsync(cocktailToAdd); await _context.SaveChangesAsync(); }