diff --git a/r2dbc/boot-jooq-r2dbc-sample/src/main/java/com/example/jooq/r2dbc/config/Initializer.java b/r2dbc/boot-jooq-r2dbc-sample/src/main/java/com/example/jooq/r2dbc/config/Initializer.java index d5432c909..e993c6a98 100644 --- a/r2dbc/boot-jooq-r2dbc-sample/src/main/java/com/example/jooq/r2dbc/config/Initializer.java +++ b/r2dbc/boot-jooq-r2dbc-sample/src/main/java/com/example/jooq/r2dbc/config/Initializer.java @@ -1,14 +1,19 @@ package com.example.jooq.r2dbc.config; +import static com.example.jooq.r2dbc.testcontainersflyway.db.Tables.POSTS_TAGS; import static com.example.jooq.r2dbc.testcontainersflyway.db.tables.PostComments.POST_COMMENTS; import static com.example.jooq.r2dbc.testcontainersflyway.db.tables.Posts.POSTS; +import static com.example.jooq.r2dbc.testcontainersflyway.db.tables.Tags.TAGS; import static org.jooq.impl.DSL.multiset; import static org.jooq.impl.DSL.select; import com.example.jooq.r2dbc.config.logging.Loggable; +import com.example.jooq.r2dbc.model.response.PostCommentResponse; +import com.example.jooq.r2dbc.model.response.PostResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.jooq.DSLContext; +import org.jooq.Record1; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; import reactor.core.publisher.Mono; @@ -30,32 +35,90 @@ public void run(String... args) { .columns(POSTS.TITLE, POSTS.CONTENT) .values("jooq test", "content of Jooq test") .returningResult(POSTS.ID)) - .flatMapMany( - id -> - dslContext - .insertInto(POST_COMMENTS) - .columns(POST_COMMENTS.POST_ID, POST_COMMENTS.CONTENT) - .values(id.component1(), "test comments") - .values(id.component1(), "test comments 2") - .returningResult(POST_COMMENTS.ID)) .flatMap( + postId -> + Mono.from( + dslContext + .insertInto(TAGS) + .columns(TAGS.NAME) + .values("java") + .returningResult(TAGS.ID)) + .flatMap( + tagId -> + Mono.from( + dslContext + .insertInto(POSTS_TAGS) + .columns( + POSTS_TAGS.TAG_ID, + POSTS_TAGS.POST_ID) + .values( + tagId.component1(), + postId.component1()) + .returningResult( + POSTS_TAGS + .POST_ID))) + .flatMapMany( + pid -> + dslContext + .insertInto(POST_COMMENTS) + .columns( + POST_COMMENTS.POST_ID, + POST_COMMENTS.CONTENT) + .values( + pid.component1(), + "test comments") + .values( + pid.component1(), + "test comments 2") + .returningResult(POST_COMMENTS.ID)) + .collectList() // Collect all comment IDs into a list + ) + .flatMapMany( it -> dslContext .select( + POSTS.ID, POSTS.TITLE, POSTS.CONTENT, multiset( - select(POST_COMMENTS.CONTENT) + select( + POST_COMMENTS.ID, + POST_COMMENTS + .CONTENT, + POST_COMMENTS + .CREATED_AT) .from(POST_COMMENTS) .where( POST_COMMENTS .POST_ID.eq( POSTS.ID))) - .as("comments")) + .as("comments") + .convertFrom( + record3s -> + record3s.into( + PostCommentResponse + .class)), + multiset( + select(TAGS.NAME) + .from(TAGS) + .join(POSTS_TAGS) + .on( + TAGS.ID.eq( + POSTS_TAGS + .TAG_ID)) + .where( + POSTS_TAGS.POST_ID + .eq( + POSTS.ID))) + .as("tags") + .convertFrom( + record -> + record.map( + Record1::value1))) .from(POSTS) .orderBy(POSTS.CREATED_AT)) .subscribe( - data -> log.debug("Retrieved data: {}", data.formatJSON()), + data -> log.debug("Retrieved data: {}", data.into(PostResponse.class)), error -> log.debug("error: " + error), () -> log.debug("done")); } diff --git a/r2dbc/boot-jooq-r2dbc-sample/src/main/java/com/example/jooq/r2dbc/model/response/PostCommentResponse.java b/r2dbc/boot-jooq-r2dbc-sample/src/main/java/com/example/jooq/r2dbc/model/response/PostCommentResponse.java new file mode 100644 index 000000000..a1cfb64a4 --- /dev/null +++ b/r2dbc/boot-jooq-r2dbc-sample/src/main/java/com/example/jooq/r2dbc/model/response/PostCommentResponse.java @@ -0,0 +1,6 @@ +package com.example.jooq.r2dbc.model.response; + +import java.time.LocalDateTime; +import java.util.UUID; + +public record PostCommentResponse(UUID id, String content, LocalDateTime createdAt) {} diff --git a/r2dbc/boot-jooq-r2dbc-sample/src/main/java/com/example/jooq/r2dbc/model/response/PostResponse.java b/r2dbc/boot-jooq-r2dbc-sample/src/main/java/com/example/jooq/r2dbc/model/response/PostResponse.java new file mode 100644 index 000000000..970af25fd --- /dev/null +++ b/r2dbc/boot-jooq-r2dbc-sample/src/main/java/com/example/jooq/r2dbc/model/response/PostResponse.java @@ -0,0 +1,11 @@ +package com.example.jooq.r2dbc.model.response; + +import java.util.List; +import java.util.UUID; + +public record PostResponse( + UUID id, + String title, + String content, + List comments, + List tags) {}