Skip to content

Instantly share code, notes, and snippets.

@pawelpluta
Last active February 2, 2020 19:13
Show Gist options
  • Save pawelpluta/afd3dfced6e9d1f286daae0cc42ededc to your computer and use it in GitHub Desktop.
Save pawelpluta/afd3dfced6e9d1f286daae0cc42ededc to your computer and use it in GitHub Desktop.
@RestController
@RequestMapping("/api/users")
class UserDetailsController {
private final UserRepository userRepository;
UserDetailsController(UserRepository userRepository) {
this.userRepository = userRepository;
}
@GetMapping("/{userId}")
ResponseEntity<UserDetails> getByUserId(@PathVariable("userId") String userId) {
return userRepository.findById(UserId.of(userId))
.map(toUserDetails())
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
}
private Function<User, UserDetails> toUserDetails() {
return (user) -> new UserDetails(idOf(user), displayNameOf(user));
}
private String idOf(User user) {
return user.getId().value();
}
private String displayNameOf(User user) {
return user.getFirstName() + " " + user.getLastName();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment