From bfd732575ad65ad2e448a88648181f529a182ee0 Mon Sep 17 00:00:00 2001 From: FhRh Date: Thu, 3 Oct 2024 05:55:39 +0900 Subject: [PATCH] =?UTF-8?q?fix=20:=20=EC=9D=B4=20=EB=B6=80=EB=B6=84?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=EC=A0=95=EB=A0=AC=EB=90=98=EB=8A=94=20?= =?UTF-8?q?=EA=B1=B4=EA=B0=80=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../recipe/application/RecipeService.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) 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); } }