Skip to content

Commit

Permalink
fix: 위치 조회 API 수정 ##33
Browse files Browse the repository at this point in the history
  • Loading branch information
yongseok-dev committed May 22, 2023
1 parent 589d524 commit 08fdcd7
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,43 +1,48 @@
package app.seok.picnicmap.geolocation;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Setter
@Getter
public class GeoLocationResponseDTO {
private Integer status;
private String massage;
private Position data;

@Setter
@Getter
public static class Position {
private double lng;
private double lat;
}
private Integer status;
private String massage;
private Position data;
@JsonIgnore
private String returnMessage;

@Setter
@Getter
public static class Position {

private double lng;
private double lat;
}

public static GeoLocationResponseDTO OkFromGeoLocation(GeoLocation dbResult) {
double lng = dbResult.getLng();
double lat = dbResult.getLat();
GeoLocationResponseDTO geoLocationResponse = new GeoLocationResponseDTO();
geoLocationResponse.setStatus(200);
geoLocationResponse.setMassage("Success " + dbResult.getR2());
Position position = new Position();
position.setLng(lng);
position.setLat(lat);
geoLocationResponse.setData(position);
return geoLocationResponse;
}

public static GeoLocationResponseDTO OkFromGeoLocation(GeoLocation dbResult) {
double lng = dbResult.getLng();
double lat = dbResult.getLat();
GeoLocationResponseDTO geoLocationResponse = new GeoLocationResponseDTO();
geoLocationResponse.setStatus(200);
geoLocationResponse.setMassage("Success");
Position position = new Position();
position.setLng(lng);
position.setLat(lat);
geoLocationResponse.setData(position);
return geoLocationResponse;
}
public static GeoLocationResponseDTO Ok() {
GeoLocationResponseDTO geoLocationResponse = new GeoLocationResponseDTO();
geoLocationResponse.setStatus(200);
Position position = new Position();
position.setLng(126.977380);
position.setLat(37.575843);
geoLocationResponse.setData(position);
return geoLocationResponse;
}
public static GeoLocationResponseDTO Ok() {
GeoLocationResponseDTO geoLocationResponse = new GeoLocationResponseDTO();
geoLocationResponse.setMassage("위치를 확인해 주세요.");
geoLocationResponse.setStatus(200);
Position position = new Position();
position.setLng(126.977380);
position.setLat(37.575843);
geoLocationResponse.setData(position);
return geoLocationResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,51 @@
import app.seok.picnicmap.api.naver.NcloudApiExplorer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class GeoLocationService {
private final NcloudApiExplorer ncloudApiExplorer;
private final GeoLocationRepository geoLocationRepository;

public GeoLocationResponseDTO getGeoLocation(String ipAddress) {
//DB에서 조회 최근 1시간 이내 조회 했던 IP는 해당 값으로 출력
LocalDateTime startTime = LocalDateTime.now().minusHours(1);
Optional<GeoLocation> optionalGeoLocation = geoLocationRepository.findRecentRecordByIpAddress(ipAddress, startTime);

GeoLocation geoLocation;
if (optionalGeoLocation.isPresent()) {
geoLocation = optionalGeoLocation.get();
} else {
try {
geoLocation = callNcloudApiGeolocation(ipAddress);
geoLocationRepository.save(geoLocation);
} catch (JsonProcessingException e) {
geoLocation = new GeoLocation();
throw new RuntimeException(e);
}
}

if ("서울특별시".equals(geoLocation.getR1())) {
return GeoLocationResponseDTO.OkFromGeoLocation(geoLocation);
}
return GeoLocationResponseDTO.Ok();

private final NcloudApiExplorer ncloudApiExplorer;
private final GeoLocationRepository geoLocationRepository;

public GeoLocationResponseDTO getGeoLocation(String ipAddress) {
//DB에서 조회 최근 1시간 이내 조회 했던 IP는 해당 값으로 출력
LocalDateTime startTime = LocalDateTime.now().minusHours(1);
Optional<GeoLocation> optionalGeoLocation = geoLocationRepository.findRecentRecordByIpAddress(
ipAddress, startTime);

GeoLocation geoLocation;
if (optionalGeoLocation.isPresent()) {
geoLocation = optionalGeoLocation.get();
} else {
try {
geoLocation = callNcloudApiGeolocation(ipAddress);
geoLocationRepository.save(geoLocation);
} catch (JsonProcessingException e) {
geoLocation = new GeoLocation();
throw new RuntimeException(e);
}
}

private GeoLocation callNcloudApiGeolocation(String ipAddress) throws JsonProcessingException {
String jsonString = String.valueOf(ncloudApiExplorer.getGeolocation(ipAddress));
ObjectMapper objectMapper = new ObjectMapper();
jsonString = jsonString.replace("\"long\":", "\"lng\":");
GeoLocationDTO responseDTO = objectMapper.readValue(jsonString, GeoLocationDTO.class);
GeoLocation geoLocation = responseDTO.getGeoLocation();
geoLocation.setIpAddress(ipAddress);
return geoLocation;
if ("서울특별시".equals(geoLocation.getR1())) {
return GeoLocationResponseDTO.OkFromGeoLocation(geoLocation);
}

return GeoLocationResponseDTO.Ok();
}

private GeoLocation callNcloudApiGeolocation(String ipAddress) throws JsonProcessingException {
String jsonString = String.valueOf(ncloudApiExplorer.getGeolocation(ipAddress));
ObjectMapper objectMapper = new ObjectMapper();
jsonString = jsonString.replace("\"long\":", "\"lng\":");
GeoLocationDTO responseDTO = objectMapper.readValue(jsonString, GeoLocationDTO.class);
GeoLocation geoLocation = responseDTO.getGeoLocation();
geoLocation.setIpAddress(ipAddress);
return geoLocation;
}
}

0 comments on commit 08fdcd7

Please sign in to comment.