Skip to content

Instantly share code, notes, and snippets.

View YEMEAC's full-sized avatar
🎯
Focusing

YM YEMEAC

🎯
Focusing
View GitHub Profile
@YEMEAC
YEMEAC / ReactiveIf2.java
Created October 2, 2018 13:07
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);
}
@YEMEAC
YEMEAC / consulAddRemoveService.json
Created October 5, 2018 09:58
register and delete service in consul with curl
curl -X PUT \
http://127.0.0.1:8500/v1/catalog/register \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-H 'postman-token: 9aa3163f-31b9-8c8f-f9cd-6feaca81d4ce' \
-d '{
"Datacenter": "dc1",
"Node": "externalServices",
"Address": "172.30.50.59",
"Service": {
@YEMEAC
YEMEAC / ReactiveIf.java
Last active October 16, 2018 10:05
Wanted to throw an exception using Reactive Spring only if a service call had results, also a good trick to add your on function directly in a map
private Mono<UserDto> validateUserNameAvailable(UserDto user) {
Mono<Boolean> result = securityClient.getByUserName(user.getUserName()).hasElement();
return result.flatMap(value -> {
if (value) {
throw new UserAlreadyExistsException(
String.format("User {} already exists", user.getUserName()));
}
return Mono.just(user);
});
}