Conversation
WalkthroughProfileService에 UserImageEntityRepository 의존성을 추가하고, getMyProfile/getProfile에 Changes
Sequence Diagram(s)sequenceDiagram
participant Controller
participant ProfileService
participant UserRepo as UserRepository
participant ImgRepo as UserImageEntityRepository
participant DTO
Controller->>ProfileService: getMyProfile(userId)
ProfileService->>UserRepo: find user by id
ProfileService->>ImgRepo: findByUserId(userId)
ImgRepo-->>ProfileService: Optional<UserImageEntity> (maybe empty)
ProfileService->>DTO: new GetMyProfileResponse(..., imageId)
ProfileService-->>Controller: GetMyProfileResponse
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches
🧪 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: 0
🧹 Nitpick comments (1)
src/main/java/hanium/modic/backend/domain/profile/service/ProfileService.java (1)
36-38: 중복 쿼리를 최적화하는 것을 권장합니다.
userImageService.createImageGetUrlOptional과userImageEntityRepository.findByUserId모두 동일한UserImageEntity를 조회합니다. 이로 인해 프로필 조회 시마다 불필요한 데이터베이스 호출이 중복 발생합니다.다음 중 하나의 방식으로 개선을 고려해보세요:
방법 1: UserImageEntity를 한 번만 조회
- final Optional<String> userImageUrl = userImageService.createImageGetUrlOptional(user.getId()); - final Optional<Long> userImageId = userImageEntityRepository.findByUserId(user.getId()) - .map(UserImageEntity::getId); + final Optional<UserImageEntity> userImage = userImageEntityRepository.findByUserId(user.getId()); + final Optional<String> userImageUrl = userImage.map(img -> userImageService.createImageGetUrl(img)); + final Optional<Long> userImageId = userImage.map(UserImageEntity::getId);방법 2: UserImageService가 ID도 함께 반환하도록 수정
// UserImageService에 새 메서드 추가 public Optional<UserImageInfo> getUserImageInfo(Long userId) { return userImageEntityRepository.findByUserId(userId) .map(entity -> new UserImageInfo(entity.getId(), createImageGetUrl(entity))); } record UserImageInfo(Long id, String url) {}참고: 방법 1은
userImageService.createImageGetUrlOptional내부 구현을 확인하여UserImageEntity를 받는 오버로드 메서드가 있는지 검토가 필요합니다.Also applies to: 64-66
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/main/java/hanium/modic/backend/domain/profile/service/ProfileService.java(6 hunks)src/main/java/hanium/modic/backend/web/profile/dto/GetMyProfileResponse.java(1 hunks)src/main/java/hanium/modic/backend/web/profile/dto/GetProfileResponse.java(1 hunks)
🔇 Additional comments (5)
src/main/java/hanium/modic/backend/web/profile/dto/GetProfileResponse.java (1)
14-14: 필드 추가가 적절합니다.
userImageId필드가 레코드에 올바르게 추가되었으며,Long타입 사용으로 null 처리가 가능합니다.@JsonInclude(NON_NULL)설정에 따라 값이 없을 때는 JSON에서 제외됩니다.src/main/java/hanium/modic/backend/web/profile/dto/GetMyProfileResponse.java (1)
14-14: 필드 추가가 적절합니다.
GetProfileResponse와 일관되게userImageId필드가 추가되었습니다. 타입과 위치가 적절합니다.src/main/java/hanium/modic/backend/domain/profile/service/ProfileService.java (3)
6-6: 필요한 의존성이 올바르게 추가되었습니다.
@Transactional,UserImageEntity,UserImageEntityRepository임포트 및 리포지토리 의존성 주입이 적절하게 추가되었습니다.Also applies to: 13-13, 15-15, 28-28
31-31: 트랜잭션 추가가 적절합니다.여러 리포지토리 호출을 포함하는 조회 메서드에
@Transactional(readOnly = true)를 추가한 것은 데이터 일관성을 보장하는 좋은 관행입니다.Also applies to: 56-56
41-52: 응답 객체 생성이 올바릅니다.
userImageId.orElse(null)을 사용하여 새로운 필드를 응답 생성자에 전달하는 방식이 적절하며, 기존userImageUrl패턴과 일관성이 있습니다.Also applies to: 69-79
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/main/java/hanium/modic/backend/domain/profile/service/ProfileService.java (1)
37-38: userImageId 조회 로직이 올바르게 구현되었습니다.
userImageEntityRepository.findByUserId()를 사용하여 이미지 ID를 조회하고 Optional 처리를 통해 null 안전성을 확보한 구현이 정확합니다.선택적 개선 사항: 향후 성능 최적화가 필요한 경우,
UserEntity조회 시UserImageEntity를 fetch join하여 추가 쿼리를 줄일 수 있습니다. 하지만 단일 프로필 조회에서는 현재 구현으로도 충분합니다.// 예시: UserEntityRepository에 fetch join 쿼리 추가 시 @Query("SELECT u FROM UserEntity u LEFT JOIN FETCH u.userImage WHERE u.id = :userId") Optional<UserEntity> findByIdWithImage(@Param("userId") Long userId);Also applies to: 47-47, 65-66, 75-75
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/main/java/hanium/modic/backend/domain/profile/service/ProfileService.java(6 hunks)src/test/java/hanium/modic/backend/domain/profile/ProfileServiceTest.java(4 hunks)
🔇 Additional comments (3)
src/test/java/hanium/modic/backend/domain/profile/ProfileServiceTest.java (1)
24-24: 목(mock) 추가가 적절합니다.새로운 의존성
UserImageEntityRepository에 대한 목 설정이 올바르게 추가되었습니다.Also applies to: 44-45
src/main/java/hanium/modic/backend/domain/profile/service/ProfileService.java (2)
6-6: 읽기 전용 트랜잭션 적용이 적절합니다.
@Transactional(readOnly = true)어노테이션을 프로필 조회 메서드에 추가한 것은 좋은 개선입니다. 읽기 전용 트랜잭션은 성능 최적화와 의도 명시에 도움이 됩니다.Also applies to: 31-31, 56-56
13-13: 새로운 의존성 추가가 올바릅니다.
UserImageEntity와UserImageEntityRepository추가가 적절하게 이루어졌습니다. 프로필 이미지 ID를 조회하기 위한 필수 의존성입니다.Also applies to: 15-15, 28-28
| when(postRepository.countByUserId(1L)).thenReturn(5L); | ||
| when(followRepository.countByMyId(1L)).thenReturn(3L); | ||
| when(followRepository.countByFollowingId(1L)).thenReturn(7L); | ||
| when(userImageEntityRepository.findByUserId(1L)).thenReturn(Optional.empty()); |
There was a problem hiding this comment.
🧩 Analysis chain
테스트에서 userImageId 필드 검증이 누락되었습니다.
테스트가 userImageEntityRepository를 스텁(stub)하고 있지만, 응답 객체의 userImageId 필드를 실제로 검증하지 않습니다. 또한 이미지가 존재하는 경우(non-empty Optional)에 대한 테스트 케이스가 없습니다.
다음 스크립트를 실행하여 응답 DTO에 userImageId 필드가 있는지 확인하세요:
테스트를 개선하기 위해 다음을 제안합니다:
- 기존 테스트에 userImageId 검증 추가:
assertThat(response.followerCount()).isEqualTo(7L);
assertThat(response.coin()).isEqualTo(0L);
+ assertThat(response.userImageId()).isNull();- 이미지가 존재하는 경우를 테스트하는 새로운 테스트 케이스 추가를 고려하세요.
Also applies to: 86-86
🏁 Script executed:
#!/bin/bash
# GetMyProfileResponse와 GetProfileResponse에 userImageId 필드가 있는지 확인
ast-grep --pattern $'record GetMyProfileResponse($$$, Long userImageId, $$$) {}'
ast-grep --pattern $'record GetProfileResponse($$$, Long userImageId, $$$) {}'Length of output: 0
추가 정보를 수집하기 위해 코드베이스를 더 깊이 있게 검색하겠습니다.
---
🏁 Script executed:
```shell
#!/bin/bash
# GetMyProfileResponse와 GetProfileResponse 정의 찾기
echo "=== Searching for GetMyProfileResponse ==="
rg -n "class GetMyProfileResponse|record GetMyProfileResponse|interface GetMyProfileResponse" -A 5
echo "=== Searching for GetProfileResponse ==="
rg -n "class GetProfileResponse|record GetProfileResponse|interface GetProfileResponse" -A 5
echo "=== Searching for userImageId field ==="
rg -n "userImageId" -C 2
Length of output: 4908
테스트 파일을 검토하여 실제 검증 상태를 확인하겠습니다.
🏁 Script executed:
#!/bin/bash
# ProfileServiceTest.java의 전체 내용 확인 (특히 59번과 86번 라인)
echo "=== ProfileServiceTest.java content around line 59 ==="
sed -n '45,75p' src/test/java/hanium/modic/backend/domain/profile/ProfileServiceTest.java
echo ""
echo "=== ProfileServiceTest.java content around line 86 ==="
sed -n '75,100p' src/test/java/hanium/modic/backend/domain/profile/ProfileServiceTest.java
echo ""
echo "=== Check for userImageId assertions in test ==="
rg -n "userImageId" src/test/java/hanium/modic/backend/domain/profile/ProfileServiceTest.javaLength of output: 2238
테스트에서 userImageId, hasUserImage, userImageUrl 필드 검증이 누락되었습니다.
검증 결과, userImageId 필드가 응답 DTO에 실제로 존재하며 ProfileService에서 데이터베이스 조회 결과를 매핑하고 있음이 확인되었습니다. 그러나 현재 테스트는 이 필드들을 전혀 검증하고 있지 않습니다.
필요한 개선사항:
-
기존 테스트에 누락된 필드 검증 추가 (line 59, 86):
assertThat(response.userImageId()).isNull();assertThat(response.hasUserImage()).isFalse();assertThat(response.userImageUrl()).isNull();
-
이미지가 존재하는 경우에 대한 추가 테스트 케이스 작성:
- Optional이 비어있지 않은 경우의 시나리오를 테스트하여 userImageId가 올바르게 매핑되는지 검증
🤖 Prompt for AI Agents
In src/test/java/hanium/modic/backend/domain/profile/ProfileServiceTest.java
around lines 59 and 86, the test currently stubs
userImageEntityRepository.findByUserId(1L) to return Optional.empty() but does
not assert the userImageId, hasUserImage, and userImageUrl fields on the
response; add assertions after the call at line 59:
assertThat(response.userImageId()).isNull();
assertThat(response.hasUserImage()).isFalse();
assertThat(response.userImageUrl()).isNull(); and similarly update the
assertions at line 86 if applicable; additionally add a new test case that stubs
findByUserId to return a non-empty Optional with a UserImageEntity and asserts
that response.userImageId() equals the entity id, response.hasUserImage()
isTrue, and response.userImageUrl() equals the expected URL to verify correct
mapping.
개요
작업사항
Summary by CodeRabbit
릴리스 노트