@@ -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- }
0 commit comments