Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(RecipeQueryService) : 안쓰는 의존성 제거 #69

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading