public async Task <bool> Handle(CreateJoinShopIngredient request, CancellationToken cancellationToken) { try { cancellationToken.ThrowIfCancellationRequested(); await Task.Delay(1000, cancellationToken); } catch (Exception ex) when(ex is TaskCanceledException) { throw new TaskCanceledException("The user has cancelled the task!"); } bool alreadyExists = context.JoinIngredientShop.Any(s => s.IngredientsId == request.IngrdientId && s.ShopsId == request.ShopId); if (alreadyExists) { return(false); } var relation = new IngredientFromShopShop { IngredientsId = request.IngrdientId, ShopsId = request.ShopId, Quantity = request.Quantity }; await context.JoinIngredientShop.AddAsync(relation, cancellationToken); await context.SaveChangesAsync(); return(true); }
public async Task <ActionResult> Add(IngredientFromShopShop relation) { bool alreadyExists = _context.IngredientFromShopShops.Any(s => s.ShopsId == relation.ShopsId && s.IngredientsId == relation.IngredientsId); if (alreadyExists) { return(UnprocessableEntity()); } await _context.IngredientFromShopShops.AddAsync(relation); await _context.SaveChangesAsync(); var newRelation = _context.IngredientFromShopShops.Where(s => s.ShopsId == relation.ShopsId && s.IngredientsId == relation.IngredientsId); return(Ok(newRelation)); }