Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 주소를 위경도로 변환하는 Geocode API 연동 및 구현 #42

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ftw.hometerview.place.controller.dto;

import com.ftw.hometerview.place.domain.Company;
import com.ftw.hometerview.place.util.NaverMapGeocodeAPI.GeoPoint;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import lombok.Builder;
Expand All @@ -15,18 +16,14 @@ public static class RegisterCompany {
private String name;
@NotBlank
private String roadAddress;
@NotNull
private Double lat;
@NotNull
private Double lon;

public Company toCompany() {
public Company toCompany(GeoPoint geoPoint) {
return Company.builder()
.name(this.name)
.roadAddress(this.roadAddress) // oo시 oo구 oo동 ~~
.province(this.roadAddress.split(" ")[1])
.lat(this.lat)
.lon(this.lon)
.lat(geoPoint.getLat())
.lon(geoPoint.getLon())
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.ftw.hometerview.place.controller.dto.CompanyDto;
import com.ftw.hometerview.place.domain.Company;
import com.ftw.hometerview.place.repository.company.CompanyRepository;
import com.ftw.hometerview.place.util.NaverMapGeocodeAPI;
import com.ftw.hometerview.place.util.NaverMapGeocodeAPI.GeoPoint;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
Expand All @@ -15,12 +17,16 @@
public class CompanyService {

private final CompanyRepository companyRepository;
private final NaverMapGeocodeAPI naverMapGeocodeAPI;

public void register(CompanyDto.RegisterCompany req) {
if (isDuplicated(req.getName(), req.getRoadAddress())) {
throw new BadRequestException(ResponseType.DATA_DUPLICATED);
}
Company company = req.toCompany();

GeoPoint geoPoint = naverMapGeocodeAPI.convert(req.getRoadAddress());

Company company = req.toCompany(geoPoint);
// TODO: get nearliest station from naver API
// TODO: get station id from db
company.setNearliestStation("");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.ftw.hometerview.place.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.Map;
import lombok.Builder;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

@Log4j2
@Component
public class NaverMapGeocodeAPI {

@Builder
@Getter
public static class GeoPoint {

Double lat;
Double lon;
}

private static final String BASE_URL = "https://naveropenapi.apigw.ntruss.com";
private static final String PATH = "/map-geocode/v2/geocode";
@Value("${naver.api-key-id}")
private String API_KEY_ID;
@Value("${naver.api-key}")
private String API_KEY;

public GeoPoint convert(String address) {
WebClient client = WebClient.builder()
.baseUrl(BASE_URL)
.build();

ObjectMapper objectMapper = new ObjectMapper();

String responseJson = client.get()
.uri(uriBuilder -> uriBuilder
.path(PATH)
.queryParam("query", address)
.build())
.header("X-NCP-APIGW-API-KEY-ID", API_KEY_ID)
.header("X-NCP-APIGW-API-KEY", API_KEY)
.retrieve()
.bodyToMono(String.class)
.block();

Map<String, Object> responseMap = null;
try {
responseMap = objectMapper.readValue(responseJson, Map.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
List<Map<String, String>> addressesMap = (List<Map<String, String>>) responseMap.get(
"addresses");
Map<String, String> addressMap = addressesMap.get(0);
String lon = addressMap.get("x");
String lat = addressMap.get("y");
return GeoPoint.builder()
.lat(Double.parseDouble(lat))
.lon(Double.parseDouble(lon))
.build();
}
}