-
-
Save pawelpluta/afd3dfced6e9d1f286daae0cc42ededc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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