Skip to content

Commit

Permalink
chore: clean codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hantsy committed Dec 5, 2022
1 parent d440902 commit 26c744a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 49 deletions.
11 changes: 5 additions & 6 deletions auditing/src/test/java/com/example/demo/IntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.test.web.reactive.server.WebTestClient;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Expand All @@ -23,11 +23,10 @@ public void setup() {

@Test
public void willLoadPosts() {
this.webClient.get().uri("/posts")
.exchange()
.expectStatus().is2xxSuccessful()
.expectBodyList(Post.class).hasSize(2);
this.webClient.get().uri("/posts")
.exchange()
.expectStatus().is2xxSuccessful()
.expectBodyList(Post.class).hasSize(2);
}

}

5 changes: 2 additions & 3 deletions boot/src/main/java/com/example/demo/DemoApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import java.io.IOException;
import java.net.URI;
import java.time.Duration;
Expand Down Expand Up @@ -542,4 +542,3 @@ class Comment {
private Long version;

}

11 changes: 5 additions & 6 deletions boot/src/test/java/com/example/demo/IntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.test.web.reactive.server.WebTestClient;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Expand All @@ -23,11 +23,10 @@ public void setup() {

@Test
public void willLoadPosts() {
this.webClient.get().uri("/posts")
.exchange()
.expectStatus().is2xxSuccessful()
.expectBodyList(Post.class).hasSize(2);
this.webClient.get().uri("/posts")
.exchange()
.expectStatus().is2xxSuccessful()
.expectBodyList(Post.class).hasSize(2);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public Mono<Long> countByTitleContains(String name) {
return this.template.count(Query.query(where("title").like("%" + name + "%")), Post.class);
}


public Flux<Post> findAll() {
return this.template.select(Post.class).all();
}
Expand All @@ -49,26 +48,25 @@ public Mono<UUID> save(Post p) {
.map(post -> post.getId());
}

public Mono<Integer> update(Post p) {
/*
return this.template.update(Post.class)
.matching(Query.query(where("id").is(p.getId())))
.apply(Update.update("title", p.getTitle())
.set("content", p.getContent())
.set("status", p.getStatus())
.set("metadata", p.getMetadata()));
*/
public Mono<Long> update(Post p) {
/*
* return this.template.update(Post.class)
* .matching(Query.query(where("id").is(p.getId())))
* .apply(Update.update("title", p.getTitle())
* .set("content", p.getContent())
* .set("status", p.getStatus())
* .set("metadata", p.getMetadata()));
*/
return this.template.update(
Query.query(where("id").is(p.getId())),
Update.update("title", p.getTitle())
.set("content", p.getContent())
.set("status", p.getStatus())
.set("metadata", p.getMetadata()),
Post.class
);
Post.class);
}

public Mono<Integer> deleteById(UUID id) {
public Mono<Long> deleteById(UUID id) {
return this.template.delete(Query.query(where("id").is(id)), Post.class);
}
}
}
26 changes: 16 additions & 10 deletions database-client/src/main/java/com/example/demo/PostRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public Flux<Post> findAll() {
.all();
}

// see: https://stackoverflow.com/questions/64267699/spring-data-r2dbc-and-group-by
// see:
// https://stackoverflow.com/questions/64267699/spring-data-r2dbc-and-group-by
public Flux<Map<Object, Object>> countByStatus() {
return this.databaseClient
.sql("SELECT count(*) as cnt, status FROM posts group by status")
Expand All @@ -71,7 +72,8 @@ public Mono<Post> findById(UUID id) {
}

public Mono<UUID> save(Post p) {
return this.databaseClient.sql("INSERT INTO posts (title, content, metadata, status) VALUES (:title, :content, :metadata, :status)")
return this.databaseClient.sql(
"INSERT INTO posts (title, content, metadata, status) VALUES (:title, :content, :metadata, :status)")
.filter((statement, executeFunction) -> statement.returnGeneratedValues("id").execute())
.bind("title", p.getTitle())
.bind("content", p.getContent())
Expand All @@ -83,12 +85,14 @@ public Mono<UUID> save(Post p) {
}

// see: https://github.com/spring-projects/spring-data-r2dbc/issues/259
// and https://stackoverflow.com/questions/62514094/how-to-execute-multiple-inserts-in-batch-in-r2dbc
// and
// https://stackoverflow.com/questions/62514094/how-to-execute-multiple-inserts-in-batch-in-r2dbc
public Flux<UUID> saveAll(List<Post> data) {
Assert.notEmpty(data, "saving data can be empty");
return this.databaseClient.inConnectionMany(connection -> {

var statement = connection.createStatement("INSERT INTO posts (title, content, status) VALUES ($1, $2, $3)")
var statement = connection
.createStatement("INSERT INTO posts (title, content, status) VALUES ($1, $2, $3)")
.returnGeneratedValues("id");

for (int i = 0; i < data.size() - 1; i++) {
Expand All @@ -105,12 +109,14 @@ public Flux<UUID> saveAll(List<Post> data) {
.bind(1, lastItem.getContent())
.bind(2, lastItem.getStatus());

return Flux.from(statement.execute()).flatMap(result -> result.map((row, rowMetadata) -> row.get("id", UUID.class)));
return Flux.from(statement.execute())
.flatMap(result -> result.map((row, rowMetadata) -> row.get("id", UUID.class)));
});
}

public Mono<Integer> update(Post p) {
return this.databaseClient.sql("UPDATE posts set title=:title, content=:content, metadata=:metadata, status=:status WHERE id=:id")
public Mono<Long> update(Post p) {
return this.databaseClient
.sql("UPDATE posts set title=:title, content=:content, metadata=:metadata, status=:status WHERE id=:id")
.bind("title", p.getTitle())
.bind("content", p.getContent())
.bind("metadata", p.getMetadata())
Expand All @@ -120,16 +126,16 @@ public Mono<Integer> update(Post p) {
.rowsUpdated();
}

public Mono<Integer> deleteById(UUID id) {
public Mono<Long> deleteById(UUID id) {
return this.databaseClient.sql("DELETE FROM posts WHERE id=:id")
.bind("id", id)
.fetch()
.rowsUpdated();
}

public Mono<Integer> deleteAll() {
public Mono<Long> deleteAll() {
return this.databaseClient.sql("DELETE FROM posts")
.fetch()
.rowsUpdated();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.test.web.reactive.server.WebTestClient;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Expand All @@ -23,11 +23,10 @@ public void setup() {

@Test
public void willLoadPosts() {
this.webClient.get().uri("/posts")
.exchange()
.expectStatus().is2xxSuccessful()
.expectBodyList(Post.class).hasSize(2);
this.webClient.get().uri("/posts")
.exchange()
.expectStatus().is2xxSuccessful()
.expectBodyList(Post.class).hasSize(2);
}

}

7 changes: 3 additions & 4 deletions jooq/src/test/java/com/example/demo/IntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.test.web.reactive.server.WebTestClient;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;


@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class IntegrationTests {

Expand All @@ -31,8 +30,8 @@ public void willLoadPosts() {
this.webClient.get().uri("/posts")
.exchange()
.expectStatus().is2xxSuccessful()
.expectBody().jsonPath("$[*].title").value((List<String> titles) -> assertThat(titles).containsAnyOf("jooq test"));
.expectBody().jsonPath("$[*].title")
.value((List<String> titles) -> assertThat(titles).containsAnyOf("jooq test"));
}

}

0 comments on commit 26c744a

Please sign in to comment.