From 19c0959c1437e2c0bb1957e9311051f0f7e36ff8 Mon Sep 17 00:00:00 2001 From: tmddus2 Date: Sat, 10 Aug 2024 03:51:57 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20=EC=A7=91=EA=B3=84?= =?UTF-8?q?=ED=95=A8=EC=88=98=20=EA=B0=92=20=ED=95=84=EC=9A=94=20=EC=97=86?= =?UTF-8?q?=EC=9D=84=20=EB=95=8C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../filter/repository/FilterRepository.java | 12 +++---- .../domain/filter/service/FilterService.java | 34 ++++++++++++++----- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/purithm/src/main/java/com/example/purithm/domain/filter/repository/FilterRepository.java b/purithm/src/main/java/com/example/purithm/domain/filter/repository/FilterRepository.java index 898c74d..4e20327 100644 --- a/purithm/src/main/java/com/example/purithm/domain/filter/repository/FilterRepository.java +++ b/purithm/src/main/java/com/example/purithm/domain/filter/repository/FilterRepository.java @@ -11,14 +11,14 @@ @Repository public interface FilterRepository extends JpaRepository { - Page findAllByOs(OS os, Pageable pageable); + @Query("SELECT f FROM Filter f WHERE f.os=:os ORDER BY f.createdAt DESC") + Page findAllByOs(OS os, Pageable pageable); @Query("SELECT f, COUNT(fl.id) AS like_count " + "FROM Filter f " + - "LEFT JOIN FilterLike fl " + - "ON f.id=fl.filter.id " + - "WHERE f.os=:os " + + "LEFT JOIN FilterLike fl ON f.id = fl.filter.id " + + "WHERE f.os = :os " + "GROUP BY f.id " + - "ORDER BY like_count DESC") + "ORDER BY like_count DESC, f.id ASC") Page findAllWithLikeSorting(OS os, Pageable pageable); @Query("SELECT f, AVG(r.pureDegree) AS avg " + @@ -26,7 +26,7 @@ public interface FilterRepository extends JpaRepository { "LEFT JOIN Review r ON f.id = r.filter.id " + "WHERE f.os=:os " + "GROUP BY f.id " + - "ORDER BY avg DESC") + "ORDER BY avg DESC, f.id ASC") Page findAllWithReviewSorting(OS os, Pageable pageable); } diff --git a/purithm/src/main/java/com/example/purithm/domain/filter/service/FilterService.java b/purithm/src/main/java/com/example/purithm/domain/filter/service/FilterService.java index 64a1910..26fb39a 100644 --- a/purithm/src/main/java/com/example/purithm/domain/filter/service/FilterService.java +++ b/purithm/src/main/java/com/example/purithm/domain/filter/service/FilterService.java @@ -57,24 +57,42 @@ public FilterListDto getFilters(Long id, int page, int size, OS os, String tag, PageRequest pageRequest = PageRequest.of(page, size); Page filters; + boolean isLast; + List filterDtos; if (tag == null) { if (sortedBy.equals("popular")) { filters = filterRepository.findAllWithLikeSorting(os, pageRequest); + isLast = filters.isLast(); + filterDtos = filters.getContent().stream().map(filter -> + FilterDto.of( + (Filter) filter[0], + user.getMembership(), + isLike(((Filter) filter[0]).getId(), id), + filterLikeRepository.getLikes((Filter) filter[0]))).toList(); } else if (sortedBy.equals("pure")) { filters = filterRepository.findAllWithReviewSorting(os, pageRequest); - + isLast = filters.isLast(); + filterDtos = filters.getContent().stream().map(filter -> + FilterDto.of( + (Filter) filter[0], + user.getMembership(), + isLike(((Filter) filter[0]).getId(), id), + filterLikeRepository.getLikes((Filter) filter[0]))).toList(); } else { pageRequest = PageRequest.of(page, size, Sort.by("createdAt").descending()); // 정렬 없을 때는 최신 순 - filters = filterRepository.findAllByOs(os, pageRequest); + Page filterByLatest = filterRepository.findAllByOs(os, pageRequest); + isLast = filterByLatest.isLast(); + filterDtos = filterByLatest.getContent().stream().map(filter -> + FilterDto.of( + filter, + user.getMembership(), + isLike((filter).getId(), id), + filterLikeRepository.getLikes(filter))).toList(); } return FilterListDto.builder() - .isLast(filters.isLast()) - .filters(filters.getContent().stream().map(filter -> - FilterDto.of((Filter) filter[0], - user.getMembership(), - isLike(((Filter) filter[0]).getId(), id), - filterLikeRepository.getLikes((Filter) filter[0]))).toList()).build(); + .isLast(isLast) + .filters(filterDtos).build(); } filters = tagRepository.findFilterByTagAndOs(tag, os, pageRequest);