From 364d49cf32139a607d2691890e19b2b0f5919e98 Mon Sep 17 00:00:00 2001 From: siyeonSon Date: Sun, 29 Dec 2024 11:26:36 +0900 Subject: [PATCH 1/2] :recycle: refactor(api): extract private method ordering by ItemOrderType --- .../repository/ItemLikeRepositoryImpl.java | 83 ++++++++----------- .../item/repository/ItemRepositoryImpl.java | 34 +++----- .../QueryDslItemLikeRepository.java | 2 +- 3 files changed, 49 insertions(+), 70 deletions(-) diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemLikeRepositoryImpl.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemLikeRepositoryImpl.java index 46635e13..2addd765 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemLikeRepositoryImpl.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemLikeRepositoryImpl.java @@ -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; @@ -35,9 +36,26 @@ public class ItemLikeRepositoryImpl implements QueryDslItemLikeRepository { private final JPAQueryFactory queryFactory; + public List 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 findByUserId(Long userId, Long lastCursor, ItemOrderType itemOrderType) { - + public List findByUserId(Long userId, Long lastCursor, ItemOrderType orderType) { DateExpression currentWeekExpr = currentDate(); DateTimePath createdAtExpr = itemLike.createdAt; QItemLike innerItemLike = new QItemLike("innerItemLike"); @@ -49,7 +67,7 @@ public List 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, @@ -72,38 +90,13 @@ public List 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 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 findByUserIdAndState(Long userId, Long lastCursor, ItemOrderType itemOrderType, String state) { + public List findByUserIdAndState(Long userId, Long lastCursor, ItemOrderType orderType, String state) { DateExpression currentWeekExpr = currentDate(); DateTimePath createdAtExpr = itemLike.createdAt; QItemLike innerItemLike = new QItemLike("innerItemLike"); @@ -115,7 +108,7 @@ public List 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, @@ -140,20 +133,13 @@ public List 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 findByUserIdAndCity(Long userId, Long lastCursor, ItemOrderType itemOrderType, String city) { + public List findByUserIdAndCity(Long userId, Long lastCursor, ItemOrderType orderType, String city) { DateExpression currentWeekExpr = currentDate(); DateTimePath createdAtExpr = itemLike.createdAt; QItemLike innerItemLike = new QItemLike("innerItemLike"); @@ -165,7 +151,7 @@ public List 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, @@ -190,14 +176,17 @@ public List 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 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()); + } + } + } diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemRepositoryImpl.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemRepositoryImpl.java index 1a737461..d6d32e9b 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemRepositoryImpl.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemRepositoryImpl.java @@ -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; @@ -25,7 +25,6 @@ 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 @@ -36,7 +35,6 @@ public class ItemRepositoryImpl implements QueryDslItemRepository { @Override public List findByUserId(Long userId, long lastCursor, ItemOrderType orderType) { - DateExpression currentWeekExpr = currentDate(); DateTimePath createdAtExpr = item.createdAt; @@ -70,19 +68,13 @@ public List 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 findByUserIdAndState(Long userId, long lastCursor, ItemOrderType orderType, String state) { - DateExpression currentWeekExpr = currentDate(); DateTimePath createdAtExpr = item.createdAt; @@ -113,19 +105,13 @@ public List 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 findByUserIdAndCity(Long userId, long lastCursor, ItemOrderType orderType, String city) { - DateExpression currentWeekExpr = currentDate(); DateTimePath createdAtExpr = item.createdAt; @@ -156,13 +142,17 @@ public List 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 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(); + } } -} \ No newline at end of file + +} diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/QueryDslItemLikeRepository.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/QueryDslItemLikeRepository.java index 0970d90e..6de8c2d3 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/QueryDslItemLikeRepository.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/QueryDslItemLikeRepository.java @@ -7,8 +7,8 @@ import java.util.List; public interface QueryDslItemLikeRepository { - List findByUserId(Long userId, Long lastCursor, ItemOrderType itemOrderType); List findUserLikedItemsPoints(Long userId); + List findByUserId(Long userId, Long lastCursor, ItemOrderType itemOrderType); List findByUserIdAndState(Long userId, Long lastCursor, ItemOrderType itemOrderType, String state); List findByUserIdAndCity(Long userId, Long lastCursor, ItemOrderType itemOrderType, String city); } From d30f7d14286189606733e3ff7fcdfcd4b884f2d8 Mon Sep 17 00:00:00 2001 From: siyeonSon Date: Sun, 29 Dec 2024 12:03:07 +0900 Subject: [PATCH 2/2] :bug: fix(api): cannot find user items filtered by location --- .../item/repository/ItemRepositoryImpl.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemRepositoryImpl.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemRepositoryImpl.java index d6d32e9b..3b1229e8 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemRepositoryImpl.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemRepositoryImpl.java @@ -30,7 +30,6 @@ @RequiredArgsConstructor public class ItemRepositoryImpl implements QueryDslItemRepository { - private final JPAQueryFactory queryFactory; @Override @@ -41,7 +40,7 @@ public List findByUserId(Long userId, long lastCursor, ItemOrderType or 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( @@ -78,10 +77,15 @@ public List findByUserIdAndState(Long userId, long lastCursor, ItemOrde DateExpression currentWeekExpr = currentDate(); DateTimePath 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, @@ -91,7 +95,7 @@ public List 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)) @@ -115,6 +119,11 @@ public List findByUserIdAndCity(Long userId, long lastCursor, ItemOrder DateExpression currentWeekExpr = currentDate(); DateTimePath 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, @@ -128,7 +137,7 @@ public List 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))