Skip to content

Commit

Permalink
🐛 fix(api): cannot find user items filtered by location (#533)
Browse files Browse the repository at this point in the history
* ♻️ refactor(api): extract private method ordering by ItemOrderType

* 🐛 fix(api): cannot find user items filtered by location
  • Loading branch information
siyeonSon authored Dec 29, 2024
1 parent d97752e commit 73221b4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.querydsl.core.types.dsl.DateExpression;
import com.querydsl.core.types.dsl.DateTimePath;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
Expand All @@ -35,9 +36,26 @@ public class ItemLikeRepositoryImpl implements QueryDslItemLikeRepository {

private final JPAQueryFactory queryFactory;

public List<UserItemPointDao> findUserLikedItemsPoints(Long userId) {
return queryFactory.select(
Projections.fields(
UserItemPointDao.class,
itemLocation.point,
item.id,
albumCover.albumThumbnail
))
.from(itemLike)
.join(itemLike.item, item)
.on(itemLike.item.id.eq(item.id))
.join(item.itemLocation, itemLocation)
.on(item.itemLocation.id.eq(itemLocation.id))
.join(itemLocation.item.albumCover, albumCover)
.on(item.albumCover.id.eq(albumCover.id))
.where(itemLike.user.id.eq(userId))
.fetch();
}

public List<UserItemLikeDao> findByUserId(Long userId, Long lastCursor, ItemOrderType itemOrderType) {

public List<UserItemLikeDao> findByUserId(Long userId, Long lastCursor, ItemOrderType orderType) {
DateExpression<Date> currentWeekExpr = currentDate();
DateTimePath<LocalDateTime> createdAtExpr = itemLike.createdAt;
QItemLike innerItemLike = new QItemLike("innerItemLike");
Expand All @@ -49,7 +67,7 @@ public List<UserItemLikeDao> findByUserId(Long userId, Long lastCursor, ItemOrde
var query = queryFactory.select(
Projections.constructor(
UserItemLikeDao.class,
itemOrderType == ItemOrderType.MOST_LIKED ? Expressions.constant(1): createdAtExpr.week().subtract(currentWeekExpr.week()).abs().as("weekAgo"),
orderType == ItemOrderType.MOST_LIKED ? Expressions.constant(1): createdAtExpr.week().subtract(currentWeekExpr.week()).abs().as("weekAgo"),
item.id,
item.content,
itemLike.createdAt,
Expand All @@ -72,38 +90,13 @@ public List<UserItemLikeDao> findByUserId(Long userId, Long lastCursor, ItemOrde
.join(user).on(user.eq(item.user))
.where(itemLike.user.id.eq(userId));


query = switch (itemOrderType) {
case RECENT -> query.orderBy(itemLike.createdAt.desc());
case OLDEST -> query.orderBy(itemLike.createdAt.asc());
case MOST_LIKED -> query.orderBy(Expressions.numberTemplate(Long.class, "({0})", likeCountExpr).desc());
};
orderBy(orderType, query);

return query.fetch();
}


public List<UserItemPointDao> findUserLikedItemsPoints(Long userId) {
return queryFactory.select(
Projections.fields(
UserItemPointDao.class,
itemLocation.point,
item.id,
albumCover.albumThumbnail
))
.from(itemLike)
.join(itemLike.item, item)
.on(itemLike.item.id.eq(item.id))
.join(item.itemLocation, itemLocation)
.on(item.itemLocation.id.eq(itemLocation.id))
.join(itemLocation.item.albumCover, albumCover)
.on(item.albumCover.id.eq(albumCover.id))
.where(itemLike.user.id.eq(userId))
.fetch();
}

@Override
public List<UserItemLikeDao> findByUserIdAndState(Long userId, Long lastCursor, ItemOrderType itemOrderType, String state) {
public List<UserItemLikeDao> findByUserIdAndState(Long userId, Long lastCursor, ItemOrderType orderType, String state) {
DateExpression<Date> currentWeekExpr = currentDate();
DateTimePath<LocalDateTime> createdAtExpr = itemLike.createdAt;
QItemLike innerItemLike = new QItemLike("innerItemLike");
Expand All @@ -115,7 +108,7 @@ public List<UserItemLikeDao> findByUserIdAndState(Long userId, Long lastCursor,
var query = queryFactory.select(
Projections.constructor(
UserItemLikeDao.class,
itemOrderType == ItemOrderType.MOST_LIKED ? Expressions.constant(1):createdAtExpr.week().subtract(currentWeekExpr.week()).abs().as("weekAgo"),
orderType == ItemOrderType.MOST_LIKED ? Expressions.constant(1):createdAtExpr.week().subtract(currentWeekExpr.week()).abs().as("weekAgo"),
item.id,
item.content,
itemLike.createdAt,
Expand All @@ -140,20 +133,13 @@ public List<UserItemLikeDao> findByUserIdAndState(Long userId, Long lastCursor,
.where(itemLike.user.id.eq(userId))
.where(villageArea.cityArea.stateArea.stateName.eq(state));


query = switch (itemOrderType) {
case RECENT -> query.orderBy(itemLike.createdAt.desc());
case OLDEST -> query.orderBy(itemLike.createdAt.asc());
case MOST_LIKED -> query.orderBy(Expressions.numberTemplate(Long.class, "({0})", likeCountExpr).desc());
};
orderBy(orderType, query);

return query.fetch();
}



@Override
public List<UserItemLikeDao> findByUserIdAndCity(Long userId, Long lastCursor, ItemOrderType itemOrderType, String city) {
public List<UserItemLikeDao> findByUserIdAndCity(Long userId, Long lastCursor, ItemOrderType orderType, String city) {
DateExpression<Date> currentWeekExpr = currentDate();
DateTimePath<LocalDateTime> createdAtExpr = itemLike.createdAt;
QItemLike innerItemLike = new QItemLike("innerItemLike");
Expand All @@ -165,7 +151,7 @@ public List<UserItemLikeDao> findByUserIdAndCity(Long userId, Long lastCursor, I
var query = queryFactory.select(
Projections.constructor(
UserItemLikeDao.class,
itemOrderType == ItemOrderType.MOST_LIKED ? Expressions.constant(1): createdAtExpr.week().subtract(currentWeekExpr.week()).abs().as("weekAgo"),
orderType == ItemOrderType.MOST_LIKED ? Expressions.constant(1): createdAtExpr.week().subtract(currentWeekExpr.week()).abs().as("weekAgo"),
item.id,
item.content,
itemLike.createdAt,
Expand All @@ -190,14 +176,17 @@ public List<UserItemLikeDao> findByUserIdAndCity(Long userId, Long lastCursor, I
.where(itemLike.user.id.eq(userId))
.where(villageArea.cityArea.cityName.eq(city));


query = switch (itemOrderType) {
case RECENT -> query.orderBy(itemLike.createdAt.desc());
case OLDEST -> query.orderBy(itemLike.createdAt.asc());
case MOST_LIKED -> query.orderBy(Expressions.numberTemplate(Long.class, "({0})", likeCountExpr).desc());
};
orderBy(orderType, query);

return query.fetch();
}

private void orderBy(ItemOrderType orderType, JPAQuery<UserItemLikeDao> query) {
switch (orderType) {
case RECENT -> query.orderBy(item.createdAt.desc());
case OLDEST -> query.orderBy(item.createdAt.asc());
case MOST_LIKED -> query.orderBy(itemLike.count().desc());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.depromeet.domains.item.dao.ItemDao;
import com.depromeet.domains.user.dto.request.ItemOrderType;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.CaseBuilder;
import com.querydsl.core.types.dsl.DateExpression;
import com.querydsl.core.types.dsl.DateTimePath;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
Expand All @@ -25,25 +25,22 @@
import static com.depromeet.music.artist.QArtist.artist;
import static com.depromeet.music.song.QSong.song;
import static com.querydsl.core.types.dsl.Expressions.currentDate;
import static com.querydsl.jpa.JPAExpressions.select;

@Repository
@RequiredArgsConstructor
public class ItemRepositoryImpl implements QueryDslItemRepository {


private final JPAQueryFactory queryFactory;

@Override
public List<ItemDao> findByUserId(Long userId, long lastCursor, ItemOrderType orderType) {

DateExpression<Date> currentWeekExpr = currentDate();
DateTimePath<LocalDateTime> createdAtExpr = item.createdAt;

var isLikedSubQuery = JPAExpressions.select(itemLike.id)
.from(itemLike)
.where(itemLike.item.id.eq(item.id)
.and(itemLike.user.id.eq(userId)));
.and(itemLike.user.id.eq(userId)));

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


query = switch (orderType) {
case RECENT -> query.orderBy(item.createdAt.desc());
case OLDEST -> query.orderBy(item.createdAt.asc());
case MOST_LIKED -> query.orderBy(itemLike.count().desc());
};
orderBy(orderType, query);

return query.fetch();
}

@Override
public List<ItemDao> findByUserIdAndState(Long userId, long lastCursor, ItemOrderType orderType, String state) {

DateExpression<Date> currentWeekExpr = currentDate();
DateTimePath<LocalDateTime> createdAtExpr = item.createdAt;

var isLikedSubQuery = JPAExpressions.select(itemLike.id)
.from(itemLike)
.where(itemLike.item.id.eq(item.id)
.and(itemLike.user.id.eq(userId)));

var query = queryFactory.select(
Projections.constructor(
ItemDao.class,
orderType == ItemOrderType.MOST_LIKED ? Expressions.constant(1): createdAtExpr.week().subtract(currentWeekExpr.week()).abs().as("weekAgo"),
orderType == ItemOrderType.MOST_LIKED ? Expressions.constant(1) : createdAtExpr.week().subtract(currentWeekExpr.week()).abs().as("weekAgo"),
item.id,
item.content,
item.createdAt,
Expand All @@ -99,7 +95,7 @@ public List<ItemDao> findByUserIdAndState(Long userId, long lastCursor, ItemOrde
artist.name.as("artistName"),
albumCover.albumThumbnail.as("albumThumbnail"),
itemLike.count().as("itemCount"),
itemLike.user.id.eq(userId).as("isLiked")
isLikedSubQuery.exists().as("isLiked")
)
).from(item)
.join(itemLocation).on(item.id.eq(itemLocation.item.id))
Expand All @@ -113,22 +109,21 @@ public List<ItemDao> findByUserIdAndState(Long userId, long lastCursor, ItemOrde
.where(villageArea.cityArea.stateArea.stateName.eq(state))
.groupBy(item.id, item.content, item.createdAt, itemLocation.name, song.name, album.name, artist.name, albumCover.albumThumbnail);


query = switch (orderType) {
case RECENT -> query.orderBy(item.createdAt.desc());
case OLDEST -> query.orderBy(item.createdAt.asc());
case MOST_LIKED -> query.orderBy(itemLike.count().desc());
};
orderBy(orderType, query);

return query.fetch();
}

@Override
public List<ItemDao> findByUserIdAndCity(Long userId, long lastCursor, ItemOrderType orderType, String city) {

DateExpression<Date> currentWeekExpr = currentDate();
DateTimePath<LocalDateTime> createdAtExpr = item.createdAt;

var isLikedSubQuery = JPAExpressions.select(itemLike.id)
.from(itemLike)
.where(itemLike.item.id.eq(item.id)
.and(itemLike.user.id.eq(userId)));

var query = queryFactory.select(
Projections.constructor(
ItemDao.class,
Expand All @@ -142,7 +137,7 @@ public List<ItemDao> findByUserIdAndCity(Long userId, long lastCursor, ItemOrder
artist.name.as("artistName"),
albumCover.albumThumbnail.as("albumThumbnail"),
itemLike.count().as("itemCount"),
itemLike.user.id.eq(userId).as("isLiked")
isLikedSubQuery.exists().as("isLiked")
)
).from(item)
.join(itemLocation).on(item.id.eq(itemLocation.item.id))
Expand All @@ -156,13 +151,17 @@ public List<ItemDao> findByUserIdAndCity(Long userId, long lastCursor, ItemOrder
.where(villageArea.cityArea.cityName.eq(city))
.groupBy(item.id, item.content, item.createdAt, itemLocation.name, song.name, album.name, artist.name, albumCover.albumThumbnail);

orderBy(orderType, query);

return query.fetch();
}

query = switch (orderType) {
private void orderBy(ItemOrderType orderType, JPAQuery<ItemDao> query) {
switch (orderType) {
case RECENT -> query.orderBy(item.createdAt.desc());
case OLDEST -> query.orderBy(item.createdAt.asc());
case MOST_LIKED -> query.orderBy(itemLike.count().desc());
};

return query.fetch();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import java.util.List;

public interface QueryDslItemLikeRepository {
List<UserItemLikeDao> findByUserId(Long userId, Long lastCursor, ItemOrderType itemOrderType);
List<UserItemPointDao> findUserLikedItemsPoints(Long userId);
List<UserItemLikeDao> findByUserId(Long userId, Long lastCursor, ItemOrderType itemOrderType);
List<UserItemLikeDao> findByUserIdAndState(Long userId, Long lastCursor, ItemOrderType itemOrderType, String state);
List<UserItemLikeDao> findByUserIdAndCity(Long userId, Long lastCursor, ItemOrderType itemOrderType, String city);
}

0 comments on commit 73221b4

Please sign in to comment.