Skip to content

Commit

Permalink
feat: fixes pagination issue
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Sep 30, 2023
1 parent 1f19ee1 commit c1c1dc3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -128,7 +132,7 @@ private Mono<UUID> fetchOrInsertTag(String tagName) {
.map(Record1::value1);
}

public Mono<PaginatedResult<PostSummary>> findByKeyword(String keyword, Pageable pageable) {
public Mono<PaginatedResult<PostResponse>> findByKeyword(String keyword, Pageable pageable) {
log.debug(
"findByKeyword with keyword :{} with offset :{} and limit :{}",
keyword,
Expand All @@ -144,7 +148,21 @@ public Mono<PaginatedResult<PostSummary>> 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)
Expand All @@ -160,21 +178,18 @@ public Mono<PaginatedResult<PostSummary>> 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()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,33 @@ void willLoadPosts() {
.value((List<String> 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 =
Expand Down Expand Up @@ -60,9 +88,8 @@ void willCreatePosts() {
.exists("Location")
.expectBody(UUID.class);

Mono<Long> newCount = tagRepository.count();

Mono<Long> resultMono = newCount.zipWith(newCount, (value1, value2) -> value2 - value1);
Mono<Long> resultMono =
tagRepository.count().zipWith(count, (value1, value2) -> value2 - value1);

// Use StepVerifier to assert the behavior
StepVerifier.create(resultMono)
Expand Down

0 comments on commit c1c1dc3

Please sign in to comment.