refactor geocoding to service for internal use + return addressdto
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package SuMoC.backend.geocoding;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
public class AddressDTO {
|
||||
// Getters and setters
|
||||
private String houseNumber;
|
||||
private String road;
|
||||
private String suburb;
|
||||
private String cityDistrict;
|
||||
private String city;
|
||||
private String state;
|
||||
private String iso3166_2_lvl4;
|
||||
private String postcode;
|
||||
private String country;
|
||||
private String countryCode;
|
||||
|
||||
// Constructor with fields
|
||||
public AddressDTO(String houseNumber, String road, String suburb, String cityDistrict, String city, String state, String iso3166_2_lvl4, String postcode, String country, String countryCode) {
|
||||
this.houseNumber = houseNumber;
|
||||
this.road = road;
|
||||
this.suburb = suburb;
|
||||
this.cityDistrict = cityDistrict;
|
||||
this.city = city;
|
||||
this.state = state;
|
||||
this.iso3166_2_lvl4 = iso3166_2_lvl4;
|
||||
this.postcode = postcode;
|
||||
this.country = country;
|
||||
this.countryCode = countryCode;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package SuMoC.backend.geocoding;
|
||||
|
||||
import SuMoC.backend.nominatim.NominatimClient;
|
||||
import SuMoC.backend.nominatim.ReverseResponse;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Service
|
||||
public class GeoCodingService {
|
||||
|
||||
private final NominatimClient nominatimClient;
|
||||
|
||||
public GeoCodingService(NominatimClient nominatimClient) {
|
||||
this.nominatimClient = nominatimClient;
|
||||
}
|
||||
|
||||
public Mono<AddressDTO> getAddress(double lat, double lon) {
|
||||
return nominatimClient.reverse(lat, lon)
|
||||
.map(this::mapToAddressDTO);
|
||||
}
|
||||
|
||||
private AddressDTO mapToAddressDTO(ReverseResponse response) {
|
||||
var addressMap = response.address();
|
||||
return new AddressDTO(
|
||||
addressMap.get("house_number"),
|
||||
addressMap.get("road"),
|
||||
addressMap.get("suburb"),
|
||||
addressMap.get("city_district"),
|
||||
addressMap.get("city"),
|
||||
addressMap.get("state"),
|
||||
addressMap.get("ISO3166-2-lvl4"),
|
||||
addressMap.get("postcode"),
|
||||
addressMap.get("country"),
|
||||
addressMap.get("country_code")
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package SuMoC.backend.geocoding;
|
||||
|
||||
import SuMoC.backend.nominatim.NominatimClient;
|
||||
import SuMoC.backend.nominatim.ReverseResponse;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -13,15 +11,15 @@ import reactor.core.publisher.Mono;
|
||||
@RequestMapping("/geocoding")
|
||||
public class GeocodingController {
|
||||
|
||||
private final NominatimClient nominatimClient;
|
||||
private final GeoCodingService geoCodingService;
|
||||
|
||||
public GeocodingController(NominatimClient nominatimClient) {
|
||||
this.nominatimClient = nominatimClient;
|
||||
public GeocodingController(GeoCodingService geoCodingService) {
|
||||
this.geoCodingService = geoCodingService;
|
||||
}
|
||||
|
||||
@GetMapping("/reverse")
|
||||
@Operation(description = "Returns the OSM location at the given lat / lon including the address.")
|
||||
public Mono<ReverseResponse> reverse(@RequestParam double lat, @RequestParam double lon) {
|
||||
return nominatimClient.reverse(lat, lon);
|
||||
@Operation(description = "Returns the address at the given lat / lon.")
|
||||
public Mono<AddressDTO> reverse(@RequestParam double lat, @RequestParam double lon) {
|
||||
return geoCodingService.getAddress(lat, lon);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user