public async Task <ActionResult <ApiRecipeInCollection> > PostRecipeInCollection( ApiRecipeInCollection apiRecipeInCollection) { var currentUserId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; if (apiRecipeInCollection.CollectionUserId != currentUserId) { return(Unauthorized()); } var recipeInCollection = new RecipeInCollection() { CollectionTitle = apiRecipeInCollection.CollectionTitle, RecipeId = apiRecipeInCollection.RecipeId, CollectionUserId = apiRecipeInCollection.CollectionUserId }; _context.RecipeInCollection.Add(recipeInCollection); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (RecipeInCollectionExists(recipeInCollection)) { return(Conflict()); } throw; } return(CreatedAtAction("GetRecipeInCollection", new { id = apiRecipeInCollection.RecipeId }, apiRecipeInCollection)); }
private bool RecipeInCollectionExists(RecipeInCollection recipeInCollection) { return(_context.RecipeInCollection.Any(e => e.RecipeId == recipeInCollection.RecipeId && e.CollectionTitle == recipeInCollection.CollectionTitle && e.CollectionUserId == recipeInCollection.CollectionUserId)); }