Last active
May 3, 2020 10:34
-
-
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
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
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