Skip to content

Commit

Permalink
♻️ apply same logic to list
Browse files Browse the repository at this point in the history
  • Loading branch information
hyxrxn committed Jan 6, 2025
1 parent 87e9f67 commit 54f4e8b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 119 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.pengcook.authentication.domain.UserInfo;
import net.pengcook.category.dto.CategoryResponse;
import net.pengcook.ingredient.dto.IngredientResponse;
import net.pengcook.recipe.domain.Recipe;

public record RecipeHomeWithMineResponse(
long recipeId,
Expand All @@ -25,25 +26,25 @@ public record RecipeHomeWithMineResponse(

public RecipeHomeWithMineResponse(
UserInfo userInfo,
RecipeDataResponse firstResponse,
Recipe recipe,
List<CategoryResponse> category,
List<IngredientResponse> ingredient

) {
this(
firstResponse.recipeId(),
firstResponse.title(),
new AuthorResponse(firstResponse.authorId(), firstResponse.authorName(), firstResponse.authorImage()),
firstResponse.cookingTime(),
firstResponse.thumbnail(),
firstResponse.difficulty(),
firstResponse.likeCount(),
firstResponse.commentCount(),
firstResponse.description(),
firstResponse.createdAt(),
recipe.getId(),
recipe.getTitle(),
new AuthorResponse(recipe.getAuthor()),
recipe.getCookingTime(),
recipe.getThumbnail(),
recipe.getDifficulty(),
recipe.getLikeCount(),
recipe.getCommentCount(),
recipe.getDescription(),
recipe.getCreatedAt(),
category,
ingredient,
userInfo.isSameUser(firstResponse.authorId())
userInfo.isSameUser(recipe.getAuthor().getId())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import jakarta.annotation.Nullable;
import java.util.List;
import net.pengcook.recipe.domain.Recipe;
import net.pengcook.recipe.dto.RecipeDataResponse;
import net.pengcook.recipe.dto.RecipeHomeResponse;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
Expand Down Expand Up @@ -48,34 +47,7 @@ List<Long> findRecipeIdsByCategoryAndKeyword(
@Param("userId") @Nullable Long userId
);

@Query("""
SELECT new net.pengcook.recipe.dto.RecipeDataResponse(
r.id,
r.title,
r.author.id,
r.author.username,
r.author.image,
r.cookingTime,
r.thumbnail,
r.difficulty,
r.likeCount,
r.commentCount,
r.description,
r.createdAt,
c.id,
c.name,
i.id,
i.name,
ir.requirement
)
FROM Recipe r
JOIN FETCH CategoryRecipe cr ON cr.recipe = r
JOIN FETCH Category c ON cr.category = c
JOIN FETCH IngredientRecipe ir ON ir.recipe = r
JOIN FETCH Ingredient i ON ir.ingredient = i
WHERE r.id IN :recipeIds
""")
List<RecipeDataResponse> findRecipeData(List<Long> recipeIds);
List<Recipe> findAllByIdIn(List<Long> recipeIds);

@Query("""
SELECT new net.pengcook.recipe.dto.RecipeHomeResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
package net.pengcook.recipe.service;

import java.time.LocalTime;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import net.pengcook.authentication.domain.UserInfo;
import net.pengcook.category.dto.CategoryResponse;
import net.pengcook.category.service.CategoryService;
import net.pengcook.comment.service.CommentService;
import net.pengcook.image.service.ImageClientService;
import net.pengcook.ingredient.dto.IngredientResponse;
import net.pengcook.ingredient.service.IngredientRecipeService;
import net.pengcook.ingredient.service.IngredientService;
import net.pengcook.like.repository.RecipeLikeRepository;
import net.pengcook.like.service.RecipeLikeService;
import net.pengcook.recipe.domain.Recipe;
import net.pengcook.category.dto.CategoryResponse;
import net.pengcook.ingredient.dto.IngredientResponse;
import net.pengcook.recipe.dto.PageRecipeRequest;
import net.pengcook.recipe.dto.RecipeDataResponse;
import net.pengcook.recipe.dto.RecipeDescriptionResponse;
import net.pengcook.recipe.dto.RecipeHomeResponse;
import net.pengcook.recipe.dto.RecipeHomeWithMineResponse;
Expand Down Expand Up @@ -68,8 +65,7 @@ public List<RecipeHomeWithMineResponse> readRecipes(UserInfo userInfo, PageRecip
pageRecipeRequest.userId()
);

List<RecipeDataResponse> recipeDataResponses = recipeRepository.findRecipeData(recipeIds);
return convertToMainRecipeResponses(userInfo, recipeDataResponses);
return getRecipeHomeWithMineResponses(userInfo, recipeIds);
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -133,9 +129,8 @@ private List<Long> findRecipeIdsBySingleCondition(PageRecipeRequest pageRecipeRe
@Transactional(readOnly = true)
public List<RecipeHomeWithMineResponse> readLikeRecipes(UserInfo userInfo) {
List<Long> likeRecipeIds = likeRepository.findRecipeIdsByUserId(userInfo.getId());
List<RecipeDataResponse> recipeDataResponses = recipeRepository.findRecipeData(likeRecipeIds);

return convertToMainRecipeResponses(userInfo, recipeDataResponses);
return getRecipeHomeWithMineResponses(userInfo, likeRecipeIds);
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -219,44 +214,16 @@ public void deleteRecipe(UserInfo userInfo, long recipeId) {
});
}

private List<RecipeHomeWithMineResponse> convertToMainRecipeResponses(
UserInfo userInfo,
List<RecipeDataResponse> recipeDataResponses
) {
Collection<List<RecipeDataResponse>> groupedRecipeData = recipeDataResponses.stream()
.collect(Collectors.groupingBy(RecipeDataResponse::recipeId))
.values();

return groupedRecipeData.stream()
.map(data -> getMainRecipeResponse(userInfo, data))
private List<RecipeHomeWithMineResponse> getRecipeHomeWithMineResponses(UserInfo userInfo, List<Long> recipeIds) {
List<Recipe> recipes = recipeRepository.findAllByIdIn(recipeIds);
return recipes.stream()
.map(recipe -> {
List<CategoryResponse> categories = categoryService.findCategoryByRecipe(recipe);
List<IngredientResponse> ingredients = ingredientService.findIngredientByRecipe(recipe);
return new RecipeHomeWithMineResponse(userInfo, recipe, categories, ingredients);
})
.sorted(Comparator.comparing(RecipeHomeWithMineResponse::recipeId).reversed())
.collect(Collectors.toList());
}

private RecipeHomeWithMineResponse getMainRecipeResponse(UserInfo userInfo,
List<RecipeDataResponse> groupedResponses) {
RecipeDataResponse firstResponse = groupedResponses.getFirst();

return new RecipeHomeWithMineResponse(
userInfo,
firstResponse,
getCategoryResponses(groupedResponses),
getIngredientResponses(groupedResponses)
);
}

private List<IngredientResponse> getIngredientResponses(List<RecipeDataResponse> groupedResponses) {
return groupedResponses.stream()
.map(r -> new IngredientResponse(r.ingredientId(), r.ingredientName(), r.ingredientRequirement()))
.distinct()
.collect(Collectors.toList());
}

private List<CategoryResponse> getCategoryResponses(List<RecipeDataResponse> groupedResponses) {
return groupedResponses.stream()
.map(r -> new CategoryResponse(r.categoryId(), r.categoryName()))
.distinct()
.collect(Collectors.toList());
.toList();
}

private void verifyRecipeOwner(UserInfo userInfo, Recipe recipe) {
Expand Down

0 comments on commit 54f4e8b

Please sign in to comment.