Skip to content

Instantly share code, notes, and snippets.

@Deviad
Last active May 3, 2020 10:34
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 Deviad/4a77da64e4036fad2131583d06a2e452 to your computer and use it in GitHub Desktop.
Save Deviad/4a77da64e4036fad2131583d06a2e452 to your computer and use it in GitHub Desktop.
Get coords from Google and use them to create address entity and then save a new user
Mono<GeocodingResponseDto> personalAddressGeo = bundle.getGeocodingClient().getGeocoding(URLEncoder.encode(requestDTO.getPersonalAddress().getFirstLine(), StandardCharsets.UTF_8.toString()), apiKey);
Mono<GeocodingResponseDto> companyAddressGeo = bundle.getGeocodingClient().getGeocoding(URLEncoder.encode(requestDTO.getCompanyAddress().getFirstLine(), StandardCharsets.UTF_8.toString()), apiKey);
return personalAddressGeo
.subscribeOn(Schedulers.immediate())
.flatMap(this::handleNotFound)
.doOnNext(x -> {
requestDTO.getPersonalAddress().setLat(x.getLocation().getLat());
requestDTO.getPersonalAddress().setLon(x.getLocation().getLon());
})
.then(companyAddressGeo)
.flatMap(this::handleNotFound)
.doOnNext(x -> {
requestDTO.getCompanyAddress().setLat(x.getLocation().getLat());
requestDTO.getCompanyAddress().setLon(x.getLocation().getLon());
})
.doOnNext(x -> {
UserInfoEntity user = bundle.getMapper().mapToUserInfoEntity(requestDTO);
bundle.getRepository().save(user);
})
.flatMap(x -> getByUsername(requestDTO.getUsername(), false))
.map(UserDTO::getUsername)
.map(this::getUrl);
}
private Mono<? extends Geometry> handleNotFound(GeocodingResponseDto x) {
try {
return Mono.just(x.getResults().get(0).getGeometry());
} catch (Exception ex) {
return Mono.error(new NotFoundException("Invalid address"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment