Skip to content

Commit

Permalink
feat: polish initial data setUp
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Sep 29, 2023
1 parent 54cfc25 commit 24c4a5d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {}
Original file line number Diff line number Diff line change
@@ -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<PostCommentResponse> comments,
List<String> tags) {}

0 comments on commit 24c4a5d

Please sign in to comment.