33import com .querydsl .core .types .Projections ;
44import com .querydsl .jpa .impl .JPAQueryFactory ;
55import lombok .RequiredArgsConstructor ;
6+ import org .dfbf .soundlink .domain .emotionRecord .entity .EmotionRecord ;
67import org .dfbf .soundlink .domain .emotionRecord .entity .QEmotionRecord ;
8+ import org .dfbf .soundlink .domain .emotionRecord .entity .QSpotifyMusic ;
79import org .dfbf .soundlink .domain .user .dto .response .EmotionRecordDto ;
10+ import org .dfbf .soundlink .domain .user .entity .QUser ;
811import org .dfbf .soundlink .domain .user .entity .User ;
12+ import org .springframework .data .domain .Page ;
13+ import org .springframework .data .domain .PageImpl ;
14+ import org .springframework .data .domain .Pageable ;
915import org .springframework .stereotype .Repository ;
1016
1117import java .util .List ;
18+ import java .util .Optional ;
1219
1320@ Repository
1421@ RequiredArgsConstructor
@@ -34,4 +41,75 @@ public List<EmotionRecordDto> findByUser(User user) {
3441 .fetch ();
3542
3643 }
44+
45+ // loginId를 기준으로 JOIN FETCH (user, spotifyMusic) 후 페이징 처리
46+ @ Override
47+ public Page <EmotionRecord > findByLoginId (String loginId , Pageable pageable ) {
48+ List <EmotionRecord > emotionRecords = jpaQueryFactory
49+ .selectFrom (QEmotionRecord .emotionRecord )
50+ .join (QEmotionRecord .emotionRecord .user , QUser .user ).fetchJoin ()
51+ .join (QEmotionRecord .emotionRecord .spotifyMusic , QSpotifyMusic .spotifyMusic ).fetchJoin ()
52+ .where (QUser .user .loginId .eq (loginId ))
53+ .offset (pageable .getOffset ())
54+ .limit (pageable .getPageSize ())
55+ .fetch ();
56+
57+ // Spring Data JPA에서 페이징 처리를 위한 메서드 사용 시,
58+ // 내부적으로 데이터(페이징된 결과)를 가져오는 쿼리와 전체 데이터 수를 계산하는 쿼리가 둘 다 실행됨
59+ // QueryDSL을 사용할 경우에 위와 달리 데이터 수 계산 쿼리를 별도로 실행해 줘야함 (Querydsl 5 이상 권장 방식)
60+ long total = Optional .ofNullable (
61+ jpaQueryFactory
62+ .select (QEmotionRecord .emotionRecord .count ())
63+ .from (QEmotionRecord .emotionRecord )
64+ .join (QEmotionRecord .emotionRecord .user , QUser .user )
65+ .where (QUser .user .loginId .eq (loginId ))
66+ .fetchOne ()
67+ ).orElse (0L );
68+
69+ return new PageImpl <>(emotionRecords , pageable , total );
70+ }
71+
72+ // 로그인 된 userId를 제외한 EmotionRecord 조회 (LEFT JOIN으로 spotifyMusic 포함) 후 페이징처리
73+ @ Override
74+ public Page <EmotionRecord > findByWithoutUserId (Long userId , Pageable pageable ) {
75+ List <EmotionRecord > emotionRecords = jpaQueryFactory
76+ .selectFrom (QEmotionRecord .emotionRecord )
77+ .join (QEmotionRecord .emotionRecord .user , QUser .user ).fetchJoin ()
78+ .leftJoin (QEmotionRecord .emotionRecord .spotifyMusic , QSpotifyMusic .spotifyMusic ).fetchJoin ()
79+ .where (QUser .user .userId .ne (userId ))
80+ .offset (pageable .getOffset ())
81+ .limit (pageable .getPageSize ())
82+ .fetch ();
83+
84+ // 위의 findByLoginId 메서드 설명 참고
85+ long total = Optional .ofNullable (
86+ jpaQueryFactory
87+ .select (QEmotionRecord .emotionRecord .count ())
88+ .from (QEmotionRecord .emotionRecord )
89+ .join (QEmotionRecord .emotionRecord .user , QUser .user )
90+ .where (QUser .user .userId .ne (userId ))
91+ .fetchOne ()
92+ ).orElse (0L );
93+
94+ return new PageImpl <>(emotionRecords , pageable , total );
95+ }
96+
97+ // recordId에 해당하는 EmotionRecord 조회
98+ @ Override
99+ public Optional <EmotionRecord > findByRecordId (Long recordId ) {
100+ EmotionRecord emotionRecord = jpaQueryFactory
101+ .selectFrom (QEmotionRecord .emotionRecord )
102+ .where (QEmotionRecord .emotionRecord .recordId .eq (recordId ))
103+ .fetchOne ();
104+ return Optional .ofNullable (emotionRecord );
105+ }
106+
107+ // recordId에 해당하는 EmotionRecord 삭제
108+ @ Override
109+ public int deleteByRecordId (Long recordId ) {
110+ return (int )jpaQueryFactory
111+ .delete (QEmotionRecord .emotionRecord )
112+ .where (QEmotionRecord .emotionRecord .recordId .eq (recordId ))
113+ .execute ();
114+ }
37115}
0 commit comments