Skip to content

Commit

Permalink
Merge pull request #106 from Central-MakeUs/105-정렬-기준-수정
Browse files Browse the repository at this point in the history
Feat(#105): 정렬 기준 수정
  • Loading branch information
tmddus2 authored Aug 28, 2024
2 parents 7aa6a64 + ed6b698 commit 193e126
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public SuccessResponse<FilterListDto> getFilters(
examples =
{@ExampleObject(name = "최신순", summary = "최신순 정렬", value = "latest"),
@ExampleObject(name = "오래된순", summary = "오래된순 정렬", value = "earliest"),
@ExampleObject(name = "퓨어지수 높은순", summary = "퓨어지수 높은순 정렬", value = "popular")}) String sortedBy,
@ExampleObject(name = "퓨어지수 높은순", summary = "퓨어지수 높은순 정렬", value = "popular"),
@ExampleObject(name = "이름순", summary = "이름순 정렬", value = "name"),
@ExampleObject(name = "등급 낮은순", summary = "등급 낮은순 정렬", value = "membership")}) String sortedBy,
@RequestParam(value = "page") int page,
@RequestParam(value = "size") int size
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface CustomFilterRepository {
Page<Object[]> findAllWithLikeSorting(OS os, Tag tag, Long photographerId, Pageable pageable);
Page<Object[]> findAllWithReviewSorting(OS os, Tag tag, Long photographerId, Pageable pageable);
Page<Object[]> findAllWithViewsSorting(OS os, Long photographerId, Pageable pageable);
Page<Filter> findAllWithNameSorting(OS os, Tag tag, Long photographerId, Pageable pageable);
Page<Filter> findAllWithMembershipSorting(OS os, Tag tag, Long photographerId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ public class CustomFilterRepositoryImpl implements CustomFilterRepository {

@Override
public Page<Filter> findAllByOs(OS os, Tag tag, Long photographerId, Pageable pageable) {
BooleanBuilder builder = new BooleanBuilder();
builder.and(filter.os.eq(os));
if (tag != null) {
builder.and(filter.tag.eq(tag));
}
if (photographerId != null) {
builder.and(filter.photographer.id.eq(photographerId));
}
BooleanBuilder builder = this.createBuilder(os, tag, photographerId);

List<Filter> results = jpaQueryFactory
.selectFrom(filter)
Expand All @@ -57,14 +50,7 @@ public Page<Filter> findAllByOs(OS os, Tag tag, Long photographerId, Pageable pa

@Override
public Page<Object[]> findAllWithLikeSorting(OS os, Tag tag, Long photographerId, Pageable pageable) {
BooleanBuilder builder = new BooleanBuilder();
builder.and(filter.os.eq(os));
if (tag != null) {
builder.and(filter.tag.eq(tag));
}
if (photographerId != null) {
builder.and(filter.photographer.id.eq(photographerId));
}
BooleanBuilder builder = this.createBuilder(os, tag, photographerId);

List<Tuple> tuples = jpaQueryFactory
.select(filter, filterLike.count())
Expand Down Expand Up @@ -93,14 +79,7 @@ public Page<Object[]> findAllWithLikeSorting(OS os, Tag tag, Long photographerId

@Override
public Page<Object[]> findAllWithReviewSorting(OS os, Tag tag, Long photographerId, Pageable pageable) {
BooleanBuilder builder = new BooleanBuilder();
builder.and(filter.os.eq(os));
if (tag != null) {
builder.and(filter.tag.eq(tag));
}
if (photographerId != null) {
builder.and(filter.photographer.id.eq(photographerId));
}
BooleanBuilder builder = this.createBuilder(os, tag, photographerId);

List<Tuple> tuples = jpaQueryFactory
.select(filter, review.pureDegree.avg().as("avg"))
Expand Down Expand Up @@ -129,11 +108,7 @@ public Page<Object[]> findAllWithReviewSorting(OS os, Tag tag, Long photographer

@Override
public Page<Object[]> findAllWithViewsSorting(OS os, Long photographerId, Pageable pageable) {
BooleanBuilder builder = new BooleanBuilder();
builder.and(filter.os.eq(os));
if (photographerId != null) {
builder.and(filter.photographer.id.eq(photographerId));
}
BooleanBuilder builder = this.createBuilder(os, null, photographerId);

List<Tuple> tuples = jpaQueryFactory
.select(filter, userFilterLog.filterId.count().as("count"))
Expand All @@ -159,4 +134,58 @@ public Page<Object[]> findAllWithViewsSorting(OS os, Long photographerId, Pageab

return new PageImpl<>(results, pageable, total);
}

@Override
public Page<Filter> findAllWithNameSorting(OS os, Tag tag, Long photographerId, Pageable pageable) {
BooleanBuilder builder = this.createBuilder(os, tag, photographerId);

List<Filter> results = jpaQueryFactory
.selectFrom(filter)
.where(builder)
.orderBy(filter.name.asc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

long total = jpaQueryFactory
.select(filter.count())
.from(filter)
.where(builder).fetchOne();

return new PageImpl<>(results, pageable, total);
}

@Override
public Page<Filter> findAllWithMembershipSorting(OS os, Tag tag, Long photographerId, Pageable pageable) {
BooleanBuilder builder = this.createBuilder(os, tag, photographerId);

List<Filter> results = jpaQueryFactory
.selectFrom(filter)
.where(builder)
.orderBy(filter.membership.asc(), filter.name.asc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

long total = jpaQueryFactory
.select(filter.count())
.from(filter)
.where(builder).fetchOne();

return new PageImpl<>(results, pageable, total);
}

private BooleanBuilder createBuilder(OS os, Tag tag, Long photographerId) {
BooleanBuilder builder = new BooleanBuilder();

builder.and(filter.os.eq(os));
if (tag != null) {
builder.and(filter.tag.eq(tag));
}
if (photographerId != null) {
builder.and(filter.photographer.id.eq(photographerId));
}
return builder;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,28 @@ public FilterListDto getFilters(Long id, int page, int size, OS os, Tag tag, Str
isLike(((Filter) filter[0]).getId(), id),
filterLikeRepository.getLikes((Filter) filter[0]),
checkAccess(user.getMembership(), ((Filter)filter[0]).getMembership()))).toList();
} else if (sortedBy.equals("name")) {
Page<Filter> filterByName = filterRepository.findAllWithNameSorting(os, tag, photographerId, pageRequest);
isLast = filterByName.isLast();
totalPage = filterByName.getTotalPages();
totalElement = filterByName.getTotalElements();
filterDtos = filterByName.getContent().stream().map(filter ->
FilterDto.of(
filter,
isLike((filter).getId(), id),
filterLikeRepository.getLikes(filter),
checkAccess(user.getMembership(), (filter).getMembership()))).toList();
} else if (sortedBy.equals("membership")) {
Page<Filter> filterByName = filterRepository.findAllWithMembershipSorting(os, tag, photographerId, pageRequest);
isLast = filterByName.isLast();
totalPage = filterByName.getTotalPages();
totalElement = filterByName.getTotalElements();
filterDtos = filterByName.getContent().stream().map(filter ->
FilterDto.of(
filter,
isLike((filter).getId(), id),
filterLikeRepository.getLikes(filter),
checkAccess(user.getMembership(), (filter).getMembership()))).toList();
} else {
pageRequest = PageRequest.of(page, size, Sort.by("createdAt").descending()); // 정렬 없을 때는 최신 순
Page<Filter> filterByLatest = filterRepository.findAllByOs(os, tag, photographerId, pageRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public SuccessResponse<FilterListDto> getFiltersByPhotographer(
examples =
{@ExampleObject(name = "최신순", summary = "최신순 정렬", value = "latest"),
@ExampleObject(name = "퓨어지수 높은순", summary = "퓨어지수 높은순 정렬", value = "pure"),
@ExampleObject(name = "조회순", summary = "조회순 정렬", value = "views")}) String sortedBy,
@ExampleObject(name = "조회순", summary = "조회순 정렬", value = "views"),
@ExampleObject(name = "이름순", summary = "이름순 정렬", value = "name"),
@ExampleObject(name = "등급 낮은순", summary = "등급 낮은순 정렬", value = "membership")}) String sortedBy,
@RequestParam(value = "os", required = true) @Parameter(description = "휴대폰 os",
examples = {@ExampleObject(value = "AOS"), @ExampleObject(value = "iOS")}) OS os,
@RequestParam(value = "page") int page,
Expand Down

0 comments on commit 193e126

Please sign in to comment.