コード例 #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());
        }
コード例 #2
0
        public override async Task <RecipeNameIsExistResponseModel> Handle(RecipeNameIsExistQuery request, CancellationToken cancellationToken)
        {
            RecipeNameIsExistResponseModel responseModel = new RecipeNameIsExistResponseModel();

            RecipeNameIsExistValidationDto recipeNameIsExistValidationDto = new RecipeNameIsExistValidationDto
            {
                RecipeId          = request.RecipeId,
                RecipeName        = request.RecipeName.Trim().ToLower(),
                CancellationToken = cancellationToken
            };

            responseModel.RecipeNameIsExist = await _recipeValidatorService.RecipeNameIsExist(recipeNameIsExistValidationDto);

            return(responseModel);
        }
コード例 #3
0
        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());
        }