public async Task CreateRecipe(CreateRecipeFormData recipeFormData)
        {
            if (recipeFormData == null)
            {
                throw new ArgumentNullException(nameof(recipeFormData));
            }

            if (!recipeFormData.Recipe.SelectedCategoriesIds.Any())
            {
                throw new InvalidOperationException("Cannot add recipe without categories");
            }

            using var factory = await _dbFactory.Create(IsolationLevel.ReadCommitted);

            using var _dbContext = factory.FactoryFor <ApnaBawarchiKhanaDbContext>();

            try
            {
                var recipe = await _dbContext.Recipes.AddAsync(
                    new Recipe
                {
                    Title       = recipeFormData.Recipe.Title,
                    Description = recipeFormData.Recipe.Description,
                    Difficulty  = recipeFormData.Recipe.Difficulty,
                    Serving     = recipeFormData.Recipe.Serving,
                    Time        = recipeFormData.Recipe.Time,
                    TimeUnit    = recipeFormData.Recipe.TimeUnit,
                    CreatedAt   = DateTime.UtcNow
                });

                await _dbContext.SaveChangesAsync();


                foreach (var catId in recipeFormData.Recipe.SelectedCategoriesIds)
                {
                    await _dbContext.RecipeCategories.AddAsync(new RecipeCategory { CategoryId = catId, RecipeId = recipe.Entity.Id });
                }

                await _dbContext.SaveChangesAsync();

                if (recipeFormData.Recipe.Ingredients != null && recipeFormData.Recipe.Ingredients.Any())
                {
                    foreach (var ingredient in recipeFormData.Recipe.Ingredients.OrderBy(o => o.StepNr))
                    {
                        await _dbContext.Ingredients.AddAsync(
                            new Ingredient
                        {
                            StepNr = ingredient.StepNr, Description = ingredient.Description, Quantity = ingredient.Quantity, RecipeId = recipe.Entity.Id
                        });
                    }

                    await _dbContext.SaveChangesAsync();
                }

                if (recipeFormData.Recipe.Directions != null && recipeFormData.Recipe.Directions.Any())
                {
                    foreach (var direction in recipeFormData.Recipe.Directions.OrderBy(o => o.StepNr))
                    {
                        await _dbContext.Directions.AddAsync(new Direction { StepNr = direction.StepNr, Step = direction.Step, RecipeId = recipe.Entity.Id });
                    }
                    await _dbContext.SaveChangesAsync();
                }

                if (recipeFormData.Images != null && recipeFormData.Images.Any())
                {
                    foreach (var image in recipeFormData.Images.Where(w => !string.IsNullOrEmpty(w)).ToList())
                    {
                        var imageData = await akImageFileService.GetFileAsBytes(image);

                        var insertedImageId = await StoreImageInDb(_dbContext, akImageFileService.ResizeImage(imageData));

                        await _dbContext.RecipeImages.AddAsync(new RecipeImage { ImageId = insertedImageId, RecipeId = recipe.Entity.Id });
                    }

                    await _dbContext.SaveChangesAsync();
                }

                factory.CommitTransaction();

                _logger.Information("Recipe: {RecipeTitle} has been created sucessfully.", recipeFormData.Recipe.Title);
            }
            catch (Exception ee)
            {
                _logger.Error(ee, "Failed to Create Recipe. ");
                throw;
            }
        }
Esempio n. 2
0
 public async Task CreateRecipe([FromBody] CreateRecipeFormData recipeFormData)
 {
     await _recipeService.CreateRecipe(recipeFormData);
 }