예제 #1
0
        public async Task <IActionResult> AddCocktail(CocktailViewModel vm)
        {
            if (await _cocktailServices.CocktailWithThatNameExists(vm.Name))
            {
                ModelState.AddModelError(string.Empty, "Cocktail with that name already exists.");
                return(View());
            }
            if (vm.NewPicture != null)
            {
                using (var stream = new MemoryStream())
                {
                    await vm.NewPicture.CopyToAsync(stream);

                    vm.Picture = stream.ToArray();
                }
            }
            var cocktail = await _cocktailServices.AddAsync(vm.Name, vm.Picture);

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

            for (int i = 0; i < vm.Quantities.Count; i++)
            {
                await _cocktailIngredientsServices.AddAsync(cocktail, ingredients[i], vm.Quantities[i]);
            }

            foreach (var bar in vm.Bars)
            {
                var barEntity = await _barServices.GetAsync(bar);

                await _barCocktailServices.CreateAsync(barEntity, cocktail);
            }
            return(RedirectToAction("UpdateCocktail", new { id = cocktail.Id }));
        }