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
1522public class EmotionRecordRepositoryImpl implements EmotionRecordRepositoryCustom {
1623
1724 private final JPAQueryFactory jpaQueryFactory ;
25+ private final QEmotionRecord emotionRecord = QEmotionRecord .emotionRecord ;
26+ private final QUser user = QUser .user ;
27+ private final QSpotifyMusic spotifyMusic = QSpotifyMusic .spotifyMusic ;
28+
1829
1930 @ Override
2031 public List <EmotionRecordDto > findByUser (User user ) {
@@ -34,4 +45,75 @@ public List<EmotionRecordDto> findByUser(User user) {
3445 .fetch ();
3546
3647 }
48+
49+ // loginId를 기준으로 JOIN FETCH (user, spotifyMusic) 후 페이징 처리
50+ @ Override
51+ public Page <EmotionRecord > findByLoginId (String loginId , Pageable pageable ) {
52+ List <EmotionRecord > content = jpaQueryFactory
53+ .selectFrom (emotionRecord )
54+ .join (emotionRecord .user , user ).fetchJoin ()
55+ .join (emotionRecord .spotifyMusic , spotifyMusic ).fetchJoin ()
56+ .where (user .loginId .eq (loginId ))
57+ .offset (pageable .getOffset ())
58+ .limit (pageable .getPageSize ())
59+ .fetch ();
60+
61+ // Spring Data JPA에서 페이징 처리를 위한 메서드 사용 시,
62+ // 내부적으로 데이터(페이징된 결과)를 가져오는 쿼리와 전체 데이터 수를 계산하는 쿼리가 둘 다 실행됨
63+ // QueryDSL을 사용할 경우에 위와 달리 데이터 수 계산 쿼리를 별도로 실행해 줘야함 (Querydsl 5 이상 권장 방식)
64+ long total = Optional .ofNullable (
65+ jpaQueryFactory
66+ .select (emotionRecord .count ())
67+ .from (emotionRecord )
68+ .join (emotionRecord .user , user )
69+ .where (user .loginId .eq (loginId ))
70+ .fetchOne ()
71+ ).orElse (0L );
72+
73+ return new PageImpl <>(content , pageable , total );
74+ }
75+
76+ // 로그인 된 userId를 제외한 EmotionRecord 조회 (LEFT JOIN으로 spotifyMusic 포함) 후 페이징처리
77+ @ Override
78+ public Page <EmotionRecord > findByWithoutUserId (Long userId , Pageable pageable ) {
79+ List <EmotionRecord > content = jpaQueryFactory
80+ .selectFrom (emotionRecord )
81+ .join (emotionRecord .user , user ).fetchJoin ()
82+ .leftJoin (emotionRecord .spotifyMusic , spotifyMusic ).fetchJoin ()
83+ .where (user .userId .ne (userId ))
84+ .offset (pageable .getOffset ())
85+ .limit (pageable .getPageSize ())
86+ .fetch ();
87+
88+ // 위의 findByLoginId 메서드 설명 참고
89+ long total = Optional .ofNullable (
90+ jpaQueryFactory
91+ .select (emotionRecord .count ())
92+ .from (emotionRecord )
93+ .join (emotionRecord .user , user )
94+ .where (user .userId .ne (userId ))
95+ .fetchOne ()
96+ ).orElse (0L );
97+
98+ return new PageImpl <>(content , pageable , total );
99+ }
100+
101+ // recordId에 해당하는 EmotionRecord 조회
102+ @ Override
103+ public Optional <EmotionRecord > findByRecordId (Long recordId ) {
104+ EmotionRecord record = jpaQueryFactory
105+ .selectFrom (emotionRecord )
106+ .where (emotionRecord .recordId .eq (recordId ))
107+ .fetchOne ();
108+ return Optional .ofNullable (record );
109+ }
110+
111+ // recordId에 해당하는 EmotionRecord 삭제
112+ @ Override
113+ public int deleteByRecordId (Long recordId ) {
114+ return (int )jpaQueryFactory
115+ .delete (emotionRecord )
116+ .where (emotionRecord .recordId .eq (recordId ))
117+ .execute ();
118+ }
37119}
0 commit comments