Skip to content

Commit

Permalink
refactor(RecipeQueryService) : 안쓰는 의존성 제거 (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
Due-IT authored Nov 27, 2024
1 parent c75610b commit 272b405
Showing 1 changed file with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,61 +13,62 @@
import com.sundaegukbap.banchango.user.domain.User;
import com.sundaegukbap.banchango.user.repository.UserRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class RecipeQueryService {

private final RecipeRepository recipeRepository;
private final UserRepository userRepository;
private final RecommendedRecipeRepository recommendedRecipeRepository;
private final IngredientMatcher ingredientMatcher;
private final ApplicationEventPublisher applicationEventPublisher;

@Transactional
public RecommendedRecipeResponse getRecipeDetail(Long userId, Long recipeId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new NoSuchElementException("no user"));
.orElseThrow(() -> new NoSuchElementException("no user"));
Recipe recipe = recipeRepository.findById(recipeId)
.orElseThrow(() -> new NoSuchElementException("no recipe"));
.orElseThrow(() -> new NoSuchElementException("no recipe"));

return resolveRecipeWithUser(user, recipe);
}

@Transactional
public RecommendedRecipeResponses getRecommendedRecipes(int pageIndex, int pageSize, Long userId) {
public RecommendedRecipeResponses getRecommendedRecipes(int pageIndex, int pageSize,
Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new NoSuchElementException("no user"));
.orElseThrow(() -> new NoSuchElementException("no user"));

int firstDataIndex = pageIndex * pageSize;
List<Recipe> recipes;
if(firstDataIndex >= 50){
if (firstDataIndex >= 50) {
recipes = recipeRepository.findRecipesByRandom(pageSize);
} else {
PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
List<UserRecommendedRecipe> userRecommendedRecipeList = recommendedRecipeRepository.findAllByUser(pageRequest, user).getContent();
List<UserRecommendedRecipe> userRecommendedRecipeList = recommendedRecipeRepository.findAllByUser(
pageRequest, user).getContent();
recipes = userRecommendedRecipeList.stream()
.map(r -> r.getRecipe())
.collect(Collectors.toList());
.map(r -> r.getRecipe())
.collect(Collectors.toList());
}

List<RecommendedRecipeResponse> recommendedRecipeResponseList = recipes.stream()
.map(recipe -> resolveRecipeWithUser(user, recipe))
.collect(Collectors.toList());
.map(recipe -> resolveRecipeWithUser(user, recipe))
.collect(Collectors.toList());

return RecommendedRecipeResponses.of(recommendedRecipeResponseList);
}

public RecommendedRecipeResponse resolveRecipeWithUser(User user, Recipe recipe) {
HashMap<String, List> ingredientRelation = ingredientMatcher.checkIngredientRelation(user, recipe);
HashMap<String, List> ingredientRelation = ingredientMatcher.checkIngredientRelation(user,
recipe);
List<Ingredient> have = ingredientRelation.get("have");
List<Ingredient> need = ingredientRelation.get("need");

Expand Down

0 comments on commit 272b405

Please sign in to comment.