Skip to content

Instantly share code, notes, and snippets.

@YEMEAC
Created October 2, 2018 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YEMEAC/b4cf8b75c2d5d5bed3da08eb1bac918e to your computer and use it in GitHub Desktop.
Save YEMEAC/b4cf8b75c2d5d5bed3da08eb1bac918e to your computer and use it in GitHub Desktop.
Wanted to throw an exception using Reactive Spring only if username a new user wanted to have was being used for someone what was't him
private Mono<UserDto> validateUpdate(Long partyId, UserDto request) {
return partyClient.getIndividual(partyId)
.flatMap(r -> securityClient.getByUserName(request.getUsername()))
.switchIfEmpty(Mono.just(UserModel.builder().withId(partyId).build())) //dummy to have something to compare with and kee the reactive flow "flowing"
.flatMap(r -> {
if (!r.getId().equals(partyId)) {
throw new UserAlreadyExistsException("Username already taken");
}
return Mono.just(request);
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment