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

♻️ separate recipe info query #445

Open
wants to merge 2 commits into
base: be/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -7,11 +7,13 @@
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class Category {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import net.pengcook.recipe.domain.Recipe;

Expand All @@ -22,6 +23,7 @@ public class CategoryRecipe {

@ManyToOne
@JoinColumn(name = "category_id")
@Getter
private Category category;

@ManyToOne
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.pengcook.category.dto;

import net.pengcook.category.domain.Category;

public record CategoryResponse(long categoryId, String categoryName) {

public CategoryResponse(Category category) {
this(category.getId(), category.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public interface CategoryRecipeRepository extends JpaRepository<CategoryRecipe,
List<Long> findRecipeIdsByCategoryName(String categoryName, Pageable pageable);

void deleteByRecipe(Recipe recipe);

List<CategoryRecipe> findAllByRecipe(Recipe recipe);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.pengcook.category.repository.CategoryRecipeRepository;
import net.pengcook.category.repository.CategoryRepository;
import net.pengcook.recipe.domain.Recipe;
import net.pengcook.category.dto.CategoryResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -27,6 +28,15 @@ public void deleteCategoryRecipe(Recipe recipe) {
categoryRecipeRepository.deleteByRecipe(recipe);
}

@Transactional(readOnly = true)
public List<CategoryResponse> findCategoryByRecipe(Recipe recipe) {
List<CategoryRecipe> categoryRecipes = categoryRecipeRepository.findAllByRecipe(recipe);
return categoryRecipes.stream()
.map(CategoryRecipe::getCategory)
.map(CategoryResponse::new)
.toList();
}

private void saveCategoryRecipe(Recipe recipe, String name) {
Category category = categoryRepository.findByName(name)
.orElseGet(() -> categoryRepository.save(new Category(name)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.pengcook.ingredient.dto;

import net.pengcook.ingredient.domain.IngredientRecipe;
import net.pengcook.ingredient.domain.Requirement;

public record IngredientResponse(long ingredientId, String ingredientName, Requirement requirement) {

public IngredientResponse(IngredientRecipe ingredientRecipe) {
this(
ingredientRecipe.getIngredient().getId(),
ingredientRecipe.getIngredient().getName(),
ingredientRecipe.getRequirement())
;
Comment on lines +12 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

말도안돼

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import net.pengcook.ingredient.domain.IngredientRecipe;
import net.pengcook.ingredient.domain.Requirement;
import net.pengcook.ingredient.dto.IngredientCreateRequest;
import net.pengcook.ingredient.dto.IngredientResponse;
import net.pengcook.ingredient.exception.InvalidNameException;
import net.pengcook.ingredient.repository.IngredientRecipeRepository;
import net.pengcook.ingredient.repository.IngredientRepository;
import net.pengcook.recipe.domain.Recipe;
import org.springframework.stereotype.Service;
Expand All @@ -21,6 +23,7 @@
public class IngredientService {

private final IngredientRepository ingredientRepository;
private final IngredientRecipeRepository ingredientRecipeRepository;
private final IngredientRecipeService ingredientRecipeService;
private final IngredientSubstitutionService ingredientSubstitutionService;

Expand All @@ -32,6 +35,13 @@ public void register(List<IngredientCreateRequest> requests, Recipe recipe) {
}
}

public List<IngredientResponse> findIngredientByRecipe(Recipe recipe) {
Comment on lines 37 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Transactional(readOnly = true)을 안붙이신 이유가 있으신가요??

return ingredientRecipeRepository.findAllByRecipeId(recipe.getId())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

categoryRecipeRepository.findAllByRecipe(recipe);
엔티티로 조회할지 id로 조회할지 얘기해보면 좋을 것 같아요!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

크게 문제가 될것 같지는 않지만 다른 로직에서 대부분 아이디로 조회하고 있네요. 수정하는것도 좋아 보입니다!

.stream()
.map(IngredientResponse::new)
.toList();
}

private void registerOne(IngredientCreateRequest request, Recipe recipe) {
Ingredient ingredient = registerOrGetIngredient(request.name());
IngredientRecipe ingredientRecipe = registerIngredientRecipe(recipe, request, ingredient);
Expand Down Expand Up @@ -87,5 +97,4 @@ private boolean hasDuplicateName(List<String> names) {
HashSet<String> nonDuplicate = new HashSet<>(names);
return (names.size() != nonDuplicate.size());
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
package net.pengcook.recipe.dto;

import net.pengcook.user.domain.User;

public record AuthorResponse(long authorId, String authorName, String authorImage) {

public AuthorResponse(User author) {
this(author.getId(), author.getUsername(), author.getImage());
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.time.LocalTime;
import java.util.List;
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 RecipeDescriptionResponse(
long recipeId,
Expand All @@ -24,25 +27,25 @@ public record RecipeDescriptionResponse(

public RecipeDescriptionResponse(
UserInfo userInfo,
RecipeDataResponse firstResponse,
Recipe recipe,
List<CategoryResponse> category,
List<IngredientResponse> ingredient,
boolean isLike
Comment on lines 28 to 33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍

) {
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()),
Comment on lines -45 to +48

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

isLike
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.time.LocalTime;
import java.util.List;
import net.pengcook.authentication.domain.UserInfo;
import net.pengcook.category.dto.CategoryResponse;
import net.pengcook.ingredient.dto.IngredientResponse;

public record RecipeHomeWithMineResponse(
long recipeId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,35 +94,6 @@ List<Long> findRecipeIdsByCategoryAndKeyword(
""")
List<RecipeHomeResponse> findRecipeDataV1(List<Long> recipeIds);

@Query("""
SELECT new net.pengcook.recipe.dto.RecipeDataResponse(
r.id,
r.title,
r.author.id,
r.author.username,
r.author.image,
Comment on lines -97 to -103

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍

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 = :recipeId
""")
List<RecipeDataResponse> findRecipeData(long recipeId);

@Query("""
SELECT r.id
FROM Recipe r
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import net.pengcook.like.repository.RecipeLikeRepository;
import net.pengcook.like.service.RecipeLikeService;
import net.pengcook.recipe.domain.Recipe;
import net.pengcook.recipe.dto.CategoryResponse;
import net.pengcook.recipe.dto.IngredientResponse;
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;
Expand All @@ -27,6 +27,7 @@
import net.pengcook.recipe.dto.RecipeRequest;
import net.pengcook.recipe.dto.RecipeResponse;
import net.pengcook.recipe.dto.RecipeUpdateRequest;
import net.pengcook.recipe.exception.NotFoundException;
import net.pengcook.recipe.exception.UnauthorizedException;
import net.pengcook.recipe.repository.RecipeRepository;
import net.pengcook.recipe.repository.RecipeStepRepository;
Expand Down Expand Up @@ -194,16 +195,13 @@ public void updateRecipe(UserInfo userInfo, Long recipeId, RecipeUpdateRequest r

@Transactional(readOnly = true)
public RecipeDescriptionResponse readRecipeDescription(UserInfo userInfo, long recipeId) {
List<RecipeDataResponse> recipeDataResponses = recipeRepository.findRecipeData(recipeId);
Recipe recipe = recipeRepository.findById(recipeId)
.orElseThrow(() -> new NotFoundException("존재하지 않는 레시피입니다."));
List<CategoryResponse> categories = categoryService.findCategoryByRecipe(recipe);
List<IngredientResponse> ingredients = ingredientService.findIngredientByRecipe(recipe);
boolean isLike = likeRepository.existsByUserIdAndRecipeId(userInfo.getId(), recipeId);

return new RecipeDescriptionResponse(
userInfo,
recipeDataResponses.getFirst(),
getCategoryResponses(recipeDataResponses),
getIngredientResponses(recipeDataResponses),
isLike
);
return new RecipeDescriptionResponse(userInfo, recipe, categories, ingredients, isLike);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import net.pengcook.ingredient.domain.Requirement;
import net.pengcook.ingredient.dto.IngredientCreateRequest;
import net.pengcook.recipe.dto.AuthorResponse;
import net.pengcook.recipe.dto.CategoryResponse;
import net.pengcook.recipe.dto.IngredientResponse;
import net.pengcook.category.dto.CategoryResponse;
import net.pengcook.ingredient.dto.IngredientResponse;
import net.pengcook.recipe.dto.RecipeDescriptionResponse;
import net.pengcook.recipe.dto.RecipeHomeWithMineResponse;
import net.pengcook.recipe.dto.RecipeHomeWithMineResponseV1;
Expand Down
Loading