Skip to content

Commit 4f4625a

Browse files
committed
DEMO-1 endpoint for updating user
1 parent d9295ed commit 4f4625a

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/main/java/com/sergiofreire/xray/tutorials/springboot/boundary/UserRestController.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ public ResponseEntity<User> getCarById(@PathVariable(value = "id") Long id)
3737
return ResponseEntity.ok().body(user);
3838
}
3939

40+
@PutMapping("/users/{id}")
41+
public ResponseEntity<User> updateUser(@PathVariable(value = "id") Long id, @RequestBody User userDetails)
42+
throws ResourceNotFoundException {
43+
User user = userService.getUserDetails(id)
44+
.orElseThrow(() -> new ResourceNotFoundException("User not found for id: " + id));
45+
user.setName(userDetails.getName());
46+
user.setUsername(userDetails.getUsername());
47+
user.setPassword(userDetails.getPassword());
48+
final User updatedUser = userService.save(user);
49+
return ResponseEntity.ok(updatedUser);
50+
}
51+
4052
@GetMapping(path="/users" )
4153
public List<User> getAllUsers() {
4254
return userService.getAllUsers();

0 commit comments

Comments
 (0)