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 async Task <bool> RecipeNameIsExist(RecipeNameIsExistValidationDto dtoModel) { if (dtoModel.RecipeId.HasValue) { Recipe existingRecipe = await _context.Recipes.FirstAsync(x => x.RecipeId == dtoModel.RecipeId, dtoModel.CancellationToken); if (existingRecipe.Name.ToLower() == dtoModel.RecipeName) { return(false); } } return(await _context.Recipes.AnyAsync(x => x.Name.ToLower() == dtoModel.RecipeName, dtoModel.CancellationToken)); }
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); }
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()); }