Skip to content

Commit

Permalink
refact : 메서드 분할
Browse files Browse the repository at this point in the history
  • Loading branch information
Due-IT committed Oct 2, 2024
1 parent a0ce745 commit c5ad95e
Showing 1 changed file with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,14 @@ public void changeRecipeCategory(Long userId, RecipeCategory recipeCategory) {

@Transactional
public void refreshRecommendedRecipes(Long userId, RecipeCategory recipeCategory) {
List<Container> containers = containerRepository.findAllByUserId(userId);
List<ContainerIngredient> containerIngredients = containerIngredientRepository.findByContainerIn(containers);
List<Ingredient> ingredients = containerIngredients.stream()
.map(ContainerIngredient::getIngredient)
.collect(Collectors.toList());
List<Ingredient> ingredients = getIngredientsWithUser(userId);

recommendedRecipeRepository.deleteAllByUserId(userId);

User user = userRepository.findById(userId)
.orElseThrow(() -> new NoSuchElementException("no user"));
List<Long> recommendedRecipeIds = aiRecipeRecommendClient.getRecommendedRecipesFromAI(recipeCategory, ingredients);
List<Recipe> recipes = new ArrayList<>();
recommendedRecipeIds.forEach(recommendedRecipeId -> {
Optional<Recipe> recipe = recipeRepository.findById(recommendedRecipeId);
if(recipe.isPresent()) recipes.add(recipe.get());
});
List<Recipe> recipes = recommendedRecipesFromAI(recipeCategory, ingredients);

List<UserRecommendedRecipe> recommendedRecipes = recipes.stream()
.map(recipe -> UserRecommendedRecipe.builder()
.user(user)
Expand All @@ -72,4 +64,24 @@ public void refreshRecommendedRecipes(Long userId, RecipeCategory recipeCategory
.collect(Collectors.toList());
recommendedRecipeRepository.saveAll(recommendedRecipes);
}

private List<Ingredient> getIngredientsWithUser(Long userId) {
List<Container> containers = containerRepository.findAllByUserId(userId);
List<ContainerIngredient> containerIngredients = containerIngredientRepository.findByContainerIn(containers);
List<Ingredient> ingredients = containerIngredients.stream()
.map(ContainerIngredient::getIngredient)
.collect(Collectors.toList());
return ingredients;
}

private List<Recipe> recommendedRecipesFromAI(RecipeCategory recipeCategory, List<Ingredient> ingredients) {
List<Long> recommendedRecipeIds = aiRecipeRecommendClient.getRecommendedRecipesFromAI(recipeCategory, ingredients);
List<Recipe> recipes = new ArrayList<>();
recommendedRecipeIds.forEach(recommendedRecipeId -> {
Optional<Recipe> recipe = recipeRepository.findById(recommendedRecipeId);

if (recipe.isPresent()) recipes.add(recipe.get());
});
return recipes;
}
}

0 comments on commit c5ad95e

Please sign in to comment.