Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@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
You can’t perform that action at this time.