Skip to content

[FIX] 노벨 통계 조회 시 통계 테이블 데이터 사용 - #631

Merged
GiJungPark merged 3 commits into
devfrom
fix/#630
Sep 14, 2026
Merged

GiJungPark merged 3 commits into
devfrom
fix/#630

Conversation

@ljy1348

@ljy1348 ljy1348 commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Related Issue

Key Changes

1. 노벨 상세 조회에 통계 데이터 적용

  • GET /novels/{novelId}의 평균 평점과 평가 수를 user_novel에서 매번 집계하지 않고 novel_statistics.average_rating, rating_count로 조회합니다.
  • 노벨과 통계 데이터를 QueryDSL fetch join으로 함께 조회합니다.
  • 평균 평점은 기존 응답 계약처럼 소수 첫째 자리까지 반올림하고, 통계 행이 없으면 평점과 평가 수를 0으로 반환합니다.
  • 애플리케이션 회귀 테스트와 작품 기본 정보 문서 테스트를 추가했습니다.

2. 서재 조회에 노벨 통계 데이터 적용

  • GET /users/{userId}/novels, GET /users/{userId}/novels/v2novelRatingnovel_statistics.average_rating 기준으로 변경했습니다.
  • userNovelRating은 서재 소유자가 직접 남긴 평점인 user_novel.user_novel_rating을 그대로 유지합니다. 두 평점은 서로 다른 필드와 데이터 소스를 사용합니다.
  • 서재 목록 조회 시 노벨 통계를 fetch join해 작품별 평점 SUM/COUNT 쿼리와 통계 N+1 조회를 제거했습니다.
  • 사용하지 않게 된 기존 평점 합계·개수 집계 메서드를 제거했습니다.
  • 평점 데이터 소스와 두 평점 필드의 분리를 DTO 회귀 테스트로 검증합니다. 서재 V2 문서 테스트는 작가명 작업 PR #629의 테스트를 함께 사용해 동일 엔드포인트 테스트가 중복되지 않도록 했습니다.

3. 탈퇴 시 노벨 통계 기여분 차감

  • 회원 탈퇴로 서재 데이터가 cascade 삭제되기 전에 해당 사용자의 평점 합계·평가 수·인기도 기여분을 차감합니다.
  • 탈퇴 정리에는 User 엔티티 대신 userId만 전달하며, QueryDSL의 findUserNovelByUserId로 차감 대상 서재와 노벨을 fetch join해 조회합니다.
  • 사용자 ID 조회 → 작품 통계 차감 → 사용자 삭제 순서를 테스트로 고정했습니다.

Verification

  • 관련 단위·회귀 테스트 통과
  • ./gradlew apiDocs 통과 및 OpenAPI 문서 생성 확인
  • 로컬 서버에서 작품 상세, 서재 V1, 서재 V2 API HTTP 200 확인
  • 실제 서재 응답에서 작품 평균 평점 4.3과 사용자 자체 평점 3.0이 분리되어 반환되는 것을 확인
  • SQL 로그에서 novel_statistics 조인 및 기존 평점 SUM/COUNT 재집계 쿼리 미발생 확인

To Reviewers

  • 개발 DB를 원천 user_novel 집계와 읽기 전용으로 대조한 결과, 기존 통계 행 누락과 통계값 불일치가 일부 확인됐습니다. 데이터는 직접 수정하지 않았습니다.
  • 기존 데이터는 매일 04시 통계 보정 배치 대상이며, 불일치 재발 원인 중 회원 탈퇴 경로는 이번 변경으로 보완했습니다.
  • 전체 테스트 786개 중 782개가 통과했습니다. 현재 dev 기준으로 이번 변경과 무관한 피드 필터 테스트 3개와 RecentSearch 테스트 1개가 실패하며, 이번 작업 관련 테스트는 모두 통과했습니다.

References

@ljy1348
ljy1348 force-pushed the fix/#630 branch 4 times, most recently from 78d7bfb to c232b1d Compare September 11, 2026 15:54

List<UserNovel> findUserNovelByUser(User user);

@Query("SELECT un FROM UserNovel un WHERE un.user.userId = :userId")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

p3;
이거 QueryDsl로 옮겨버리죠~

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

반영했습니다. findUserNovelByUserIdUserNovelCustomRepository/Impl의 QueryDSL 조회로 옮겼고, 탈퇴 처리 중 작품 접근 시 N+1이 발생하지 않도록 novel fetch join을 적용했습니다. (1176dbe)

@Repository
public interface NovelRepository extends JpaRepository<Novel, Long>, NovelCustomRepository {

@Query("SELECT n FROM Novel n LEFT JOIN FETCH n.novelStatistics WHERE n.novelId = :novelId")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

p3;
이것도 동일합니다~

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

반영했습니다. findByIdWithStatisticsNovelCustomRepository/Impl의 QueryDSL 조회로 옮기고 novelStatistics left fetch join을 유지했습니다. (49d89dd)

@GiJungPark
GiJungPark merged commit d81a0c0 into dev Sep 14, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FIX] 노벨 통계 조회 시 통계 테이블 데이터 사용

2 participants