Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(#105): 정렬 기준 수정 #106

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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