From 2751527df35abb082ff38d90092dfd303fe64a2d Mon Sep 17 00:00:00 2001 From: JSoi Date: Fri, 22 Jul 2022 22:26:15 +0900 Subject: [PATCH] =?UTF-8?q?#18=20[Refactor]=20=EC=9C=84=EB=8F=84/=EA=B2=BD?= =?UTF-8?q?=EB=8F=84=20->=20=EC=A3=BC=EC=86=8C=20=EB=B0=98=ED=99=98=20?= =?UTF-8?q?=ED=98=95=ED=83=9C=20DTO=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/controller/LocationController.java | 2 +- .../baechelin/api/dto/LocationPartDto.java | 11 ++++++- .../api/service/LocationService.java | 31 ++++++++++--------- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/mpnp/baechelin/api/controller/LocationController.java b/src/main/java/com/mpnp/baechelin/api/controller/LocationController.java index 96b99ad..22b0360 100644 --- a/src/main/java/com/mpnp/baechelin/api/controller/LocationController.java +++ b/src/main/java/com/mpnp/baechelin/api/controller/LocationController.java @@ -23,7 +23,7 @@ public class LocationController { @ApiOperation(value = "위도, 경도를 주소로 바꾸는 함수") @GetMapping("/convert/geo") - public Map getAddressByGeo(@RequestParam BigDecimal lat, @RequestParam BigDecimal lng) { + public LocationPartDto.Address getAddressByGeo(@RequestParam BigDecimal lat, @RequestParam BigDecimal lng) { return locationService.convertGeoToAddressRT(String.valueOf(lat), String.valueOf(lng)); } diff --git a/src/main/java/com/mpnp/baechelin/api/dto/LocationPartDto.java b/src/main/java/com/mpnp/baechelin/api/dto/LocationPartDto.java index 8cf44eb..bfdba51 100644 --- a/src/main/java/com/mpnp/baechelin/api/dto/LocationPartDto.java +++ b/src/main/java/com/mpnp/baechelin/api/dto/LocationPartDto.java @@ -12,7 +12,7 @@ public class LocationPartDto { @Builder public static class LatLong{ @Builder.Default - private boolean status = true; + private boolean status = false; private String latitude; private String longitude; @@ -20,4 +20,13 @@ public boolean validate() { return this.latitude != null && this.longitude != null; } } + + @AllArgsConstructor + @Getter @Setter + @Builder + public static class Address{ + @Builder.Default + private boolean status = false; + private String address; + } } diff --git a/src/main/java/com/mpnp/baechelin/api/service/LocationService.java b/src/main/java/com/mpnp/baechelin/api/service/LocationService.java index c91d4f5..19ac6e8 100644 --- a/src/main/java/com/mpnp/baechelin/api/service/LocationService.java +++ b/src/main/java/com/mpnp/baechelin/api/service/LocationService.java @@ -34,7 +34,7 @@ public class LocationService { * @param address 주소 * @return LocationKeywordSearchForm의 규격에 맞는 결과 하나를 가져옴 */ - public LocationKeywordSearchForm getLatLngByAddressWC(String address) { + public LocationKeywordSearchForm convertAddressToGeoWC(String address) { WebClient client = WebClient.builder() .baseUrl("https://dapi.kakao.com/v2/local/search/keyword.json") .defaultUriVariables(Collections.singletonMap("url", "https://dapi.kakao.com/v2/local/search/keyword.json")) @@ -280,7 +280,7 @@ private String categoryFilter(String category) { * @param lng 경도 * @return 위도, 경도를 카카오맵 API(RestTemplate)를 통해 주소로 변환 후 Map에 넣어 반환 */ - public Map convertGeoToAddressRT(String lat, String lng) { + public LocationPartDto.Address convertGeoToAddressRT(String lat, String lng) { HttpHeaders headers = new HttpHeaders(); headers.setAccept(List.of(MediaType.APPLICATION_JSON)); headers.setContentType(MediaType.APPLICATION_JSON); @@ -298,18 +298,21 @@ public Map convertGeoToAddressRT(String lat, String lng) { uri, HttpMethod.GET, new HttpEntity<>(headers), LocationAddressSearchForm.class ); LocationAddressSearchForm locationKeywordSearchForm = resultRe.getBody(); - Map map = new HashMap<>(); - if (locationKeywordSearchForm == null) { - map.put("status", false); - } else { - LocationAddressSearchForm.TotalAddress address = Arrays.stream(locationKeywordSearchForm.getDocuments()).findFirst().orElse(null); - if (address != null) { - map.put("address", address.getAddress().getAddress_name()); - map.put("status", true); - } else { - map.put("status", false); - } + return formToDto(locationKeywordSearchForm); + } + + private LocationPartDto.Address formToDto(LocationAddressSearchForm resultRe) { + LocationPartDto.Address addressInfoDto = LocationPartDto.Address.builder().build(); + if (resultRe == null) { + return addressInfoDto; + } + LocationAddressSearchForm.TotalAddress address = Arrays.stream(resultRe.getDocuments()).findFirst().orElse(null); + if (address != null) { + addressInfoDto = LocationPartDto.Address.builder() + .address(address.getAddress().getAddress_name()) + .status(true) + .build(); } - return map; + return addressInfoDto; } }