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

refactor : 메인페이지 공연 정렬 순서 변경 #567

Merged
merged 2 commits into from
Jul 24, 2023
Merged
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 @@ -51,22 +51,47 @@ public Slice<Event> querySliceEventsByStatus(EventStatus status, Pageable pageab

@Override
public Slice<Event> querySliceEventsByKeyword(String keyword, Pageable pageable) {
List<Event> events =
List<Event> openEvents =
queryFactory
.selectFrom(event)
.where(eqStatusCanExposed(), nameContains(keyword))
.orderBy(statusDesc(), startAtAsc())
.where(eqStatusOpen().and(nameContains(keyword)))
.orderBy(createdAtAsc())
Comment on lines +57 to +58
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and로 묶어주는것도 좋네용

.offset(pageable.getOffset())
.limit(pageable.getPageSize() + 1)
.fetch();
return SliceUtil.valueOf(events, pageable);

final long remainingSize = Math.max(pageable.getPageSize() - openEvents.size(), 0);
if (remainingSize > 0) {
openEvents.addAll(queryClosedEventsByKeywordAndSize(keyword, pageable, remainingSize));
}
Comment on lines +63 to +66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 이 로직은 무슨역할이죵

Copy link
Member Author

@gengminy gengminy Jul 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정렬 순서가 앞 뒤로 달라서 어쩔 수 없이 쿼리를 두번 쏴서 페이징하는데
OPEN 상태인 공연 개수 결과를 세서 pageSize 보다 작으면
pageSize - OPEN 공연수 계산해서 나머지 개수만큼 limit 해서 CLOSED 공연을 가져오는 로직입니다

근데 이미 OPEN 공연을 pageSize 만큼 전부 가져왔을 수도 있으니
그 때는 CLOSED 공연 가져오는 쿼리 안날라가도록 조건문으로 최적화 했습니다~

return SliceUtil.valueOf(openEvents, pageable);
}

@Override
public List<Event> queryEventsByEndAtBeforeAndStatusOpen(LocalDateTime time) {
return queryFactory.selectFrom(event).where(endAtBefore(time), statusEq(OPEN)).fetch();
}

private List<Event> queryClosedEventsByKeywordAndSize(
String keyword, Pageable pageable, Long size) {
final long totalOpenEventsSize = queryCountByKeywordAndStatus(keyword, OPEN);
final long closedEventsOffset = Math.max(pageable.getOffset() - totalOpenEventsSize, 0);
return queryFactory
.selectFrom(event)
.where(eqStatusClosed().and(nameContains(keyword)))
.orderBy(startAtDesc())
.offset(closedEventsOffset)
.limit(size + 1)
.fetch();
}

private long queryCountByKeywordAndStatus(String keyword, EventStatus status) {
return queryFactory
.from(event)
.where(statusEq(status).and(nameContains(keyword)))
.fetchCount();
}

private BooleanExpression hostIdIn(List<Long> hostId) {
return event.hostId.in(hostId);
}
Expand All @@ -75,18 +100,14 @@ private BooleanExpression eqStatusOpen() {
return event.status.eq(OPEN);
}

private BooleanExpression eqStatusCanExposed() {
return event.status.eq(OPEN).or(event.status.eq(CLOSED));
private BooleanExpression eqStatusClosed() {
return event.status.eq(CLOSED);
}

private BooleanExpression statusEq(EventStatus status) {
return event.status.eq(status);
}

private BooleanExpression statusNotEq(EventStatus status) {
return event.status.eq(status).not();
}

private BooleanExpression nameContains(String keyword) {
return keyword == null ? null : event.eventBasic.name.containsIgnoreCase(keyword);
}
Expand All @@ -95,10 +116,18 @@ private OrderSpecifier<LocalDateTime> createdAtDesc() {
return event.createdAt.desc();
}

private OrderSpecifier<LocalDateTime> createdAtAsc() {
return event.createdAt.asc();
}

private OrderSpecifier<LocalDateTime> startAtAsc() {
return event.eventBasic.startAt.asc();
}

private OrderSpecifier<LocalDateTime> startAtDesc() {
return event.eventBasic.startAt.desc();
}

private OrderSpecifier<EventStatus> statusDesc() {
return event.status.desc();
}
Expand Down
Loading