10
10
import org .springframework .web .reactive .function .server .ServerResponse ;
11
11
import reactor .core .publisher .Mono ;
12
12
13
+ import static org .springframework .web .reactive .function .BodyInserters .*;
14
+
13
15
@ Component
14
16
public class ContactRestHandler {
15
17
private final ContactRepository contactRepository ;
@@ -25,45 +27,50 @@ public ContactRestHandler(ContactRepository contactRepository) {
25
27
this .contactRepository = contactRepository ;
26
28
}
27
29
28
- public Mono <ServerResponse > getAllContacts (ServerRequest request ) {
29
- return ServerResponse .ok ()
30
- .contentType (MediaType .APPLICATION_JSON )
31
- .body (contactRepository .findAll (), Contact .class );
32
- }
33
-
30
+ //GET - find a contact by id
34
31
public Mono <ServerResponse > getById (ServerRequest request ) {
35
32
String id = request .pathVariable ("id" );
36
33
37
34
return contactRepository .findById (id )
38
35
.flatMap (contact ->
39
36
ServerResponse .ok ()
40
37
.contentType (MediaType .APPLICATION_JSON )
41
- .body (contact , Contact . class )
38
+ .body (fromValue ( contact ) )
42
39
).switchIfEmpty (response404 );
43
40
}
44
41
42
+ //List all contacts
43
+ public Mono <ServerResponse > getAllContacts (ServerRequest request ) {
44
+ return ServerResponse .ok ()
45
+ .contentType (MediaType .APPLICATION_JSON )
46
+ .body (contactRepository .findAll (), Contact .class );
47
+ }
48
+
49
+ //Find a Contact by email address.
45
50
public Mono <ServerResponse > getByEmail (ServerRequest request ) {
46
51
String email = request .pathVariable ("email" );
47
52
48
53
return contactRepository .findFirstByEmail (email )
49
54
.flatMap (contact ->
50
55
ServerResponse .ok ()
51
56
.contentType (MediaType .APPLICATION_JSON )
52
- .body (contact , Contact . class )
57
+ .body (fromValue ( contact ) )
53
58
).switchIfEmpty (response404 );
54
59
}
55
60
61
+ //Save a new Contact
56
62
public Mono <ServerResponse > insertContact (ServerRequest request ) {
57
63
Mono <Contact > unsavedContact = request .bodyToMono (Contact .class );
58
64
59
65
return unsavedContact
60
66
.flatMap (contact -> contactRepository .save (contact )
61
67
.flatMap (savedContact -> ServerResponse .accepted ()
62
68
.contentType (MediaType .APPLICATION_JSON )
63
- .body (savedContact , Contact . class ))
69
+ .body (fromValue ( savedContact ) ))
64
70
).switchIfEmpty (response406 );
65
71
}
66
72
73
+ //Update an existing contact
67
74
public Mono <ServerResponse > updateContact (ServerRequest request ) {
68
75
Mono <Contact > contact$ = request .bodyToMono (Contact .class );
69
76
String id = request .pathVariable ("id" );
@@ -83,11 +90,12 @@ public Mono<ServerResponse> updateContact(ServerRequest request) {
83
90
return updatedContact$ .flatMap (contact ->
84
91
ServerResponse .accepted ()
85
92
.contentType (MediaType .APPLICATION_JSON )
86
- .body (contact , Contact . class )
93
+ .body (fromValue ( contact ) )
87
94
).switchIfEmpty (response404 );
88
95
}
89
96
90
- public Mono <ServerResponse > deleteContact (ServerRequest request ){
97
+ //Delete a Contact
98
+ public Mono <ServerResponse > deleteContact (ServerRequest request ) {
91
99
String id = request .pathVariable ("id" );
92
100
Mono <Void > deleted = contactRepository .deleteById (id );
93
101
0 commit comments