Skip to content

Commit

Permalink
Merge pull request #32 from Central-MakeUs/14-필터를-조회한다
Browse files Browse the repository at this point in the history
🐛 fix: 집계함수 값 필요 없을 때 추가
  • Loading branch information
tmddus2 authored Aug 9, 2024
2 parents 8fc2167 + 19c0959 commit b96966a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@

@Repository
public interface FilterRepository extends JpaRepository<Filter, Long> {
Page<Object[]> findAllByOs(OS os, Pageable pageable);
@Query("SELECT f FROM Filter f WHERE f.os=:os ORDER BY f.createdAt DESC")
Page<Filter> 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<Object[]> findAllWithLikeSorting(OS os, Pageable pageable);

@Query("SELECT f, AVG(r.pureDegree) AS avg " +
"FROM Filter f " +
"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<Object[]> findAllWithReviewSorting(OS os, Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,42 @@ public FilterListDto getFilters(Long id, int page, int size, OS os, String tag,

PageRequest pageRequest = PageRequest.of(page, size);
Page<Object[]> filters;
boolean isLast;
List<FilterDto> 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<Filter> 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);
Expand Down

0 comments on commit b96966a

Please sign in to comment.