@@ -49,7 +49,8 @@ public ResponseEntity updateAccount(@RequestBody Account account, @PathVariable
49
49
50
50
@ RequestMapping (path = "/accounts/{id}" )
51
51
public ResponseEntity getAccount (@ PathVariable Long id ) {
52
- return Optional .ofNullable (getAccountResource (id ))
52
+ return Optional .ofNullable (accountService .get (id ))
53
+ .map (this ::getAccountResource )
53
54
.map (e -> new ResponseEntity <>(e , HttpStatus .OK ))
54
55
.orElse (new ResponseEntity <>(HttpStatus .NOT_FOUND ));
55
56
}
@@ -91,57 +92,48 @@ public ResponseEntity getCommands(@PathVariable Long id) {
91
92
92
93
@ RequestMapping (path = "/accounts/{id}/commands/confirm" )
93
94
public ResponseEntity confirm (@ PathVariable Long id ) {
94
- return Optional .ofNullable (getAccountResource (accountService .get (id )
95
- .confirm ()))
95
+ return Optional .ofNullable (accountService .get (id ))
96
+ .map (Account ::confirm )
97
+ .map (this ::getAccountResource )
96
98
.map (e -> new ResponseEntity <>(e , HttpStatus .OK ))
97
99
.orElseThrow (() -> new RuntimeException ("The command could not be applied" ));
98
100
}
99
101
100
102
@ RequestMapping (path = "/accounts/{id}/commands/activate" )
101
103
public ResponseEntity activate (@ PathVariable Long id ) {
102
- return Optional .ofNullable (getAccountResource (accountService .get (id )
103
- .activate ()))
104
+ return Optional .ofNullable (accountService .get (id ))
105
+ .map (Account ::activate )
106
+ .map (this ::getAccountResource )
104
107
.map (e -> new ResponseEntity <>(e , HttpStatus .OK ))
105
108
.orElseThrow (() -> new RuntimeException ("The command could not be applied" ));
106
109
}
107
110
108
111
@ RequestMapping (path = "/accounts/{id}/commands/suspend" )
109
112
public ResponseEntity suspend (@ PathVariable Long id ) {
110
- return Optional .ofNullable (getAccountResource (accountService .get (id )
111
- .suspend ()))
113
+ return Optional .ofNullable (accountService .get (id ))
114
+ .map (Account ::suspend )
115
+ .map (this ::getAccountResource )
112
116
.map (e -> new ResponseEntity <>(e , HttpStatus .OK ))
113
117
.orElseThrow (() -> new RuntimeException ("The command could not be applied" ));
114
118
}
115
119
116
120
@ RequestMapping (path = "/accounts/{id}/commands/archive" )
117
121
public ResponseEntity archive (@ PathVariable Long id ) {
118
- return Optional .ofNullable (getAccountResource (accountService .get (id )
119
- .archive ()))
122
+ return Optional .ofNullable (accountService .get (id ))
123
+ .map (Account ::archive )
124
+ .map (this ::getAccountResource )
120
125
.map (e -> new ResponseEntity <>(e , HttpStatus .OK ))
121
126
.orElseThrow (() -> new RuntimeException ("The command could not be applied" ));
122
127
}
123
128
124
129
@ RequestMapping (path = "/accounts/{id}/commands/postOrder" , method = RequestMethod .POST )
125
130
public ResponseEntity postOrder (@ PathVariable Long id , @ RequestBody Order order ) {
126
- return Optional .ofNullable (accountService .get (id )
127
- .postOrder (order ))
128
- .map (e -> new ResponseEntity <>(e , HttpStatus .OK ))
131
+ return Optional .ofNullable (accountService .get (id ))
132
+ .map ( a -> a . postOrder (order ))
133
+ .map (o -> new ResponseEntity <>(o , HttpStatus .CREATED ))
129
134
.orElseThrow (() -> new RuntimeException ("The command could not be applied" ));
130
135
}
131
136
132
- /**
133
- * Retrieves a hypermedia resource for {@link Account} with the specified identifier.
134
- *
135
- * @param id is the unique identifier for looking up the {@link Account} entity
136
- * @return a hypermedia resource for the fetched {@link Account}
137
- */
138
- private Resource <Account > getAccountResource (Long id ) {
139
- // Get the account for the provided id
140
- Account account = accountService .get (id );
141
-
142
- return getAccountResource (account );
143
- }
144
-
145
137
/**
146
138
* Creates a new {@link Account} entity and persists the result to the repository.
147
139
*
@@ -247,17 +239,17 @@ private LinkBuilder linkBuilder(String name, Long id) {
247
239
private Resource <Account > getAccountResource (Account account ) {
248
240
Assert .notNull (account , "Account must not be null" );
249
241
250
- if ( account .getLink ("commands" ) == null ) {
242
+ if (! account .hasLink ("commands" )) {
251
243
// Add command link
252
244
account .add (linkBuilder ("getCommands" , account .getIdentity ()).withRel ("commands" ));
253
245
}
254
246
255
- if ( account .getLink ("events" ) == null ) {
247
+ if (! account .hasLink ("events" )) {
256
248
// Add get events link
257
249
account .add (linkBuilder ("getAccountEvents" , account .getIdentity ()).withRel ("events" ));
258
250
}
259
251
260
- if ( account .getLink ("orders" ) == null ) {
252
+ if (! account .hasLink ("orders" )) {
261
253
// Add orders link
262
254
account .add (linkBuilder ("getAccountOrders" , account .getIdentity ()).withRel ("orders" ));
263
255
}
0 commit comments