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 4, 2024
1 parent 74503e7 commit 26684c0
Show file tree
Hide file tree
Showing 21 changed files with 357 additions and 340 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/boot-data-couchbase.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ jobs:
# see: https://github.com/testcontainers/testcontainers-java/discussions/4676
run: |
cd boot-data-couchbase
mvn -q clean test -Dtest=PostRepositoryWithTestcontainresTest
mvn -q clean test -Dtest=PostRepositoryWithTestcontainersTest
docker rm $(docker ps -aq) -f
mvn -q clean test -Dtest=PostRepositoryTest
5 changes: 5 additions & 0 deletions boot-data-couchbase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
18 changes: 18 additions & 0 deletions boot-data-couchbase/src/main/java/com/example/demo/DataConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.repository.auditing.EnableReactiveCouchbaseAuditing;
import org.springframework.data.domain.ReactiveAuditorAware;
import reactor.core.publisher.Mono;

@Configuration(proxyBeanMethods = false)
// see: https://jira.spring.io/browse/DATACOUCH-644
@EnableReactiveCouchbaseAuditing
public class DataConfig {

@Bean
public ReactiveAuditorAware<String> reactiveAuditorAware() {
return () -> Mono.just("hantsy");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.demo;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

import java.util.UUID;

@Component
@RequiredArgsConstructor
@Slf4j
@Profile("dev")
public class DataInitializer implements CommandLineRunner {

private final PostRepository posts;

@Override
public void run(String[] args) {
log.info("start data initialization ...");
this.posts
.deleteAll()
.thenMany(
Flux
.just("Post one", "Post two")
.flatMap(
title -> this.posts.save(Post.builder().id(UUID.randomUUID().toString()).title(title).content("content of " + title).build())
)
)
.log("[initialization log]")
.subscribe(
data -> log.info("saved data: {}", data),
error -> log.error("error: {}", error.getMessage()),
() -> log.info("done initialization...")
);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,140 +38,3 @@ public static void main(String[] args) {

}

@Configuration
class WebConfig {
@Bean
public RouterFunction<ServerResponse> routes(PostHandler handler) {
return route(GET("/posts"), handler::all)
.andRoute(POST("/posts"), handler::create)
.andRoute(GET("/posts/{id}"), handler::get)
.andRoute(PUT("/posts/{id}"), handler::update)
.andRoute(DELETE("/posts/{id}"), handler::delete);
}
}

@Configuration(proxyBeanMethods = false)
// see: https://jira.spring.io/browse/DATACOUCH-644
@EnableReactiveCouchbaseAuditing
class DataConfig {

@Bean
public ReactiveAuditorAware<String> reactiveAuditorAware() {
return () -> Mono.just("hantsy");
}
}

@Component
@Slf4j
@Profile("default")
class DataInitializer implements CommandLineRunner {

private final PostRepository posts;

public DataInitializer(PostRepository posts) {
this.posts = posts;
}

@Override
public void run(String[] args) {
log.info("start data initialization ...");
this.posts
.deleteAll()
.thenMany(
Flux
.just("Post one", "Post two")
.flatMap(
title -> this.posts.save(Post.builder().id(UUID.randomUUID().toString()).title(title).content("content of " + title).build())
)
)
.log("[initialization log]")
.subscribe(
data -> log.info("saved data: {}", data),
error -> log.error("error: {}", error.getMessage()),
() -> log.info("done initialization...")
);

}

}

@Component
@RequiredArgsConstructor
class PostHandler {
private final PostRepository posts;

public Mono<ServerResponse> all(ServerRequest req) {
return ServerResponse.ok().body(this.posts.findAll(), Post.class);
}

public Mono<ServerResponse> create(ServerRequest req) {
return req.bodyToMono(Post.class)
.flatMap(post -> this.posts.save(post))
.flatMap(p -> ServerResponse.created(URI.create("/posts/" + p.getId())).build());
}

public Mono<ServerResponse> get(ServerRequest req) {
return this.posts.findById(req.pathVariable("id"))
.flatMap(post -> ServerResponse.ok().body(Mono.just(post), Post.class))
.switchIfEmpty(ServerResponse.notFound().build());
}

public Mono<ServerResponse> update(ServerRequest req) {

return Mono
.zip(
(data) -> {
Post p = (Post) data[0];
Post p2 = (Post) data[1];
p.setTitle(p2.getTitle());
p.setContent(p2.getContent());
return p;
},
this.posts.findById(req.pathVariable("id")),
req.bodyToMono(Post.class)
)
.cast(Post.class)
.flatMap(post -> this.posts.save(post))
.flatMap(post -> ServerResponse.noContent().build());

}

public Mono<ServerResponse> delete(ServerRequest req) {
return ServerResponse.noContent().build(this.posts.deleteById(req.pathVariable("id")));
}

}

interface PostRepository extends ReactiveCouchbaseRepository<Post, String> {
}

@Document
@Data
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
class Post {

@Id
@GeneratedValue(strategy = GenerationStrategy.UNIQUE)
private String id;
private String title;
private String content;

@CreatedBy
private String createdBy;

@CreatedDate
private LocalDateTime createdAt;

@LastModifiedBy
private String lastModifiedBy;

@LastModifiedDate
private LocalDateTime lastModifiedAt;

@Version
Long version;

}
40 changes: 40 additions & 0 deletions boot-data-couchbase/src/main/java/com/example/demo/Post.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.demo;

import lombok.*;
import org.springframework.data.annotation.*;
import org.springframework.data.couchbase.core.mapping.Document;
import org.springframework.data.couchbase.core.mapping.id.GeneratedValue;
import org.springframework.data.couchbase.core.mapping.id.GenerationStrategy;

import java.time.LocalDateTime;

@Document
@Data
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Post {

@Id
@GeneratedValue(strategy = GenerationStrategy.UNIQUE)
private String id;
private String title;
private String content;

@CreatedBy
private String createdBy;

@CreatedDate
private LocalDateTime createdAt;

@LastModifiedBy
private String lastModifiedBy;

@LastModifiedDate
private LocalDateTime lastModifiedAt;

@Version
Long version;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.example.demo;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.net.URI;

@Component
@RequiredArgsConstructor
public class PostHandler {
private final PostRepository posts;

public Mono<ServerResponse> all(ServerRequest req) {
return ServerResponse.ok().body(this.posts.findAll(), Post.class);
}

public Mono<ServerResponse> create(ServerRequest req) {
return req.bodyToMono(Post.class)
.flatMap(post -> this.posts.save(post))
.flatMap(p -> ServerResponse.created(URI.create("/posts/" + p.getId())).build());
}

public Mono<ServerResponse> get(ServerRequest req) {
return this.posts.findById(req.pathVariable("id"))
.flatMap(post -> ServerResponse.ok().body(Mono.just(post), Post.class))
.switchIfEmpty(ServerResponse.notFound().build());
}

public Mono<ServerResponse> update(ServerRequest req) {

return Mono
.zip(
(data) -> {
Post p = (Post) data[0];
Post p2 = (Post) data[1];
p.setTitle(p2.getTitle());
p.setContent(p2.getContent());
return p;
},
this.posts.findById(req.pathVariable("id")),
req.bodyToMono(Post.class)
)
.cast(Post.class)
.flatMap(post -> this.posts.save(post))
.flatMap(post -> ServerResponse.noContent().build());

}

public Mono<ServerResponse> delete(ServerRequest req) {
return ServerResponse.noContent().build(this.posts.deleteById(req.pathVariable("id")));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.demo;

import org.springframework.data.couchbase.repository.ReactiveCouchbaseRepository;

public interface PostRepository extends ReactiveCouchbaseRepository<Post, String> {
}
21 changes: 21 additions & 0 deletions boot-data-couchbase/src/main/java/com/example/demo/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RequestPredicates.*;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

@Configuration
public class WebConfig {
@Bean
public RouterFunction<ServerResponse> routes(PostHandler handler) {
return route(GET("/posts"), handler::all)
.andRoute(POST("/posts"), handler::create)
.andRoute(GET("/posts/{id}"), handler::get)
.andRoute(PUT("/posts/{id}"), handler::update)
.andRoute(DELETE("/posts/{id}"), handler::delete);
}
}
9 changes: 5 additions & 4 deletions boot-data-couchbase/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# couchbase connection
spring.couchbase.connection-string=couchbase://localhost
spring.couchbase.username=Administrator
spring.couchbase.password=password
# couchbase bucket name
#spring.couchbase.connection-string=couchbase://localhost
#spring.couchbase.username=Administrator
#spring.couchbase.password=password
## couchbase bucket name
# required to activate spring testcontainers???
spring.data.couchbase.bucket-name=demo
# by default, it is disabled, you have to enable auto indexing in Couchbase console.
spring.data.couchbase.autoIndex=true
Expand Down
Loading

0 comments on commit 26684c0

Please sign in to comment.