fix: 커뮤니티 게시글 조회 응답 dto에 작성자 필드 추가#123
Conversation
개요두 개의 게시물 응답 DTO(PostDetailResponse, PostListResponse)에 변경사항
예상 코드 리뷰 노력🎯 2 (Simple) | ⏱️ ~8분 시
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In
`@src/main/java/kr/co/webee/presentation/post/dto/response/PostListResponse.java`:
- Line 36: getAllPosts() causes an N+1 because PostRepository.findAll(pageable)
returns Posts with user lazily loaded and PostListResponse.from() calls
post.getUser().getName() for each row; fix by loading user together with
posts—e.g., add a repository method like findAllWithUser(Pageable) using a fetch
join or annotate PostRepository.findAll(...) with `@EntityGraph`(attributePaths =
"user"), or switch to a DTO projection that selects p and u.name together—then
update getAllPosts() to call the new findAllWithUser / projection so
PostListResponse.from(...) no longer triggers extra queries.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: c53161d0-b275-498d-99f2-8fcfdc539e68
📒 Files selected for processing (2)
src/main/java/kr/co/webee/presentation/post/dto/response/PostDetailResponse.javasrc/main/java/kr/co/webee/presentation/post/dto/response/PostListResponse.java
| public static PostListResponse from(Post post, int likeCount, int commentCount) { | ||
| return PostListResponse.builder() | ||
| .postId(post.getId()) | ||
| .writer(post.getUser().getName()) |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Description: PostListResponse.from 호출부와 게시글 목록 조회 쿼리가 user를 선로딩하는지 확인합니다.
# Expect: 목록 조회 경로에서 fetch join user, `@EntityGraph`(attributePaths = "user"), 또는 DTO/projection으로 writer를 함께 조회하는 코드가 있어야 합니다.
echo "== PostListResponse.from 호출부 =="
rg -n -C3 'PostListResponse\.from\s*\('
echo
echo "== 게시글 repository/service의 user fetch 관련 코드 =="
rg -n -C4 'fetch\s+join\s+.*user|join\s+fetch\s+.*user|@EntityGraph|attributePaths\s*=\s*\{?\s*"user"|find.*Post|postRepository' --type=javaRepository: BeeKeeprs/was
Length of output: 14978
🏁 Script executed:
# PostRepository 확인
find . -name "PostRepository.java" -type f | head -1 | xargs cat
# Post 엔티티의 user 필드 확인
find . -name "Post.java" -path "*/domain/post/*" -type f | head -1 | xargs cat -n | grep -A5 -B5 "user"Repository: BeeKeeprs/was
Length of output: 2227
getAllPosts() 메서드에서 N+1 쿼리가 발생합니다.
PostRepository.findAll(pageable)은 Post만 로드하고 user는 LAZY이므로, PostListResponse.from() 호출 시 각 게시글마다 post.getUser().getName()이 추가 쿼리를 발생시킵니다.
게시글 목록 조회 시 user를 함께 로드하도록 수정하세요. 예를 들어:
- PostRepository에 fetch join을 사용한 커스텀 메서드 추가
- Post 엔티티에
@EntityGraph(attributePaths = "user")적용 - 또는 DTO projection으로 user 정보를 함께 조회
`@Query`("SELECT new dto(p.id, p.title, u.name) FROM Post p JOIN FETCH p.user u")
Slice<PostListResponse> findAllWithUser(Pageable pageable);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In
`@src/main/java/kr/co/webee/presentation/post/dto/response/PostListResponse.java`
at line 36, getAllPosts() causes an N+1 because PostRepository.findAll(pageable)
returns Posts with user lazily loaded and PostListResponse.from() calls
post.getUser().getName() for each row; fix by loading user together with
posts—e.g., add a repository method like findAllWithUser(Pageable) using a fetch
join or annotate PostRepository.findAll(...) with `@EntityGraph`(attributePaths =
"user"), or switch to a DTO projection that selects p and u.name together—then
update getAllPosts() to call the new findAllWithUser / projection so
PostListResponse.from(...) no longer triggers extra queries.
🧷 이슈
🔨 작업 내용
👀 리뷰 요구사항
Summary by CodeRabbit