Skip to content

Commit 26684c0

Browse files
committed
chore: clean codes
1 parent 74503e7 commit 26684c0

21 files changed

+357
-340
lines changed

.github/workflows/boot-data-couchbase.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ jobs:
3232
# see: https://github.com/testcontainers/testcontainers-java/discussions/4676
3333
run: |
3434
cd boot-data-couchbase
35-
mvn -q clean test -Dtest=PostRepositoryWithTestcontainresTest
35+
mvn -q clean test -Dtest=PostRepositoryWithTestcontainersTest
3636
docker rm $(docker ps -aq) -f
3737
mvn -q clean test -Dtest=PostRepositoryTest

boot-data-couchbase/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
<artifactId>reactor-test</artifactId>
4949
<scope>test</scope>
5050
</dependency>
51+
<dependency>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-testcontainers</artifactId>
54+
<scope>test</scope>
55+
</dependency>
5156
<dependency>
5257
<groupId>org.testcontainers</groupId>
5358
<artifactId>junit-jupiter</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.example.demo;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.data.couchbase.repository.auditing.EnableReactiveCouchbaseAuditing;
6+
import org.springframework.data.domain.ReactiveAuditorAware;
7+
import reactor.core.publisher.Mono;
8+
9+
@Configuration(proxyBeanMethods = false)
10+
// see: https://jira.spring.io/browse/DATACOUCH-644
11+
@EnableReactiveCouchbaseAuditing
12+
public class DataConfig {
13+
14+
@Bean
15+
public ReactiveAuditorAware<String> reactiveAuditorAware() {
16+
return () -> Mono.just("hantsy");
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.example.demo;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.boot.CommandLineRunner;
6+
import org.springframework.context.annotation.Profile;
7+
import org.springframework.stereotype.Component;
8+
import reactor.core.publisher.Flux;
9+
10+
import java.util.UUID;
11+
12+
@Component
13+
@RequiredArgsConstructor
14+
@Slf4j
15+
@Profile("dev")
16+
public class DataInitializer implements CommandLineRunner {
17+
18+
private final PostRepository posts;
19+
20+
@Override
21+
public void run(String[] args) {
22+
log.info("start data initialization ...");
23+
this.posts
24+
.deleteAll()
25+
.thenMany(
26+
Flux
27+
.just("Post one", "Post two")
28+
.flatMap(
29+
title -> this.posts.save(Post.builder().id(UUID.randomUUID().toString()).title(title).content("content of " + title).build())
30+
)
31+
)
32+
.log("[initialization log]")
33+
.subscribe(
34+
data -> log.info("saved data: {}", data),
35+
error -> log.error("error: {}", error.getMessage()),
36+
() -> log.info("done initialization...")
37+
);
38+
39+
}
40+
41+
}

boot-data-couchbase/src/main/java/com/example/demo/DemoApplication.java

-137
Original file line numberDiff line numberDiff line change
@@ -38,140 +38,3 @@ public static void main(String[] args) {
3838

3939
}
4040

41-
@Configuration
42-
class WebConfig {
43-
@Bean
44-
public RouterFunction<ServerResponse> routes(PostHandler handler) {
45-
return route(GET("/posts"), handler::all)
46-
.andRoute(POST("/posts"), handler::create)
47-
.andRoute(GET("/posts/{id}"), handler::get)
48-
.andRoute(PUT("/posts/{id}"), handler::update)
49-
.andRoute(DELETE("/posts/{id}"), handler::delete);
50-
}
51-
}
52-
53-
@Configuration(proxyBeanMethods = false)
54-
// see: https://jira.spring.io/browse/DATACOUCH-644
55-
@EnableReactiveCouchbaseAuditing
56-
class DataConfig {
57-
58-
@Bean
59-
public ReactiveAuditorAware<String> reactiveAuditorAware() {
60-
return () -> Mono.just("hantsy");
61-
}
62-
}
63-
64-
@Component
65-
@Slf4j
66-
@Profile("default")
67-
class DataInitializer implements CommandLineRunner {
68-
69-
private final PostRepository posts;
70-
71-
public DataInitializer(PostRepository posts) {
72-
this.posts = posts;
73-
}
74-
75-
@Override
76-
public void run(String[] args) {
77-
log.info("start data initialization ...");
78-
this.posts
79-
.deleteAll()
80-
.thenMany(
81-
Flux
82-
.just("Post one", "Post two")
83-
.flatMap(
84-
title -> this.posts.save(Post.builder().id(UUID.randomUUID().toString()).title(title).content("content of " + title).build())
85-
)
86-
)
87-
.log("[initialization log]")
88-
.subscribe(
89-
data -> log.info("saved data: {}", data),
90-
error -> log.error("error: {}", error.getMessage()),
91-
() -> log.info("done initialization...")
92-
);
93-
94-
}
95-
96-
}
97-
98-
@Component
99-
@RequiredArgsConstructor
100-
class PostHandler {
101-
private final PostRepository posts;
102-
103-
public Mono<ServerResponse> all(ServerRequest req) {
104-
return ServerResponse.ok().body(this.posts.findAll(), Post.class);
105-
}
106-
107-
public Mono<ServerResponse> create(ServerRequest req) {
108-
return req.bodyToMono(Post.class)
109-
.flatMap(post -> this.posts.save(post))
110-
.flatMap(p -> ServerResponse.created(URI.create("/posts/" + p.getId())).build());
111-
}
112-
113-
public Mono<ServerResponse> get(ServerRequest req) {
114-
return this.posts.findById(req.pathVariable("id"))
115-
.flatMap(post -> ServerResponse.ok().body(Mono.just(post), Post.class))
116-
.switchIfEmpty(ServerResponse.notFound().build());
117-
}
118-
119-
public Mono<ServerResponse> update(ServerRequest req) {
120-
121-
return Mono
122-
.zip(
123-
(data) -> {
124-
Post p = (Post) data[0];
125-
Post p2 = (Post) data[1];
126-
p.setTitle(p2.getTitle());
127-
p.setContent(p2.getContent());
128-
return p;
129-
},
130-
this.posts.findById(req.pathVariable("id")),
131-
req.bodyToMono(Post.class)
132-
)
133-
.cast(Post.class)
134-
.flatMap(post -> this.posts.save(post))
135-
.flatMap(post -> ServerResponse.noContent().build());
136-
137-
}
138-
139-
public Mono<ServerResponse> delete(ServerRequest req) {
140-
return ServerResponse.noContent().build(this.posts.deleteById(req.pathVariable("id")));
141-
}
142-
143-
}
144-
145-
interface PostRepository extends ReactiveCouchbaseRepository<Post, String> {
146-
}
147-
148-
@Document
149-
@Data
150-
@ToString
151-
@Builder
152-
@NoArgsConstructor
153-
@AllArgsConstructor
154-
class Post {
155-
156-
@Id
157-
@GeneratedValue(strategy = GenerationStrategy.UNIQUE)
158-
private String id;
159-
private String title;
160-
private String content;
161-
162-
@CreatedBy
163-
private String createdBy;
164-
165-
@CreatedDate
166-
private LocalDateTime createdAt;
167-
168-
@LastModifiedBy
169-
private String lastModifiedBy;
170-
171-
@LastModifiedDate
172-
private LocalDateTime lastModifiedAt;
173-
174-
@Version
175-
Long version;
176-
177-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.example.demo;
2+
3+
import lombok.*;
4+
import org.springframework.data.annotation.*;
5+
import org.springframework.data.couchbase.core.mapping.Document;
6+
import org.springframework.data.couchbase.core.mapping.id.GeneratedValue;
7+
import org.springframework.data.couchbase.core.mapping.id.GenerationStrategy;
8+
9+
import java.time.LocalDateTime;
10+
11+
@Document
12+
@Data
13+
@ToString
14+
@Builder
15+
@NoArgsConstructor
16+
@AllArgsConstructor
17+
public class Post {
18+
19+
@Id
20+
@GeneratedValue(strategy = GenerationStrategy.UNIQUE)
21+
private String id;
22+
private String title;
23+
private String content;
24+
25+
@CreatedBy
26+
private String createdBy;
27+
28+
@CreatedDate
29+
private LocalDateTime createdAt;
30+
31+
@LastModifiedBy
32+
private String lastModifiedBy;
33+
34+
@LastModifiedDate
35+
private LocalDateTime lastModifiedAt;
36+
37+
@Version
38+
Long version;
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.example.demo;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.springframework.stereotype.Component;
5+
import org.springframework.web.reactive.function.server.ServerRequest;
6+
import org.springframework.web.reactive.function.server.ServerResponse;
7+
import reactor.core.publisher.Mono;
8+
9+
import java.net.URI;
10+
11+
@Component
12+
@RequiredArgsConstructor
13+
public class PostHandler {
14+
private final PostRepository posts;
15+
16+
public Mono<ServerResponse> all(ServerRequest req) {
17+
return ServerResponse.ok().body(this.posts.findAll(), Post.class);
18+
}
19+
20+
public Mono<ServerResponse> create(ServerRequest req) {
21+
return req.bodyToMono(Post.class)
22+
.flatMap(post -> this.posts.save(post))
23+
.flatMap(p -> ServerResponse.created(URI.create("/posts/" + p.getId())).build());
24+
}
25+
26+
public Mono<ServerResponse> get(ServerRequest req) {
27+
return this.posts.findById(req.pathVariable("id"))
28+
.flatMap(post -> ServerResponse.ok().body(Mono.just(post), Post.class))
29+
.switchIfEmpty(ServerResponse.notFound().build());
30+
}
31+
32+
public Mono<ServerResponse> update(ServerRequest req) {
33+
34+
return Mono
35+
.zip(
36+
(data) -> {
37+
Post p = (Post) data[0];
38+
Post p2 = (Post) data[1];
39+
p.setTitle(p2.getTitle());
40+
p.setContent(p2.getContent());
41+
return p;
42+
},
43+
this.posts.findById(req.pathVariable("id")),
44+
req.bodyToMono(Post.class)
45+
)
46+
.cast(Post.class)
47+
.flatMap(post -> this.posts.save(post))
48+
.flatMap(post -> ServerResponse.noContent().build());
49+
50+
}
51+
52+
public Mono<ServerResponse> delete(ServerRequest req) {
53+
return ServerResponse.noContent().build(this.posts.deleteById(req.pathVariable("id")));
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.example.demo;
2+
3+
import org.springframework.data.couchbase.repository.ReactiveCouchbaseRepository;
4+
5+
public interface PostRepository extends ReactiveCouchbaseRepository<Post, String> {
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.example.demo;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.reactive.function.server.RouterFunction;
6+
import org.springframework.web.reactive.function.server.ServerResponse;
7+
8+
import static org.springframework.web.reactive.function.server.RequestPredicates.*;
9+
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
10+
11+
@Configuration
12+
public class WebConfig {
13+
@Bean
14+
public RouterFunction<ServerResponse> routes(PostHandler handler) {
15+
return route(GET("/posts"), handler::all)
16+
.andRoute(POST("/posts"), handler::create)
17+
.andRoute(GET("/posts/{id}"), handler::get)
18+
.andRoute(PUT("/posts/{id}"), handler::update)
19+
.andRoute(DELETE("/posts/{id}"), handler::delete);
20+
}
21+
}

boot-data-couchbase/src/main/resources/application.properties

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# couchbase connection
2-
spring.couchbase.connection-string=couchbase://localhost
3-
spring.couchbase.username=Administrator
4-
spring.couchbase.password=password
5-
# couchbase bucket name
2+
#spring.couchbase.connection-string=couchbase://localhost
3+
#spring.couchbase.username=Administrator
4+
#spring.couchbase.password=password
5+
## couchbase bucket name
6+
# required to activate spring testcontainers???
67
spring.data.couchbase.bucket-name=demo
78
# by default, it is disabled, you have to enable auto indexing in Couchbase console.
89
spring.data.couchbase.autoIndex=true

0 commit comments

Comments
 (0)