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 22b0360..b529878 100644 --- a/src/main/java/com/mpnp/baechelin/api/controller/LocationController.java +++ b/src/main/java/com/mpnp/baechelin/api/controller/LocationController.java @@ -12,7 +12,6 @@ import java.math.BigDecimal; import java.util.List; -import java.util.Map; @RestController @RequiredArgsConstructor @@ -23,8 +22,9 @@ public class LocationController { @ApiOperation(value = "위도, 경도를 주소로 바꾸는 함수") @GetMapping("/convert/geo") - public LocationPartDto.Address getAddressByGeo(@RequestParam BigDecimal lat, @RequestParam BigDecimal lng) { - return locationService.convertGeoToAddressRT(String.valueOf(lat), String.valueOf(lng)); + public LocationPartDto.Address getAddressByGeo(@RequestParam BigDecimal lat, + @RequestParam BigDecimal lng) { + return locationService.convertGeoToAddress(String.valueOf(lat), String.valueOf(lng)); } @ApiOperation(value = "주소를 위도, 경도로 바꾸는 함수") @@ -35,13 +35,17 @@ public LocationPartDto.LatLong getGeoByAddress(@RequestParam String address) { @ApiOperation(value = "위도, 경도, 업장명을 통해 업장 정보를 받아오는 함수") @GetMapping("/convert/store-name") - public LocationInfoDto.LocationResponse getKeywordByGeoAndStoreName(@RequestParam BigDecimal lat, @RequestParam BigDecimal lng, String storeName) { + public LocationInfoDto.LocationResponse getKeywordByGeoAndStoreName(@RequestParam BigDecimal lat, + @RequestParam BigDecimal lng, + String storeName) { return locationService.convertGeoAndStoreNameToKeyword(String.valueOf(lat), String.valueOf(lng), storeName); } @ApiOperation(value = "위도, 경도, 주소를 통해 후보 업장 정보 리스트를 받아오는 함수") @GetMapping("/convert/geo-address") - public List getKeywordByGeoAndAddress(@RequestParam BigDecimal lat, @RequestParam BigDecimal lng, @RequestParam String address) { + public List getKeywordByGeoAndAddress(@RequestParam BigDecimal lat, + @RequestParam BigDecimal lng, + @RequestParam String address) { return locationService.convertGeoAndAddressToKeyword(String.valueOf(lat), String.valueOf(lng), address); } } \ No newline at end of file diff --git a/src/main/java/com/mpnp/baechelin/api/service/PublicApiService.java b/src/main/java/com/mpnp/baechelin/api/service/PublicApiService.java index c80d2ee..98eaf25 100644 --- a/src/main/java/com/mpnp/baechelin/api/service/PublicApiService.java +++ b/src/main/java/com/mpnp/baechelin/api/service/PublicApiService.java @@ -2,7 +2,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.mpnp.baechelin.api.dto.*; -import com.mpnp.baechelin.api.model.LocationKeywordSearchForm; import com.mpnp.baechelin.api.model.PublicApiV1Form; import com.mpnp.baechelin.exception.CustomException; import com.mpnp.baechelin.exception.ErrorCode; @@ -32,49 +31,8 @@ public class PublicApiService { private final StoreRepository storeRepository; private final LocationService locationService; - private final StoreService storeService; @Value("${public.api.v1.key}") private String publicV1Key; - /*private final HttpConfig httpConfig; - - public PublicApiResponseDto processApiToDBWithWebclientMono(PublicApiRequestDto publicApiRequestDto) throws UnsupportedEncodingException { - WebClient client = WebClient.builder() - .baseUrl("http://openapi.seoul.go.kr:8088") -// .defaultCookie("cookieKey", "cookieValue") - .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE) - .defaultUriVariables(Collections.singletonMap("url", "http://openapi.seoul.go.kr:8088")) - .clientConnector(new ReactorClientHttpConnector(httpConfig.httpClient())) // 위의 타임아웃 적용 - .build(); - - String key = URLEncoder.encode(publicV1Key, "UTF-8"); *//*인증키 (sample사용시에는 호출시 제한됩니다.)*//* - String type = URLEncoder.encode(publicApiRequestDto.getType(), "UTF-8"); *//*요청파일타입 (xml,xmlf,xls,json) *//* - String service = URLEncoder.encode(publicApiRequestDto.getService(), "UTF-8"); *//*서비스명 (대소문자 구분 필수입니다.)*//* - String start = URLEncoder.encode(String.valueOf(publicApiRequestDto.getStartIndex()), "UTF-8"); *//*요청시작위치 (sample인증키 사용시 5이내 숫자)*//* - String end = URLEncoder.encode(String.valueOf(publicApiRequestDto.getEndIndex()), "UTF-8"); *//*요청종료위치(sample인증키 사용시 5이상 숫자 선택 안 됨)*//* - - PublicApiResponseDto result = client.get().uri( - uriBuilder -> uriBuilder.pathSegment(key, type, service, start, end).path("/") - .build()) - .accept(MediaType.APPLICATION_JSON) - .retrieve() - .onStatus(HttpStatus::is4xxClientError, response -> { - throw new IllegalAccessError("400"); - }) - .onStatus(HttpStatus::is5xxServerError, response -> { - throw new IllegalAccessError("500"); - }) - .bodyToMono(PublicApiResponseDto.class).flux() - .toStream() - .findFirst() - .orElse(null); - if (result == null) { - return null; - } - setInfos(result); - saveDTO(result.getTouristFoodInfo().getRow()); - return result; - - }*/ /** * @param publicApiRequestDto Controller에서 받은 DTO(key 등이 포함됨) @@ -105,21 +63,20 @@ public void processApiV1(PublicApiRequestDto publicApiRequestDto) { /** * @param publicApiV1Form API 호출 결과 - * */ private void setInfos(PublicApiV1Form publicApiV1Form) { publicApiV1Form.getTouristFoodInfo().getRow().forEach(row -> { - try { - if (!setRowLngLat(row)) return; // 주소를 가지고 위/경도를 찾는다 - } catch (JsonProcessingException e) { - throw new CustomException(ErrorCode.API_LOAD_FAILURE); + try { + if (!setRowLngLat(row)) return; // 주소를 가지고 위/경도를 찾는다 + } catch (JsonProcessingException e) { + throw new CustomException(ErrorCode.API_LOAD_FAILURE); + } + try { + setRowCategoryAndId(row); // 위/경도/매장명을 가지고 키워드 설정 + } catch (JsonProcessingException e) { + throw new CustomException(ErrorCode.API_LOAD_FAILURE); + } } - try { - setRowCategoryAndId(row); // 위/경도/매장명을 가지고 키워드 설정 - } catch (JsonProcessingException e) { - throw new CustomException(ErrorCode.API_LOAD_FAILURE); - } - } ); } @@ -130,15 +87,10 @@ private void setInfos(PublicApiV1Form publicApiV1Form) { * @throws JsonProcessingException JSON 파싱, 매핑 오류시 발생하는 Exception */ private boolean setRowLngLat(PublicApiV1Form.Row row) throws JsonProcessingException { - LocationKeywordSearchForm latLngSearchForm = locationService.getLatLngByAddressRT(row.getADDR()); -// LocationKeywordSearchForm latLngSearchForm = locationService.giveLatLngByAddress(row.getADDR()); - if (latLngSearchForm == null) return false; - LocationKeywordSearchForm.Documents latLngDoc = Arrays.stream(latLngSearchForm.getDocuments()).findFirst().orElse(null); - if (latLngDoc == null) - return false; - row.setLatitude(Decimal.valueOf(latLngDoc.getY())); - row.setLongitude(Decimal.valueOf(latLngDoc.getX())); - row.setCategory(categoryFilter(Optional.of(latLngDoc.getCategory_name()).orElse("기타"))); + LocationPartDto.LatLong latLong = locationService.convertAddressToGeo(row.getADDR()); + if (latLong == null || !latLong.validate()) return false; + row.setLatitude(Decimal.valueOf(latLong.getLatitude())); + row.setLongitude(Decimal.valueOf(latLong.getLongitude())); return true; } @@ -147,15 +99,13 @@ private boolean setRowLngLat(PublicApiV1Form.Row row) throws JsonProcessingExcep * @throws JsonProcessingException JSON 파싱, 매핑 오류시 발생하는 Exception */ private void setRowCategoryAndId(PublicApiV1Form.Row row) throws JsonProcessingException { - LocationKeywordSearchForm categorySearchForm = locationService - .getCategoryByLatLngKeywordRT(String.valueOf(row.getLatitude()), String.valueOf(row.getLongitude()), row.getSISULNAME()); -// LocationKeywordSearchForm categorySearchForm = locationService.giveCategoryByLatLngKeyword(row.getLatitude(), row.getLongitude(), row.getSISULNAME()); - LocationKeywordSearchForm.Documents categoryDoc = Arrays.stream(categorySearchForm.getDocuments()).findFirst().orElse(null); - if (categoryDoc == null) + LocationInfoDto.LocationResponse locationResponse = locationService + .convertGeoAndStoreNameToKeyword(String.valueOf(row.getLatitude()), String.valueOf(row.getLongitude()), row.getSISULNAME()); + if (locationResponse == null) return; // 결과가 비어있으면 진행하지 않는다 - row.setStoreId(Integer.parseInt(categoryDoc.getId())); - row.setSISULNAME(categoryDoc.getPlace_name()); - row.setCategory(categoryFilter(Optional.of(categoryDoc.getCategory_name()).orElse(null))); + row.setStoreId(locationResponse.getStoreId()); + row.setSISULNAME(locationResponse.getStoreName()); + row.setCategory(categoryFilter(Optional.of(locationResponse.getCategory()).orElse(null))); } /** diff --git a/src/main/java/com/mpnp/baechelin/api/service/PublicApiServiceV2.java b/src/main/java/com/mpnp/baechelin/api/service/PublicApiServiceV2.java index 9ecf4d0..0898f7d 100644 --- a/src/main/java/com/mpnp/baechelin/api/service/PublicApiServiceV2.java +++ b/src/main/java/com/mpnp/baechelin/api/service/PublicApiServiceV2.java @@ -4,12 +4,12 @@ import com.mpnp.baechelin.api.model.PublicApiCategoryForm; import com.mpnp.baechelin.api.model.PublicApiV2Form; import com.mpnp.baechelin.common.DataClarification; +import com.mpnp.baechelin.config.AppConfig; import com.mpnp.baechelin.store.domain.Store; import com.mpnp.baechelin.api.dto.LocationInfoDto; import com.mpnp.baechelin.store.repository.StoreRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.aspectj.lang.annotation.Before; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.*; @@ -21,9 +21,7 @@ import javax.transaction.Transactional; import java.io.*; import java.net.URI; -import java.net.URL; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; import java.util.*; import java.util.stream.Collectors; @@ -42,16 +40,6 @@ public class PublicApiServiceV2 { private String publicV2Key; - /** - * @param totalDataCount 시 & 구에 해당하는 데이터 개수 - * @param nowPage 현재 페이지 - * @return 다음 페이지가 존재하는지 - 페이지는 1000으로 고정 - */ - private boolean hasNextPage(int totalDataCount, int nowPage) { - if (totalDataCount <= 1000) return false; - return totalDataCount > nowPage * 1000; - } - /** * @return 헤더 세팅 - V2에서는 공통으로 XML 사용 */