Exemplo n.º 1
0
        public override async Task <Result> Handle(CreateRecipeCommand request, CancellationToken cancellationToken)
        {
            RecipeNameIsExistValidationDto recipeNameIsExistValidationDto = new RecipeNameIsExistValidationDto
            {
                RecipeName        = request.Name.Trim().ToLower(),
                CancellationToken = cancellationToken
            };

            if (await _recipeValidatorService.RecipeNameIsExist(recipeNameIsExistValidationDto))
            {
                throw new ApiException(ApiExceptionCode.RecipeNameIsAlreadyExist, $"Recipe name is exist! {nameof(request.Name)}: {request.Name}");
            }

            Recipe recipe = new Recipe
            {
                Name              = request.Name,
                Description       = request.Description,
                ImagePath         = request.ImagePath,
                RecipeIngredients = await _recipeIngredientService.InitialNewRecipeIngredients(new InitialNewRecipeIngredientsDto
                {
                    Ingredients       = request.Ingredients,
                    CancellationToken = cancellationToken
                }),
            };


            await Context.Recipe.AddAsync(recipe, cancellationToken);

            await Context.SaveChangesAsync(cancellationToken);

            return(Result.Success());
        }
        public override async Task <Result> Handle(UpdateRecipeCommand request, CancellationToken cancellationToken)
        {
            RecipeNameIsExistValidationDto recipeNameIsExistValidationDto = new RecipeNameIsExistValidationDto
            {
                RecipeId          = request.Id,
                RecipeName        = request.Name.Trim().ToLower(),
                CancellationToken = cancellationToken
            };

            if (await _recipeValidatorService.RecipeNameIsExist(recipeNameIsExistValidationDto))
            {
                throw new ApiException(ApiExceptionCode.RecipeNameIsAlreadyExist, $"Recipe name is exist! {nameof(request.Name)}: {request.Name}");
            }

            Recipe recipe = Context.Recipe
                            .Include(x => x.RecipeIngredients)
                            .FirstOrDefault(x => x.RecipeId == request.Id);

            if (recipe == null)
            {
                throw new ApiException(ApiExceptionCode.UpgradeableRecipeNotFound,
                                       $"Upgradeable recipe not found in database! {nameof(recipe.RecipeId)}: {request.Id}");
            }

            Context.RecipeIngredient.RemoveRange(recipe.RecipeIngredients);

            recipe.Name              = request.Name;
            recipe.Description       = request.Description;
            recipe.ImagePath         = request.ImagePath;
            recipe.RecipeIngredients = await _recipeIngredientService.InitialNewRecipeIngredients(new InitialNewRecipeIngredientsDto
            {
                Ingredients       = request.Ingredients,
                CancellationToken = cancellationToken
            });

            await Context.SaveChangesAsync(cancellationToken);

            return(Result.Success());
        }