public async Task <ActionResult> Add(IngredientFromProductProduct relation) { bool existRelation = await _repositoryIngredientProduct.CreateRelation(relation); if (existRelation) { return(UnprocessableEntity()); } return(Ok()); }
public async Task <bool> CreateRelation(IngredientFromProductProduct relation) { bool alreadyExists = _dataContext.IngredientFromProductProducts.Any(s => s.ProductsId == relation.ProductsId && s.IngredientsId == relation.IngredientsId); if (alreadyExists) { return(true); } await _dataContext.IngredientFromProductProducts.AddAsync(relation); await _dataContext.SaveChangesAsync(); return(false); }
public async Task <bool> Handle(CreateJoinProductIngredientCommand request, CancellationToken cancellationToken) { bool alreadyExists = context.IngredientFromProductProducts.Any(s => s.ProductsId == request.ProductsId && s.IngredientsId == request.IngredientsId); if (alreadyExists) { return(false); } var relation = new IngredientFromProductProduct { IngredientsId = request.IngredientsId, ProductsId = request.ProductsId }; await context.IngredientFromProductProducts.AddAsync(relation); await context.SaveChangesAsync(); return(true); }