Skip to content

Commit cd40477

Browse files
committed
make it more readble
1 parent afcace8 commit cd40477

22 files changed

Lines changed: 156 additions & 156 deletions

src/main/java/org/learning/by/example/reactive/microservices/application/ApplicationConfig.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ HelloService helloService() {
2626

2727
@Bean
2828
ApiHandler apiHandler(final HelloService helloService, final QuoteService quoteService,
29-
final LocationService locationService, final SunriseSunsetService sunriseSunsetService,
29+
final GeoLocationService geoLocationService, final SunriseSunsetService sunriseSunsetService,
3030
final ErrorHandler errorHandler) {
31-
return new ApiHandler(helloService, quoteService, locationService, sunriseSunsetService ,errorHandler);
31+
return new ApiHandler(helloService, quoteService, geoLocationService, sunriseSunsetService ,errorHandler);
3232
}
3333

3434
@Bean
35-
LocationService locationService(@Value("${LocationServiceImpl.endPoint}") final String endPoint){
36-
return new LocationServiceImpl(endPoint);
35+
GeoLocationService locationService(@Value("${GeoLocationServiceImpl.endPoint}") final String endPoint){
36+
return new GeoLocationServiceImpl(endPoint);
3737
}
3838

3939
@Bean
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.learning.by.example.reactive.microservices.exceptions;
2+
3+
public class GetGeoLocationException extends Exception{
4+
public GetGeoLocationException(final String message, final Throwable throwable) {
5+
super(message, throwable);
6+
}
7+
8+
public GetGeoLocationException(final String message) {
9+
super(message);
10+
}
11+
}

src/main/java/org/learning/by/example/reactive/microservices/exceptions/GetLocationException.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/main/java/org/learning/by/example/reactive/microservices/handlers/ApiHandler.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.learning.by.example.reactive.microservices.handlers;
22

33
import org.learning.by.example.reactive.microservices.model.*;
4+
import org.learning.by.example.reactive.microservices.services.GeoLocationService;
45
import org.learning.by.example.reactive.microservices.services.HelloService;
5-
import org.learning.by.example.reactive.microservices.services.LocationService;
66
import org.learning.by.example.reactive.microservices.services.QuoteService;
77
import org.learning.by.example.reactive.microservices.services.SunriseSunsetService;
88
import org.springframework.web.reactive.function.server.ServerRequest;
@@ -17,18 +17,18 @@ public class ApiHandler {
1717
private final ErrorHandler errorHandler;
1818
private final HelloService helloService;
1919
private final QuoteService quoteService;
20-
private final LocationService locationService;
20+
private final GeoLocationService geoLocationService;
2121
private final SunriseSunsetService sunriseSunsetService;
2222

2323
private static final Mono<String> DEFAULT_NAME = Mono.just("world");
2424

2525
public ApiHandler(final HelloService helloService, final QuoteService quoteService,
26-
final LocationService locationService, final SunriseSunsetService sunriseSunsetService,
26+
final GeoLocationService geoLocationService, final SunriseSunsetService sunriseSunsetService,
2727
final ErrorHandler errorHandler) {
2828
this.errorHandler = errorHandler;
2929
this.helloService = helloService;
3030
this.quoteService = quoteService;
31-
this.locationService = locationService;
31+
this.geoLocationService = geoLocationService;
3232
this.sunriseSunsetService = sunriseSunsetService;
3333
}
3434

@@ -72,14 +72,14 @@ Mono<ServerResponse> convertToServerResponse(final Mono<HelloResponse> helloResp
7272

7373
public Mono<ServerResponse> getLocation(final ServerRequest request){
7474
return Mono.just(request.pathVariable(ADDRESS))
75-
.transform(locationService::fromAddress)
75+
.transform(geoLocationService::fromAddress)
7676
.and(this::sunriseSunset, LocationResponse::new)
7777
.transform(this::response)
7878
.onErrorResume(errorHandler::throwableError);
7979
}
8080

81-
private Mono<SunriseSunset> sunriseSunset(Location location){
82-
return Mono.just(location).transform(sunriseSunsetService::fromLocation);
81+
private Mono<SunriseSunset> sunriseSunset(GeographicCoordinates geographicCoordinates){
82+
return Mono.just(geographicCoordinates).transform(sunriseSunsetService::fromGeographicCoordinates);
8383
}
8484
Mono<ServerResponse> response(Mono<LocationResponse> locationResponseMono) {
8585
return locationResponseMono.flatMap(locationResponse ->

src/main/java/org/learning/by/example/reactive/microservices/model/LocationResult.java renamed to src/main/java/org/learning/by/example/reactive/microservices/model/GeocodeResult.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import com.fasterxml.jackson.annotation.JsonCreator;
44
import com.fasterxml.jackson.annotation.JsonProperty;
55

6-
public final class LocationResult {
6+
public final class GeocodeResult {
77
private final Result results[];
88
private final String status;
99

1010
@JsonCreator
11-
public LocationResult(@JsonProperty("results") Result[] results, @JsonProperty("status") String status){
11+
public GeocodeResult(@JsonProperty("results") Result[] results, @JsonProperty("status") String status){
1212
this.results = results;
1313
this.status = status;
1414
}

src/main/java/org/learning/by/example/reactive/microservices/model/Location.java renamed to src/main/java/org/learning/by/example/reactive/microservices/model/GeographicCoordinates.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.fasterxml.jackson.annotation.JsonProperty;
55

66

7-
public final class Location {
7+
public final class GeographicCoordinates {
88
public double getLatitude() {
99
return latitude;
1010
}
@@ -17,7 +17,7 @@ public double getLongitude() {
1717
private final double longitude;
1818

1919
@JsonCreator
20-
public Location(@JsonProperty("latitude") double latitude, @JsonProperty("longitude") double longitude){
20+
public GeographicCoordinates(@JsonProperty("latitude") double latitude, @JsonProperty("longitude") double longitude){
2121
this.latitude = latitude;
2222
this.longitude = longitude;
2323
}

src/main/java/org/learning/by/example/reactive/microservices/model/LocationResponse.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
import com.fasterxml.jackson.annotation.JsonProperty;
66

77
public class LocationResponse {
8-
private final Location geographicCoordinates;
8+
private final GeographicCoordinates geographicCoordinates;
99
private final SunriseSunset sunriseSunset;
1010

1111
@JsonCreator
12-
public LocationResponse(@JsonProperty("geographicCoordinates") final Location geographicCoordinates,
12+
public LocationResponse(@JsonProperty("geographicCoordinates") final GeographicCoordinates geographicCoordinates,
1313
@JsonProperty("sunriseSunset") final SunriseSunset sunriseSunset){
1414
this.geographicCoordinates = geographicCoordinates;
1515
this.sunriseSunset = sunriseSunset;
1616
}
1717

18-
public Location getGeographicCoordinates() {
18+
public GeographicCoordinates getGeographicCoordinates() {
1919
return geographicCoordinates;
2020
}
2121

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.learning.by.example.reactive.microservices.services;
2+
3+
import org.learning.by.example.reactive.microservices.model.GeographicCoordinates;
4+
import reactor.core.publisher.Mono;
5+
6+
public interface GeoLocationService {
7+
Mono<GeographicCoordinates> fromAddress(Mono<String> address);
8+
}

src/main/java/org/learning/by/example/reactive/microservices/services/LocationServiceImpl.java renamed to src/main/java/org/learning/by/example/reactive/microservices/services/GeoLocationServiceImpl.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package org.learning.by.example.reactive.microservices.services;
22

3-
import org.learning.by.example.reactive.microservices.exceptions.GetLocationException;
3+
import org.learning.by.example.reactive.microservices.exceptions.GetGeoLocationException;
44
import org.learning.by.example.reactive.microservices.exceptions.LocationNotFoundException;
5-
import org.learning.by.example.reactive.microservices.model.Location;
6-
import org.learning.by.example.reactive.microservices.model.LocationResult;
5+
import org.learning.by.example.reactive.microservices.model.GeographicCoordinates;
6+
import org.learning.by.example.reactive.microservices.model.GeocodeResult;
77
import org.springframework.http.MediaType;
88
import org.springframework.web.reactive.function.client.WebClient;
99
import reactor.core.publisher.Mono;
1010

11-
public class LocationServiceImpl implements LocationService {
11+
public class GeoLocationServiceImpl implements GeoLocationService {
1212

1313
private static final String OK_STATUS = "OK";
1414
private static final String ZERO_RESULTS = "ZERO_RESULTS";
@@ -19,48 +19,48 @@ public class LocationServiceImpl implements LocationService {
1919
WebClient webClient;
2020
private final String endPoint;
2121

22-
public LocationServiceImpl(final String endPoint) {
22+
public GeoLocationServiceImpl(final String endPoint) {
2323
this.endPoint = endPoint;
2424
this.webClient = WebClient.create();
2525
}
2626

2727
@Override
28-
public Mono<Location> fromAddress(final Mono<String> addressMono) {
28+
public Mono<GeographicCoordinates> fromAddress(final Mono<String> addressMono) {
2929
return addressMono
3030
.transform(this::buildUrl)
3131
.transform(this::get)
32-
.onErrorResume(throwable -> Mono.error(new GetLocationException(ERROR_GETTING_LOCATION, throwable)))
32+
.onErrorResume(throwable -> Mono.error(new GetGeoLocationException(ERROR_GETTING_LOCATION, throwable)))
3333
.transform(this::geometryLocation);
3434
}
3535

3636
Mono<String> buildUrl(final Mono<String> addressMono) {
3737
return addressMono.flatMap(address -> Mono.just(endPoint.concat(ADDRESS_PARAMETER).concat(address)));
3838
}
3939

40-
Mono<LocationResult> get(final Mono<String> monoUrl) {
40+
Mono<GeocodeResult> get(final Mono<String> monoUrl) {
4141
return monoUrl.flatMap(url -> webClient
4242
.get()
4343
.uri(url)
4444
.accept(MediaType.APPLICATION_JSON)
4545
.exchange()
46-
.flatMap(clientResponse -> clientResponse.bodyToMono(LocationResult.class)));
46+
.flatMap(clientResponse -> clientResponse.bodyToMono(GeocodeResult.class)));
4747
}
4848

49-
Mono<Location> geometryLocation(final Mono<LocationResult> location) {
50-
return location.flatMap(locationResult -> {
51-
if (locationResult.getStatus() != null) {
52-
switch (locationResult.getStatus()) {
49+
Mono<GeographicCoordinates> geometryLocation(final Mono<GeocodeResult> geocodeResultMono) {
50+
return geocodeResultMono.flatMap(geocodeResult -> {
51+
if (geocodeResult.getStatus() != null) {
52+
switch (geocodeResult.getStatus()) {
5353
case OK_STATUS:
5454
return Mono.just(
55-
new Location(locationResult.getResults()[0].getGeometry().getLocation().getLat(),
56-
locationResult.getResults()[0].getGeometry().getLocation().getLng()));
55+
new GeographicCoordinates(geocodeResult.getResults()[0].getGeometry().getLocation().getLat(),
56+
geocodeResult.getResults()[0].getGeometry().getLocation().getLng()));
5757
case ZERO_RESULTS:
5858
return Mono.error(new LocationNotFoundException(ADDRESS_NOT_FOUND));
5959
default:
60-
return Mono.error(new GetLocationException(ERROR_GETTING_LOCATION));
60+
return Mono.error(new GetGeoLocationException(ERROR_GETTING_LOCATION));
6161
}
6262
}else {
63-
return Mono.error(new GetLocationException(ERROR_LOCATION_WAS_NULL));
63+
return Mono.error(new GetGeoLocationException(ERROR_LOCATION_WAS_NULL));
6464
}
6565
}
6666
);

src/main/java/org/learning/by/example/reactive/microservices/services/LocationService.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)