private void UpdateRecipeProducts(Recipe originalRecipe, RecipeUpdateDTO updatedRecipe) { List <Guid> productsToRemoveGuids = new List <Guid>(); List <Guid> productsToAddGuids = new List <Guid>(); if (updatedRecipe.Products != null) { updatedRecipe.Products.ForEach(p => { if (!_recipesRepository.ContainsProduct(updatedRecipe.Id, p.Id)) { productsToAddGuids.Add(p.Id); } }); } if (originalRecipe.RecipeProducts != null && updatedRecipe.Products != null) { originalRecipe.RecipeProducts.ForEach(rp => { if (!updatedRecipe.Products.Any(p => p.Id == rp.ProductId)) { productsToRemoveGuids.Add(rp.ProductId); } }); } productsToRemoveGuids.ForEach(g => _recipesRepository.RemoveProductById(g)); productsToAddGuids.ForEach(g => _recipesRepository.AddProduct( new RecipeProduct { Id = Guid.NewGuid(), ProductId = g, RecipeId = updatedRecipe.Id })); }