-
-
Notifications
You must be signed in to change notification settings - Fork 466
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
357 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
boot-data-couchbase/src/main/java/com/example/demo/DataConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
boot-data-couchbase/src/main/java/com/example/demo/DataInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...") | ||
); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
boot-data-couchbase/src/main/java/com/example/demo/Post.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
boot-data-couchbase/src/main/java/com/example/demo/PostHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
boot-data-couchbase/src/main/java/com/example/demo/PostRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
boot-data-couchbase/src/main/java/com/example/demo/WebConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
9
boot-data-couchbase/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.