Пример #1
0
        public async Task PublishRecipeAsync(Recipe recipe, IProgress <string> progress)
        {
            if (recipe == null)
            {
                throw new ArgumentNullException(nameof(recipe));
            }

            var localImages = GetLocalImages(recipe);

            foreach (var image in localImages)
            {
                progress?.Report($"Copying image {image.Data}.");
                await CopyImageAsync(image);
            }

            if (recipe.Id != 0)
            {
                progress?.Report("Adding updated recipe to database.");
                await _recipeRepository.UpdateRecipeAsync(recipe);
            }
            else
            {
                progress?.Report("Adding recipe to database.");
                await _recipeRepository.AddRecipeAsync(recipe);
            }
        }
        public async Task <Recipe> Handle(AddRecipeCommand command, CancellationToken cancellationToken)
        {
            if (!await ValidateInsertedIndexes(command))
            {
                throw new DomainException("DomainException", "Some product id's does not exist in database.");
            }

            //
            // TODO: violate DRY principle ! Change it!
            //
            var allFoodProducts = await _foodProductRepository.GetAllAsync();

            List <FoodProductDetails> productsDetails = new List <FoodProductDetails>();

            foreach (var item in command.Products)
            {
                var foodProductName    = allFoodProducts.SingleOrDefault(x => x.FoodProductId == item.FoodProductId).Name;
                FoodProductDetails fpd = new FoodProductDetails(item.FoodProductId, foodProductName, item.AmountValue, item.IsOptional);
                productsDetails.Add(fpd);
            }

            // TODO: throw Infrastrcutre Exception!! If category doesnt exist - 500 Internal Server Error.
            var recipeCategory = await _recipeRepository.GetRecipeCategoryByIdAsync(command.RecipeCategory);

            var recipe = new Recipe(
                command.Name,
                command.Description,
                recipeCategory,
                productsDetails,
                command.RequiredTime,
                command.LevelOfDifficulty);

            await _recipeRepository.AddRecipeAsync(recipe);

            await _unitOfWork.CommitAsync(cancellationToken);

            return(recipe);
        }