From b69fdc5ad07eb7548e6f6cb831f0a526cd5a9a40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=85=B8=EA=B2=BD=EB=AF=BC?= Date: Mon, 24 Jul 2023 23:57:01 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20=EB=A9=94=EC=9D=B8=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20=EA=B3=B5=EC=97=B0=20=EC=A0=95=EB=A0=AC=20?= =?UTF-8?q?=EC=88=9C=EC=84=9C=20=EB=B3=80=EA=B2=BD=20(#567)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor : 메인페이지 쿼리 순서 변경 * chore : spotless apply --- .../repository/EventCustomRepositoryImpl.java | 49 +++++++++++++++---- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/event/repository/EventCustomRepositoryImpl.java b/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/event/repository/EventCustomRepositoryImpl.java index 950307f2..96cd4b01 100644 --- a/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/event/repository/EventCustomRepositoryImpl.java +++ b/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/event/repository/EventCustomRepositoryImpl.java @@ -51,15 +51,20 @@ public Slice querySliceEventsByStatus(EventStatus status, Pageable pageab @Override public Slice querySliceEventsByKeyword(String keyword, Pageable pageable) { - List events = + List 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 @@ -67,6 +72,26 @@ public List queryEventsByEndAtBeforeAndStatusOpen(LocalDateTime time) { return queryFactory.selectFrom(event).where(endAtBefore(time), statusEq(OPEN)).fetch(); } + private List 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 hostId) { return event.hostId.in(hostId); } @@ -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); } @@ -95,10 +116,18 @@ private OrderSpecifier createdAtDesc() { return event.createdAt.desc(); } + private OrderSpecifier createdAtAsc() { + return event.createdAt.asc(); + } + private OrderSpecifier startAtAsc() { return event.eventBasic.startAt.asc(); } + private OrderSpecifier startAtDesc() { + return event.eventBasic.startAt.desc(); + } + private OrderSpecifier statusDesc() { return event.status.desc(); }