From c1c1dc36c9c65d575af1fdae9cbc479a8d65a615 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Sat, 30 Sep 2023 12:19:24 +0000 Subject: [PATCH] feat: fixes pagination issue --- .../jooq/r2dbc/service/PostService.java | 33 ++++++++++++++----- .../jooq/r2dbc/router/WebRouterConfigIT.java | 33 +++++++++++++++++-- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/r2dbc/boot-jooq-r2dbc-sample/src/main/java/com/example/jooq/r2dbc/service/PostService.java b/r2dbc/boot-jooq-r2dbc-sample/src/main/java/com/example/jooq/r2dbc/service/PostService.java index ea3a7c3f5..661242e0a 100644 --- a/r2dbc/boot-jooq-r2dbc-sample/src/main/java/com/example/jooq/r2dbc/service/PostService.java +++ b/r2dbc/boot-jooq-r2dbc-sample/src/main/java/com/example/jooq/r2dbc/service/PostService.java @@ -11,8 +11,12 @@ import com.example.jooq.r2dbc.model.request.CreatePostCommand; import com.example.jooq.r2dbc.model.request.CreatePostComment; import com.example.jooq.r2dbc.model.response.PaginatedResult; +import com.example.jooq.r2dbc.model.response.PostCommentResponse; +import com.example.jooq.r2dbc.model.response.PostResponse; import com.example.jooq.r2dbc.model.response.PostSummary; import com.example.jooq.r2dbc.repository.PostRepository; +import com.example.jooq.r2dbc.testcontainersflyway.db.tables.PostComments; +import com.example.jooq.r2dbc.testcontainersflyway.db.tables.Posts; import com.example.jooq.r2dbc.testcontainersflyway.db.tables.records.PostCommentsRecord; import com.example.jooq.r2dbc.testcontainersflyway.db.tables.records.PostsTagsRecord; import java.lang.reflect.Field; @@ -128,7 +132,7 @@ private Mono fetchOrInsertTag(String tagName) { .map(Record1::value1); } - public Mono> findByKeyword(String keyword, Pageable pageable) { + public Mono> findByKeyword(String keyword, Pageable pageable) { log.debug( "findByKeyword with keyword :{} with offset :{} and limit :{}", keyword, @@ -144,7 +148,21 @@ public Mono> findByKeyword(String keyword, Pageable .select( POSTS.ID, POSTS.TITLE, - DSL.field("count(post_comments.id)", SQLDataType.BIGINT), + POSTS.CONTENT, + multiset( + select( + PostComments.POST_COMMENTS.ID, + PostComments.POST_COMMENTS.CONTENT, + PostComments.POST_COMMENTS + .CREATED_AT) + .from(PostComments.POST_COMMENTS) + .where( + PostComments.POST_COMMENTS.POST_ID + .eq(Posts.POSTS.ID))) + .as("comments") + .convertFrom( + record3s -> + record3s.into(PostCommentResponse.class)), multiset( select(TAGS.NAME) .from(TAGS) @@ -160,21 +178,18 @@ public Mono> findByKeyword(String keyword, Pageable .limit(pageable.getPageSize()) .offset(pageable.getOffset()); - var countSql = - dslContext - .select(DSL.field("count(1)", SQLDataType.BIGINT)) - .from(POSTS) - .where(where); + var countSql = dslContext.selectCount().from(POSTS).where(where); return Mono.zip( Flux.from(dataSql) .map( r -> - new PostSummary( + new PostResponse( r.value1(), r.value2(), r.value3(), - r.value4())) + r.value4(), + r.value5())) .collectList(), Mono.from(countSql).map(Record1::value1)) .map(it -> new PageImpl<>(it.getT1(), pageable, it.getT2())) diff --git a/r2dbc/boot-jooq-r2dbc-sample/src/test/java/com/example/jooq/r2dbc/router/WebRouterConfigIT.java b/r2dbc/boot-jooq-r2dbc-sample/src/test/java/com/example/jooq/r2dbc/router/WebRouterConfigIT.java index cb80878e2..a99e89261 100644 --- a/r2dbc/boot-jooq-r2dbc-sample/src/test/java/com/example/jooq/r2dbc/router/WebRouterConfigIT.java +++ b/r2dbc/boot-jooq-r2dbc-sample/src/test/java/com/example/jooq/r2dbc/router/WebRouterConfigIT.java @@ -4,6 +4,7 @@ import com.example.jooq.r2dbc.common.AbstractIntegrationTest; import com.example.jooq.r2dbc.model.request.CreatePostCommand; +import com.example.jooq.r2dbc.model.response.PaginatedResult; import com.example.jooq.r2dbc.repository.TagRepository; import java.util.List; import java.util.UUID; @@ -30,6 +31,33 @@ void willLoadPosts() { .value((List titles) -> assertThat(titles).containsAnyOf("jooq test")); } + @Test + void searchPosts() { + this.webTestClient + .get() + .uri( + uriBuilder -> { + uriBuilder.path("/posts/search"); + uriBuilder.queryParam("keyword", "Jooq"); + return uriBuilder.build(); + }) + .exchange() + .expectStatus() + .is2xxSuccessful() + .expectBody(PaginatedResult.class) + .value( + paginatedResult -> { + assertThat(paginatedResult.data()).isNotEmpty().hasSize(1); + assertThat(paginatedResult.totalElements()).isEqualTo(1); + assertThat(paginatedResult.pageNumber()).isEqualTo(1); + assertThat(paginatedResult.totalPages()).isEqualTo(1); + assertThat(paginatedResult.isFirst()).isTrue(); + assertThat(paginatedResult.isLast()).isTrue(); + assertThat(paginatedResult.hasNext()).isFalse(); + assertThat(paginatedResult.hasPrevious()).isFalse(); + }); + } + @Test void willCreatePosts() { CreatePostCommand createPost = @@ -60,9 +88,8 @@ void willCreatePosts() { .exists("Location") .expectBody(UUID.class); - Mono newCount = tagRepository.count(); - - Mono resultMono = newCount.zipWith(newCount, (value1, value2) -> value2 - value1); + Mono resultMono = + tagRepository.count().zipWith(count, (value1, value2) -> value2 - value1); // Use StepVerifier to assert the behavior StepVerifier.create(resultMono)