From d5158ce31155ec83a56be35dfef819d9e02d81e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98jminkkk=E2=80=99?= <102847513+jminkkk@users.noreply.github.com> Date: Sat, 9 Nov 2024 00:21:32 +0900 Subject: [PATCH 1/8] =?UTF-8?q?refactor(pagination):=20=EA=B3=A0=EC=A0=95?= =?UTF-8?q?=20=ED=8E=98=EC=9D=B4=EC=A7=80=EB=84=A4=EC=9D=B4=EC=85=98?= =?UTF-8?q?=EC=97=90=20=EB=8C=80=ED=95=9C=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EA=B0=9D=EC=B2=B4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/codezap/global/pagination/FixedPage.java | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 backend/src/main/java/codezap/global/pagination/FixedPage.java diff --git a/backend/src/main/java/codezap/global/pagination/FixedPage.java b/backend/src/main/java/codezap/global/pagination/FixedPage.java new file mode 100644 index 00000000..8ab2a87c --- /dev/null +++ b/backend/src/main/java/codezap/global/pagination/FixedPage.java @@ -0,0 +1,6 @@ +package codezap.global.pagination; + +import java.util.List; + +public record FixedPage (List contents, int nextPages) { +} From 8b23deb370a836888dd1dfdb64feea36f0141024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98jminkkk=E2=80=99?= <102847513+jminkkk@users.noreply.github.com> Date: Sat, 9 Nov 2024 00:24:13 +0900 Subject: [PATCH 2/8] =?UTF-8?q?refactor(response):=20=EA=B3=A0=EC=A0=95=20?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=A7=80=EB=84=A4=EC=9D=B4=EC=85=98=EC=97=90?= =?UTF-8?q?=20=EB=8C=80=ED=95=9C=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=20=EC=9D=91=EB=8B=B5=20=ED=98=95=EC=8B=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../template/dto/response/FindAllTemplatesResponse.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/backend/src/main/java/codezap/template/dto/response/FindAllTemplatesResponse.java b/backend/src/main/java/codezap/template/dto/response/FindAllTemplatesResponse.java index 87c8be08..9b76eb02 100644 --- a/backend/src/main/java/codezap/template/dto/response/FindAllTemplatesResponse.java +++ b/backend/src/main/java/codezap/template/dto/response/FindAllTemplatesResponse.java @@ -5,10 +5,8 @@ import io.swagger.v3.oas.annotations.media.Schema; public record FindAllTemplatesResponse( - @Schema(description = "전체 페이지 개수", example = "1") - int totalPages, - @Schema(description = "총 템플릿 개수", example = "134") - long totalElements, + @Schema(description = "다음 페이지 개수, 최대 5개입니다.", example = "1") + int nextPages, @Schema(description = "템플릿 목록") List templates ) { From 678d4a0c9bb013fa9cd9b66b57c0bae1094b3c41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98jminkkk=E2=80=99?= <102847513+jminkkk@users.noreply.github.com> Date: Sat, 9 Nov 2024 00:25:24 +0900 Subject: [PATCH 3/8] =?UTF-8?q?refactor(pagination):=20=EA=B3=A0=EC=A0=95?= =?UTF-8?q?=20=ED=8E=98=EC=9D=B4=EC=A7=80=EB=84=A4=EC=9D=B4=EC=85=98?= =?UTF-8?q?=EC=97=90=20=EC=82=AC=EC=9A=A9=EB=90=98=EB=8A=94=20=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20=EC=B9=B4=EC=9A=B4=ED=8C=85=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/pagination/FixedPageCounter.java | 34 ++++++++++++ .../pagination/FixedPageCounterTest.java | 55 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 backend/src/main/java/codezap/global/pagination/FixedPageCounter.java create mode 100644 backend/src/test/java/codezap/global/pagination/FixedPageCounterTest.java diff --git a/backend/src/main/java/codezap/global/pagination/FixedPageCounter.java b/backend/src/main/java/codezap/global/pagination/FixedPageCounter.java new file mode 100644 index 00000000..f83f0780 --- /dev/null +++ b/backend/src/main/java/codezap/global/pagination/FixedPageCounter.java @@ -0,0 +1,34 @@ +package codezap.global.pagination; + +import java.util.Objects; + +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Component; + +import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.core.types.dsl.EntityPathBase; +import com.querydsl.jpa.impl.JPAQueryFactory; + +@Component +public class FixedPageCounter { + + private static final int MAXIMUM_PAGE = 5; + + public int countNextFixedPage( + JPAQueryFactory queryFactory, + EntityPathBase entityPath, + Pageable pageable, + BooleanExpression... conditions + ) { + int maximumElementsCount = pageable.getPageSize() * MAXIMUM_PAGE; + long nextFixedElementCounts = Objects.requireNonNull(queryFactory + .selectFrom(entityPath) + .where(conditions) + .offset(pageable.getOffset()) + .limit(maximumElementsCount) + .fetch()) + .size(); + + return (int) Math.ceil((double) nextFixedElementCounts / pageable.getPageSize()); + } +} diff --git a/backend/src/test/java/codezap/global/pagination/FixedPageCounterTest.java b/backend/src/test/java/codezap/global/pagination/FixedPageCounterTest.java new file mode 100644 index 00000000..fb09d80e --- /dev/null +++ b/backend/src/test/java/codezap/global/pagination/FixedPageCounterTest.java @@ -0,0 +1,55 @@ +package codezap.global.pagination; + +import static org.assertj.core.api.Assertions.assertThat; + +import jakarta.persistence.EntityManager; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.context.annotation.Import; +import org.springframework.data.domain.PageRequest; +import org.springframework.test.context.jdbc.Sql; + +import com.querydsl.jpa.impl.JPAQueryFactory; + +import codezap.global.querydsl.QueryDSLConfig; +import codezap.global.rds.DataSourceConfig; +import codezap.template.domain.QTemplate; + +@DataJpaTest +@Import({DataSourceConfig.class, QueryDSLConfig.class, FixedPageCounter.class}) +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +@Sql(scripts = "classpath:search.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) +class FixedPageCounterTest { + + @Autowired + private EntityManager em; + + private JPAQueryFactory queryFactory; + private FixedPageCounter fixedPageCounter; + + @BeforeEach + void setUp() { + queryFactory = new JPAQueryFactory(em); + fixedPageCounter = new FixedPageCounter(); + } + + @Test + @DisplayName("페이지가 10개까지 있어도, 최대 5페이지까지만 카운팅") + void countNextFixedPage_WithPageSize10() { + PageRequest pageRequest = PageRequest.of(0, 1); + QTemplate template = QTemplate.template; + + int nextFixedPage = fixedPageCounter.countNextFixedPage( + queryFactory, + template, + pageRequest + ); + + assertThat(nextFixedPage).isEqualTo(5); + } +} From 5f4c09b292d22cc07cc1c4ff1270d6f2dad705b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98jminkkk=E2=80=99?= <102847513+jminkkk@users.noreply.github.com> Date: Sat, 9 Nov 2024 00:29:06 +0900 Subject: [PATCH 4/8] =?UTF-8?q?refactor(pagination):=20=ED=85=9C=ED=94=8C?= =?UTF-8?q?=EB=A6=BF=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20=EC=8B=9C?= =?UTF-8?q?=20=EC=A0=84=EC=B2=B4=20element=20=EC=9A=94=EC=86=8C=20?= =?UTF-8?q?=EC=B9=B4=EC=9A=B4=ED=8A=B8=20=EC=A0=9C=EA=B1=B0=20=EB=B0=8F=20?= =?UTF-8?q?FixedPage=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TemplateQueryDSLRepository.java | 27 ++++--- .../repository/TemplateRepository.java | 2 +- .../template/service/TemplateService.java | 4 +- .../facade/TemplateApplicationService.java | 17 ++-- .../controller/TemplateControllerTest.java | 15 ++-- .../TemplateSearchRepositoryTest.java | 79 ++++++++++--------- .../service/TemplateSearchServiceTest.java | 65 ++++++++------- .../template/service/TemplateServiceTest.java | 12 +-- .../TemplateApplicationServiceTest.java | 4 +- backend/src/test/resources/search.sql | 15 +++- 10 files changed, 126 insertions(+), 114 deletions(-) diff --git a/backend/src/main/java/codezap/template/repository/TemplateQueryDSLRepository.java b/backend/src/main/java/codezap/template/repository/TemplateQueryDSLRepository.java index e073b11c..c25c38bf 100644 --- a/backend/src/main/java/codezap/template/repository/TemplateQueryDSLRepository.java +++ b/backend/src/main/java/codezap/template/repository/TemplateQueryDSLRepository.java @@ -23,8 +23,9 @@ public class TemplateQueryDSLRepository { private final JPAQueryFactory queryFactory; private final TemplateSearchExpressionProvider expressionProvider; + private final FixedPageCounter fixedPageCounter; - public Page