From c5ad95ee1e3dc03c9bcfbed60376735a884122c5 Mon Sep 17 00:00:00 2001 From: FhRh Date: Thu, 3 Oct 2024 06:12:08 +0900 Subject: [PATCH] =?UTF-8?q?refact=20:=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EB=B6=84=ED=95=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../recipe/application/RecipeService.java | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 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 dd792f6..c50a8fa 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 @@ -48,22 +48,14 @@ public void changeRecipeCategory(Long userId, RecipeCategory recipeCategory) { @Transactional public void refreshRecommendedRecipes(Long userId, RecipeCategory recipeCategory) { - List containers = containerRepository.findAllByUserId(userId); - List containerIngredients = containerIngredientRepository.findByContainerIn(containers); - List ingredients = containerIngredients.stream() - .map(ContainerIngredient::getIngredient) - .collect(Collectors.toList()); + List ingredients = getIngredientsWithUser(userId); recommendedRecipeRepository.deleteAllByUserId(userId); User user = userRepository.findById(userId) .orElseThrow(() -> new NoSuchElementException("no user")); - List recommendedRecipeIds = aiRecipeRecommendClient.getRecommendedRecipesFromAI(recipeCategory, ingredients); - List recipes = new ArrayList<>(); - recommendedRecipeIds.forEach(recommendedRecipeId -> { - Optional recipe = recipeRepository.findById(recommendedRecipeId); - if(recipe.isPresent()) recipes.add(recipe.get()); - }); + List recipes = recommendedRecipesFromAI(recipeCategory, ingredients); + List recommendedRecipes = recipes.stream() .map(recipe -> UserRecommendedRecipe.builder() .user(user) @@ -72,4 +64,24 @@ public void refreshRecommendedRecipes(Long userId, RecipeCategory recipeCategory .collect(Collectors.toList()); recommendedRecipeRepository.saveAll(recommendedRecipes); } + + private List getIngredientsWithUser(Long userId) { + List containers = containerRepository.findAllByUserId(userId); + List containerIngredients = containerIngredientRepository.findByContainerIn(containers); + List ingredients = containerIngredients.stream() + .map(ContainerIngredient::getIngredient) + .collect(Collectors.toList()); + return ingredients; + } + + private List recommendedRecipesFromAI(RecipeCategory recipeCategory, List ingredients) { + List recommendedRecipeIds = aiRecipeRecommendClient.getRecommendedRecipesFromAI(recipeCategory, ingredients); + List recipes = new ArrayList<>(); + recommendedRecipeIds.forEach(recommendedRecipeId -> { + Optional recipe = recipeRepository.findById(recommendedRecipeId); + + if (recipe.isPresent()) recipes.add(recipe.get()); + }); + return recipes; + } }