Ejemplo n.º 1
0
        public async Task <IActionResult> UpdateCocktail(UpdateCocktailViewModel vm)
        {
            var cocktail = await vm.MapToEntity();

            await _cocktailServices.UpdateCocktailAsync(cocktail);

            var currentIngredients = vm.CurrentIngredients.Select(c => c.MapToEntity()).ToList();

            for (int i = 0; i < currentIngredients.Count; i++)
            {
                if (await _cocktailIngredientsServices.IsPairUpdatedAsync(currentIngredients[i], cocktail, vm.Quantities[i]))
                {
                    if (vm.Quantities[i] == 0)
                    {
                        continue;
                    }

                    await _cocktailIngredientsServices.UpdateAsync(currentIngredients[i], cocktail, vm.Quantities[i]);
                }
            }

            var newIngredients = await _ingredientServices.GetMultipleIngredientsByNameAsync(vm.Ingredients);

            for (int i = 0; i < newIngredients.Count; i++)
            {
                if (!await _cocktailIngredientsServices.PairExistsAsync(newIngredients[i], cocktail))
                {
                    if (vm.Quantities[i] == 0)
                    {
                        continue;
                    }

                    await _cocktailIngredientsServices.AddAsync(cocktail, newIngredients[i], vm.Quantities[i]);
                }
            }

            return(View("ManageCocktails"));
        }