forked from hantsy/spring-reactive-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
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
6 changed files
with
217 additions
and
165 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
session/src/main/java/com/example/demo/DataInitializr.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,45 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package com.example.demo; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.stereotype.Component; | ||
import reactor.core.publisher.Flux; | ||
|
||
/** | ||
* | ||
* @author hantsy | ||
*/ | ||
@Component | ||
@Slf4j | ||
class DataInitializr implements CommandLineRunner { | ||
|
||
private final PostRepository posts; | ||
|
||
public DataInitializr(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().title(title).content("content of " + title).build())) | ||
) | ||
.log() | ||
.subscribe( | ||
null, | ||
null, | ||
() -> 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
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,37 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package com.example.demo; | ||
|
||
import java.time.LocalDateTime; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
/** | ||
* | ||
* @author hantsy | ||
*/ | ||
@Document | ||
@Data | ||
@ToString | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
class Post { | ||
|
||
@Id | ||
private String id; | ||
private String title; | ||
private String content; | ||
@CreatedDate | ||
private LocalDateTime createdDate; | ||
|
||
} |
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,67 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package com.example.demo; | ||
|
||
import java.net.URI; | ||
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; | ||
|
||
/** | ||
* | ||
* @author hantsy | ||
*/ | ||
@Component | ||
class PostHandler { | ||
|
||
private final PostRepository posts; | ||
|
||
public PostHandler(PostRepository posts) { | ||
this.posts = 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"))); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
session/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,14 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package com.example.demo; | ||
|
||
import org.springframework.data.mongodb.repository.ReactiveMongoRepository; | ||
|
||
/** | ||
* | ||
* @author hantsy | ||
*/ | ||
interface PostRepository extends ReactiveMongoRepository<Post, String> {} |
Oops, something went wrong.