Skip to content

Commit

Permalink
Api-v1.0.6
Browse files Browse the repository at this point in the history
Api-v1.0.6
  • Loading branch information
gengminy authored Jul 24, 2023
1 parent 49cd3d4 commit eb15a4e
Showing 1 changed file with 39 additions and 10 deletions.
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())
.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));
}
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

0 comments on commit eb15a4e

Please sign in to comment.