diff --git a/Server/banchango/src/main/java/com/sundaegukbap/banchango/recipe/application/RecipeService.java b/Server/banchango/src/main/java/com/sundaegukbap/banchango/recipe/application/RecipeService.java index 5b7cafb..8d60ce3 100644 --- a/Server/banchango/src/main/java/com/sundaegukbap/banchango/recipe/application/RecipeService.java +++ b/Server/banchango/src/main/java/com/sundaegukbap/banchango/recipe/application/RecipeService.java @@ -19,8 +19,10 @@ import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; import java.util.NoSuchElementException; +import java.util.Optional; import java.util.stream.Collectors; @Service @@ -55,14 +57,17 @@ public void refreshRecommendedRecipes(Long userId, RecipeCategory recipeCategory User user = userRepository.findById(userId) .orElseThrow(() -> new NoSuchElementException("no user")); List recommendedRecipeIds = aiRecipeRecommendClient.getRecommendedRecipesFromAI(recipeCategory, ingredients); - List recipes = recipeRepository.findAllById(recommendedRecipeIds); - - recipes.forEach(recipe -> { - UserRecommendedRecipe recommendedRecipe = UserRecommendedRecipe.builder() - .user(user) - .recipe(recipe) - .build(); - recommendedRecipeRepository.save(recommendedRecipe); + List recipes = new ArrayList<>(); + recommendedRecipeIds.forEach(recommendedRecipeId -> { + Optional recipe = recipeRepository.findById(recommendedRecipeId); + if(recipe.isPresent()) recipes.add(recipe.get()); }); + List recommendedRecipes = recipes.stream() + .map(recipe -> UserRecommendedRecipe.builder() + .user(user) + .recipe(recipe) + .build()) + .collect(Collectors.toList()); + recommendedRecipeRepository.saveAll(recommendedRecipes); } }