From ffde5a5ad01c491ac4f6f90b49b19080b707d666 Mon Sep 17 00:00:00 2001 From: gengminy Date: Sun, 23 Jul 2023 01:47:32 +0900 Subject: [PATCH 1/2] =?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=EC=BF=BC=EB=A6=AC=20=EC=88=9C=EC=84=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/EventCustomRepositoryImpl.java | 62 ++++++++++++++----- 1 file changed, 46 insertions(+), 16 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..32ca04ac 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 @@ -1,9 +1,5 @@ package band.gosrock.domain.domains.event.repository; -import static band.gosrock.domain.domains.event.domain.EventStatus.CLOSED; -import static band.gosrock.domain.domains.event.domain.EventStatus.OPEN; -import static band.gosrock.domain.domains.event.domain.QEvent.event; - import band.gosrock.domain.common.util.SliceUtil; import band.gosrock.domain.domains.event.domain.Event; import band.gosrock.domain.domains.event.domain.EventStatus; @@ -12,12 +8,17 @@ import com.querydsl.core.types.dsl.DateTemplate; import com.querydsl.core.types.dsl.Expressions; import com.querydsl.jpa.impl.JPAQueryFactory; -import java.time.LocalDateTime; -import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; +import java.time.LocalDateTime; +import java.util.List; + +import static band.gosrock.domain.domains.event.domain.EventStatus.CLOSED; +import static band.gosrock.domain.domains.event.domain.EventStatus.OPEN; +import static band.gosrock.domain.domains.event.domain.QEvent.event; + @RequiredArgsConstructor public class EventCustomRepositoryImpl implements EventCustomRepository { @@ -51,15 +52,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 +73,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 +101,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 +117,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(); } From 28fdd34d8920f76397c9c31cf6d1c885ebbed968 Mon Sep 17 00:00:00 2001 From: gengminy Date: Mon, 24 Jul 2023 23:47:25 +0900 Subject: [PATCH 2/2] chore : spotless apply --- .../event/repository/EventCustomRepositoryImpl.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 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 32ca04ac..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 @@ -1,5 +1,9 @@ package band.gosrock.domain.domains.event.repository; +import static band.gosrock.domain.domains.event.domain.EventStatus.CLOSED; +import static band.gosrock.domain.domains.event.domain.EventStatus.OPEN; +import static band.gosrock.domain.domains.event.domain.QEvent.event; + import band.gosrock.domain.common.util.SliceUtil; import band.gosrock.domain.domains.event.domain.Event; import band.gosrock.domain.domains.event.domain.EventStatus; @@ -8,17 +12,12 @@ import com.querydsl.core.types.dsl.DateTemplate; import com.querydsl.core.types.dsl.Expressions; import com.querydsl.jpa.impl.JPAQueryFactory; +import java.time.LocalDateTime; +import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; -import java.time.LocalDateTime; -import java.util.List; - -import static band.gosrock.domain.domains.event.domain.EventStatus.CLOSED; -import static band.gosrock.domain.domains.event.domain.EventStatus.OPEN; -import static band.gosrock.domain.domains.event.domain.QEvent.event; - @RequiredArgsConstructor public class EventCustomRepositoryImpl implements EventCustomRepository {