Skip to content

Commit 73221b4

Browse files
authored
🐛 fix(api): cannot find user items filtered by location (#533)
* ♻️ refactor(api): extract private method ordering by ItemOrderType * 🐛 fix(api): cannot find user items filtered by location
1 parent d97752e commit 73221b4

File tree

3 files changed

+63
-75
lines changed

3 files changed

+63
-75
lines changed

backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemLikeRepositoryImpl.java

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.querydsl.core.types.dsl.DateExpression;
1010
import com.querydsl.core.types.dsl.DateTimePath;
1111
import com.querydsl.core.types.dsl.Expressions;
12+
import com.querydsl.jpa.impl.JPAQuery;
1213
import com.querydsl.jpa.impl.JPAQueryFactory;
1314
import lombok.RequiredArgsConstructor;
1415
import 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
}

backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemRepositoryImpl.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import com.depromeet.domains.item.dao.ItemDao;
44
import com.depromeet.domains.user.dto.request.ItemOrderType;
55
import com.querydsl.core.types.Projections;
6-
import com.querydsl.core.types.dsl.CaseBuilder;
76
import com.querydsl.core.types.dsl.DateExpression;
87
import com.querydsl.core.types.dsl.DateTimePath;
98
import com.querydsl.core.types.dsl.Expressions;
109
import com.querydsl.jpa.JPAExpressions;
10+
import com.querydsl.jpa.impl.JPAQuery;
1111
import com.querydsl.jpa.impl.JPAQueryFactory;
1212
import lombok.RequiredArgsConstructor;
1313
import org.springframework.stereotype.Repository;
@@ -25,25 +25,22 @@
2525
import static com.depromeet.music.artist.QArtist.artist;
2626
import static com.depromeet.music.song.QSong.song;
2727
import static com.querydsl.core.types.dsl.Expressions.currentDate;
28-
import static com.querydsl.jpa.JPAExpressions.select;
2928

3029
@Repository
3130
@RequiredArgsConstructor
3231
public class ItemRepositoryImpl implements QueryDslItemRepository {
3332

34-
3533
private final JPAQueryFactory queryFactory;
3634

3735
@Override
3836
public List<ItemDao> findByUserId(Long userId, long lastCursor, ItemOrderType orderType) {
39-
4037
DateExpression<Date> currentWeekExpr = currentDate();
4138
DateTimePath<LocalDateTime> createdAtExpr = item.createdAt;
4239

4340
var isLikedSubQuery = JPAExpressions.select(itemLike.id)
4441
.from(itemLike)
4542
.where(itemLike.item.id.eq(item.id)
46-
.and(itemLike.user.id.eq(userId)));
43+
.and(itemLike.user.id.eq(userId)));
4744

4845
var query = queryFactory.select(
4946
Projections.constructor(
@@ -70,26 +67,25 @@ public List<ItemDao> findByUserId(Long userId, long lastCursor, ItemOrderType or
7067
.where(item.user.id.eq(userId))
7168
.groupBy(item.id, item.content, item.createdAt, itemLocation.name, song.name, album.name, artist.name, albumCover.albumThumbnail);
7269

73-
74-
query = switch (orderType) {
75-
case RECENT -> query.orderBy(item.createdAt.desc());
76-
case OLDEST -> query.orderBy(item.createdAt.asc());
77-
case MOST_LIKED -> query.orderBy(itemLike.count().desc());
78-
};
70+
orderBy(orderType, query);
7971

8072
return query.fetch();
8173
}
8274

8375
@Override
8476
public List<ItemDao> findByUserIdAndState(Long userId, long lastCursor, ItemOrderType orderType, String state) {
85-
8677
DateExpression<Date> currentWeekExpr = currentDate();
8778
DateTimePath<LocalDateTime> createdAtExpr = item.createdAt;
8879

80+
var isLikedSubQuery = JPAExpressions.select(itemLike.id)
81+
.from(itemLike)
82+
.where(itemLike.item.id.eq(item.id)
83+
.and(itemLike.user.id.eq(userId)));
84+
8985
var query = queryFactory.select(
9086
Projections.constructor(
9187
ItemDao.class,
92-
orderType == ItemOrderType.MOST_LIKED ? Expressions.constant(1): createdAtExpr.week().subtract(currentWeekExpr.week()).abs().as("weekAgo"),
88+
orderType == ItemOrderType.MOST_LIKED ? Expressions.constant(1) : createdAtExpr.week().subtract(currentWeekExpr.week()).abs().as("weekAgo"),
9389
item.id,
9490
item.content,
9591
item.createdAt,
@@ -99,7 +95,7 @@ public List<ItemDao> findByUserIdAndState(Long userId, long lastCursor, ItemOrde
9995
artist.name.as("artistName"),
10096
albumCover.albumThumbnail.as("albumThumbnail"),
10197
itemLike.count().as("itemCount"),
102-
itemLike.user.id.eq(userId).as("isLiked")
98+
isLikedSubQuery.exists().as("isLiked")
10399
)
104100
).from(item)
105101
.join(itemLocation).on(item.id.eq(itemLocation.item.id))
@@ -113,22 +109,21 @@ public List<ItemDao> findByUserIdAndState(Long userId, long lastCursor, ItemOrde
113109
.where(villageArea.cityArea.stateArea.stateName.eq(state))
114110
.groupBy(item.id, item.content, item.createdAt, itemLocation.name, song.name, album.name, artist.name, albumCover.albumThumbnail);
115111

116-
117-
query = switch (orderType) {
118-
case RECENT -> query.orderBy(item.createdAt.desc());
119-
case OLDEST -> query.orderBy(item.createdAt.asc());
120-
case MOST_LIKED -> query.orderBy(itemLike.count().desc());
121-
};
112+
orderBy(orderType, query);
122113

123114
return query.fetch();
124115
}
125116

126117
@Override
127118
public List<ItemDao> findByUserIdAndCity(Long userId, long lastCursor, ItemOrderType orderType, String city) {
128-
129119
DateExpression<Date> currentWeekExpr = currentDate();
130120
DateTimePath<LocalDateTime> createdAtExpr = item.createdAt;
131121

122+
var isLikedSubQuery = JPAExpressions.select(itemLike.id)
123+
.from(itemLike)
124+
.where(itemLike.item.id.eq(item.id)
125+
.and(itemLike.user.id.eq(userId)));
126+
132127
var query = queryFactory.select(
133128
Projections.constructor(
134129
ItemDao.class,
@@ -142,7 +137,7 @@ public List<ItemDao> findByUserIdAndCity(Long userId, long lastCursor, ItemOrder
142137
artist.name.as("artistName"),
143138
albumCover.albumThumbnail.as("albumThumbnail"),
144139
itemLike.count().as("itemCount"),
145-
itemLike.user.id.eq(userId).as("isLiked")
140+
isLikedSubQuery.exists().as("isLiked")
146141
)
147142
).from(item)
148143
.join(itemLocation).on(item.id.eq(itemLocation.item.id))
@@ -156,13 +151,17 @@ public List<ItemDao> findByUserIdAndCity(Long userId, long lastCursor, ItemOrder
156151
.where(villageArea.cityArea.cityName.eq(city))
157152
.groupBy(item.id, item.content, item.createdAt, itemLocation.name, song.name, album.name, artist.name, albumCover.albumThumbnail);
158153

154+
orderBy(orderType, query);
155+
156+
return query.fetch();
157+
}
159158

160-
query = switch (orderType) {
159+
private void orderBy(ItemOrderType orderType, JPAQuery<ItemDao> query) {
160+
switch (orderType) {
161161
case RECENT -> query.orderBy(item.createdAt.desc());
162162
case OLDEST -> query.orderBy(item.createdAt.asc());
163163
case MOST_LIKED -> query.orderBy(itemLike.count().desc());
164-
};
165-
166-
return query.fetch();
164+
}
167165
}
168-
}
166+
167+
}

backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/QueryDslItemLikeRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import java.util.List;
88

99
public interface QueryDslItemLikeRepository {
10-
List<UserItemLikeDao> findByUserId(Long userId, Long lastCursor, ItemOrderType itemOrderType);
1110
List<UserItemPointDao> findUserLikedItemsPoints(Long userId);
11+
List<UserItemLikeDao> findByUserId(Long userId, Long lastCursor, ItemOrderType itemOrderType);
1212
List<UserItemLikeDao> findByUserIdAndState(Long userId, Long lastCursor, ItemOrderType itemOrderType, String state);
1313
List<UserItemLikeDao> findByUserIdAndCity(Long userId, Long lastCursor, ItemOrderType itemOrderType, String city);
1414
}

0 commit comments

Comments
 (0)