Skip to content

Commit

Permalink
✨ hotfix(api): hotfix sorting by date (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
seonghun-dev authored Jul 21, 2023
1 parent 988442a commit 1962dae
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public interface ItemLikeRepository extends JpaRepository<ItemLike, Long> {
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<ItemLike> findByUserId(@Param("userId") Long userId, @Param("lastCursor") long lastCursor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ public interface ItemRepository extends JpaRepository<Item, Long> {
Optional<Item> 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<Item> findByUserId(@Param("userId") Long userId, @Param("lastCursor") long lastCursor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
};
}
}

0 comments on commit 1962dae

Please sign in to comment.