From aa69c95c5deca9fbbae79290c3c1350bb40ae5bc Mon Sep 17 00:00:00 2001 From: Jung Seonghun <80201773+seonghun-dev@users.noreply.github.com> Date: Sat, 22 Jul 2023 00:20:08 +0900 Subject: [PATCH] :sparkles: hotfix(api): hotfix sorting by date --- .../domains/item/repository/ItemLikeRepository.java | 5 +++-- .../domains/item/repository/ItemRepository.java | 7 ++++--- .../domains/user/service/UserItemService.java | 5 +++++ .../src/main/java/com/depromeet/util/WeekUtil.java | 11 +++++++++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemLikeRepository.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemLikeRepository.java index 7c3bc6da..2fb203cb 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemLikeRepository.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemLikeRepository.java @@ -19,8 +19,9 @@ public interface ItemLikeRepository extends JpaRepository { int countByItemId(Long itemId); @Query(value = """ - SELECT il FROM ItemLike il JOIN FETCh Item - WHERE il.user.id = :userId AND il.id < :lastCursor + SELECT il FROM ItemLike il JOIN FETCH il.item JOIN FETCH il.item.itemLocation + JOIN FETCH il.item.albumCover JOIN FETCH il.item.song JOIN FETCH il.item.song.album + WHERE il.user.id = :userId AND il.id < :lastCursor ORDER BY il.id DESC """) List findByUserId(@Param("userId") Long userId, @Param("lastCursor") long lastCursor); diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemRepository.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemRepository.java index 8d476922..554ed93f 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemRepository.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/item/repository/ItemRepository.java @@ -22,9 +22,10 @@ public interface ItemRepository extends JpaRepository { Optional findById(Long itemId); @Query(""" - SELECT i FROM Item i JOIN FETCH i.itemLocation - JOIN FETCH ItemLike JOIN FETCH i.user JOIN FETCH i.song - WHERE i.user.id = :userId AND i.id < :lastCursor + SELECT i FROM Item i JOIN FETCH i.itemLocation + JOIN FETCH i.likes JOIN FETCH i.user JOIN FETCH i.song JOIN FETCH i.albumCover + JOIN FETCH i.song.album JOIN FETCH i.song.album.artist + WHERE i.user.id = :userId AND i.id < :lastCursor ORDER BY i.id DESC """) List findByUserId(@Param("userId") Long userId, @Param("lastCursor") long lastCursor); diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/user/service/UserItemService.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/user/service/UserItemService.java index 0ce5c391..9f7ba232 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/domains/user/service/UserItemService.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/user/service/UserItemService.java @@ -18,9 +18,11 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; +import static com.depromeet.util.WeekUtil.getWeekString2Int; import static com.depromeet.util.WeekUtil.getWeeksAgo; @Service @@ -70,7 +72,9 @@ private static class ItemGroupWithDateResponseDto { .entrySet() .stream() .map(entry -> new ItemGroupByDateResponseDto(entry.getKey(), entry.getValue())) + .sorted(Comparator.comparingInt(dto -> getWeekString2Int(dto.date()))) .toList(); + var meta = new InfiniteScrollMetaResponseDto(itemList.size(), -1); return new InfiniteScrollResponseDto<>(itemGroupByDateResponseDtoList, meta); @@ -107,6 +111,7 @@ private static class ItemGroupWithDateResponseDto { .entrySet() .stream() .map(entry -> new ItemGroupByDateResponseDto(entry.getKey(), entry.getValue())) + .sorted(Comparator.comparingInt(dto -> getWeekString2Int(dto.date()))) .toList(); var meta = new InfiniteScrollMetaResponseDto(itemLikeList.size(), -1); diff --git a/backend/streetdrop-common/src/main/java/com/depromeet/util/WeekUtil.java b/backend/streetdrop-common/src/main/java/com/depromeet/util/WeekUtil.java index afd0bcc6..6d6fea0a 100644 --- a/backend/streetdrop-common/src/main/java/com/depromeet/util/WeekUtil.java +++ b/backend/streetdrop-common/src/main/java/com/depromeet/util/WeekUtil.java @@ -16,4 +16,15 @@ public static String getWeeksAgo(LocalDateTime date) { weeksDiff == 1 ? LAST_WEEK : String.format(WEEKS_AGO, weeksDiff); } + + public static int getWeekString2Int(String week) { + return switch (week) { + case "이번 주" -> 0; + case "지난 주" -> 1; + default -> { + String[] split = week.split("주 전"); + yield Integer.parseInt(split[0]); + } + }; + } }