99import com .querydsl .core .types .dsl .DateExpression ;
1010import com .querydsl .core .types .dsl .DateTimePath ;
1111import com .querydsl .core .types .dsl .Expressions ;
12+ import com .querydsl .jpa .impl .JPAQuery ;
1213import com .querydsl .jpa .impl .JPAQueryFactory ;
1314import lombok .RequiredArgsConstructor ;
1415import org .springframework .stereotype .Repository ;
@@ -35,9 +36,26 @@ public class ItemLikeRepositoryImpl implements QueryDslItemLikeRepository {
3536
3637 private final JPAQueryFactory queryFactory ;
3738
39+ public List <UserItemPointDao > findUserLikedItemsPoints (Long userId ) {
40+ return queryFactory .select (
41+ Projections .fields (
42+ UserItemPointDao .class ,
43+ itemLocation .point ,
44+ item .id ,
45+ albumCover .albumThumbnail
46+ ))
47+ .from (itemLike )
48+ .join (itemLike .item , item )
49+ .on (itemLike .item .id .eq (item .id ))
50+ .join (item .itemLocation , itemLocation )
51+ .on (item .itemLocation .id .eq (itemLocation .id ))
52+ .join (itemLocation .item .albumCover , albumCover )
53+ .on (item .albumCover .id .eq (albumCover .id ))
54+ .where (itemLike .user .id .eq (userId ))
55+ .fetch ();
56+ }
3857
39- public List <UserItemLikeDao > findByUserId (Long userId , Long lastCursor , ItemOrderType itemOrderType ) {
40-
58+ public List <UserItemLikeDao > findByUserId (Long userId , Long lastCursor , ItemOrderType orderType ) {
4159 DateExpression <Date > currentWeekExpr = currentDate ();
4260 DateTimePath <LocalDateTime > createdAtExpr = itemLike .createdAt ;
4361 QItemLike innerItemLike = new QItemLike ("innerItemLike" );
@@ -49,7 +67,7 @@ public List<UserItemLikeDao> findByUserId(Long userId, Long lastCursor, ItemOrde
4967 var query = queryFactory .select (
5068 Projections .constructor (
5169 UserItemLikeDao .class ,
52- itemOrderType == ItemOrderType .MOST_LIKED ? Expressions .constant (1 ): createdAtExpr .week ().subtract (currentWeekExpr .week ()).abs ().as ("weekAgo" ),
70+ orderType == ItemOrderType .MOST_LIKED ? Expressions .constant (1 ): createdAtExpr .week ().subtract (currentWeekExpr .week ()).abs ().as ("weekAgo" ),
5371 item .id ,
5472 item .content ,
5573 itemLike .createdAt ,
@@ -72,38 +90,13 @@ public List<UserItemLikeDao> findByUserId(Long userId, Long lastCursor, ItemOrde
7290 .join (user ).on (user .eq (item .user ))
7391 .where (itemLike .user .id .eq (userId ));
7492
75-
76- query = switch (itemOrderType ) {
77- case RECENT -> query .orderBy (itemLike .createdAt .desc ());
78- case OLDEST -> query .orderBy (itemLike .createdAt .asc ());
79- case MOST_LIKED -> query .orderBy (Expressions .numberTemplate (Long .class , "({0})" , likeCountExpr ).desc ());
80- };
93+ orderBy (orderType , query );
8194
8295 return query .fetch ();
8396 }
8497
85-
86- public List <UserItemPointDao > findUserLikedItemsPoints (Long userId ) {
87- return queryFactory .select (
88- Projections .fields (
89- UserItemPointDao .class ,
90- itemLocation .point ,
91- item .id ,
92- albumCover .albumThumbnail
93- ))
94- .from (itemLike )
95- .join (itemLike .item , item )
96- .on (itemLike .item .id .eq (item .id ))
97- .join (item .itemLocation , itemLocation )
98- .on (item .itemLocation .id .eq (itemLocation .id ))
99- .join (itemLocation .item .albumCover , albumCover )
100- .on (item .albumCover .id .eq (albumCover .id ))
101- .where (itemLike .user .id .eq (userId ))
102- .fetch ();
103- }
104-
10598 @ Override
106- public List <UserItemLikeDao > findByUserIdAndState (Long userId , Long lastCursor , ItemOrderType itemOrderType , String state ) {
99+ public List <UserItemLikeDao > findByUserIdAndState (Long userId , Long lastCursor , ItemOrderType orderType , String state ) {
107100 DateExpression <Date > currentWeekExpr = currentDate ();
108101 DateTimePath <LocalDateTime > createdAtExpr = itemLike .createdAt ;
109102 QItemLike innerItemLike = new QItemLike ("innerItemLike" );
@@ -115,7 +108,7 @@ public List<UserItemLikeDao> findByUserIdAndState(Long userId, Long lastCursor,
115108 var query = queryFactory .select (
116109 Projections .constructor (
117110 UserItemLikeDao .class ,
118- itemOrderType == ItemOrderType .MOST_LIKED ? Expressions .constant (1 ):createdAtExpr .week ().subtract (currentWeekExpr .week ()).abs ().as ("weekAgo" ),
111+ orderType == ItemOrderType .MOST_LIKED ? Expressions .constant (1 ):createdAtExpr .week ().subtract (currentWeekExpr .week ()).abs ().as ("weekAgo" ),
119112 item .id ,
120113 item .content ,
121114 itemLike .createdAt ,
@@ -140,20 +133,13 @@ public List<UserItemLikeDao> findByUserIdAndState(Long userId, Long lastCursor,
140133 .where (itemLike .user .id .eq (userId ))
141134 .where (villageArea .cityArea .stateArea .stateName .eq (state ));
142135
143-
144- query = switch (itemOrderType ) {
145- case RECENT -> query .orderBy (itemLike .createdAt .desc ());
146- case OLDEST -> query .orderBy (itemLike .createdAt .asc ());
147- case MOST_LIKED -> query .orderBy (Expressions .numberTemplate (Long .class , "({0})" , likeCountExpr ).desc ());
148- };
136+ orderBy (orderType , query );
149137
150138 return query .fetch ();
151139 }
152140
153-
154-
155141 @ Override
156- public List <UserItemLikeDao > findByUserIdAndCity (Long userId , Long lastCursor , ItemOrderType itemOrderType , String city ) {
142+ public List <UserItemLikeDao > findByUserIdAndCity (Long userId , Long lastCursor , ItemOrderType orderType , String city ) {
157143 DateExpression <Date > currentWeekExpr = currentDate ();
158144 DateTimePath <LocalDateTime > createdAtExpr = itemLike .createdAt ;
159145 QItemLike innerItemLike = new QItemLike ("innerItemLike" );
@@ -165,7 +151,7 @@ public List<UserItemLikeDao> findByUserIdAndCity(Long userId, Long lastCursor, I
165151 var query = queryFactory .select (
166152 Projections .constructor (
167153 UserItemLikeDao .class ,
168- itemOrderType == ItemOrderType .MOST_LIKED ? Expressions .constant (1 ): createdAtExpr .week ().subtract (currentWeekExpr .week ()).abs ().as ("weekAgo" ),
154+ orderType == ItemOrderType .MOST_LIKED ? Expressions .constant (1 ): createdAtExpr .week ().subtract (currentWeekExpr .week ()).abs ().as ("weekAgo" ),
169155 item .id ,
170156 item .content ,
171157 itemLike .createdAt ,
@@ -190,14 +176,17 @@ public List<UserItemLikeDao> findByUserIdAndCity(Long userId, Long lastCursor, I
190176 .where (itemLike .user .id .eq (userId ))
191177 .where (villageArea .cityArea .cityName .eq (city ));
192178
193-
194- query = switch (itemOrderType ) {
195- case RECENT -> query .orderBy (itemLike .createdAt .desc ());
196- case OLDEST -> query .orderBy (itemLike .createdAt .asc ());
197- case MOST_LIKED -> query .orderBy (Expressions .numberTemplate (Long .class , "({0})" , likeCountExpr ).desc ());
198- };
179+ orderBy (orderType , query );
199180
200181 return query .fetch ();
201182 }
202183
184+ private void orderBy (ItemOrderType orderType , JPAQuery <UserItemLikeDao > query ) {
185+ switch (orderType ) {
186+ case RECENT -> query .orderBy (item .createdAt .desc ());
187+ case OLDEST -> query .orderBy (item .createdAt .asc ());
188+ case MOST_LIKED -> query .orderBy (itemLike .count ().desc ());
189+ }
190+ }
191+
203192}
0 commit comments