Пример #1
0
        //Tested
        public async Task AddCocktail(CocktailDto cocktailDto)
        {
            cocktailDto.ValidateIfNull("Model is not valid!");

            var uniqueFileNamePath = _fileUploadService.SetUniqueImagePathForCocktail(cocktailDto.CocktailImage);

            cocktailDto.Image = uniqueFileNamePath;
            var cocktail = cocktailDto.MapToCocktailModel();

            _context.Cocktails.Add(cocktail);
            await _context.SaveChangesAsync();

            foreach (var cocktailComponenetDTO in cocktailDto.Ingredients)
            {
                var ingridientId = await _ingredientServices.GetIngredientIdByNameAsync(cocktailComponenetDTO.Ingredient);

                _context.CocktailComponent.Add(
                    new CocktailComponent
                {
                    CocktailId   = cocktail.Id,
                    IngredientId = ingridientId,
                    Quantity     = cocktailComponenetDTO.Quantity,
                    Unit         = cocktailComponenetDTO.Unit,
                    Name         = cocktailComponenetDTO.Ingredient
                });
            }
            cocktail.Recepie = await _recipeServices.ExtractRecipe(cocktail);

            await _context.SaveChangesAsync();
        }