Skip to content

Commit

Permalink
Merge pull request #17 from 9oormthon-univ/hyundong
Browse files Browse the repository at this point in the history
fix :: 근처 관공서 빈 배열 반환 수정
  • Loading branch information
hyundong-L authored Nov 21, 2024
2 parents ba4f6da + 4b6ea52 commit 5efb2fa
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

@Entity
@NoArgsConstructor
@Getter
@ToString
public class PublicOffice {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
import com.danpoong.onchung.domain.public_office.dto.FindAroundPublicOfficeResponse;
import com.danpoong.onchung.domain.public_office.repository.PublicOfficeRepository;
import com.danpoong.onchung.global.map.api.KakaoMap;
import com.danpoong.onchung.global.map.response.AddressApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Slf4j
public class PublicOfficeService {
private final PublicOfficeRepository publicOfficeRepository;
private final KakaoMap kakaoMap;
Expand All @@ -25,9 +26,11 @@ public List<FindAroundPublicOfficeResponse> findAroundPublicOffice(FindAroundPub
// 맵 중앙 좌표를 통해 1차 필터링을 위한 도, 시 정보 확보
String[] middleCoordinate = request.middleCoordinate();
String[] userStateAndCity = getStateAndCityFromCoordinate(middleCoordinate[0], middleCoordinate[1]);
log.info(Arrays.toString(userStateAndCity));

// 1차 필터링
List<PublicOffice> publicOffices = firstFilteringPublicOffice(userStateAndCity[0], userStateAndCity[1]);
log.info(publicOffices.toString());

// 범위 안의 관공서만 반환
return publicOffices.stream()
Expand All @@ -40,26 +43,26 @@ public List<FindAroundPublicOfficeResponse> findAroundPublicOffice(FindAroundPub
}

// 관공서 주소를 정할 때 사용할 예정 - 데이터 전처리 용도
@Transactional
public void saveAddress() {
List<PublicOffice> publicOffices = publicOfficeRepository.findAll();

publicOffices.forEach(publicOffice -> {
AddressApiResponse addressApiResponse = kakaoMap.getAddress(publicOffice.getName());

if (addressApiResponse == null || addressApiResponse.getDocuments().isEmpty()) {
return;
}

Optional<AddressApiResponse.Document> targetDocument = addressApiResponse.getDocuments()
.stream()
.findFirst();

targetDocument.ifPresent(document ->
publicOffice.updateInfo(document.getRoadAddress(), document.getX(), document.getY(), document.getPhone())
);
});
}
// @Transactional
// public void saveAddress() {
// List<PublicOffice> publicOffices = publicOfficeRepository.findAll();
//
// publicOffices.forEach(publicOffice -> {
// AddressApiResponse addressApiResponse = kakaoMap.getAddress(publicOffice.getName());
//
// if (addressApiResponse == null || addressApiResponse.getDocuments().isEmpty()) {
// return;
// }
//
// Optional<AddressApiResponse.Document> targetDocument = addressApiResponse.getDocuments()
// .stream()
// .findFirst();
//
// targetDocument.ifPresent(document ->
// publicOffice.updateInfo(document.getRoadAddress(), document.getX(), document.getY(), document.getPhone())
// );
// });
// }



Expand All @@ -68,13 +71,20 @@ private String[] getStateAndCityFromCoordinate(String longitude, String latitude
}

private List<PublicOffice> firstFilteringPublicOffice(String state, String city) {
List<PublicOffice> publicOffices = publicOfficeRepository.findAll();
log.info(publicOffices.toString());

return publicOfficeRepository.findAll()
.stream()
.filter(location -> {
boolean stateCheck = location.getState().equals(state);
boolean cityCheck = city == null || location.getCity().equals(city);

return stateCheck && cityCheck;
if (location.getCity().isEmpty()) {
return location.getState().contains(state);
} else {
boolean stateCheck = location.getState().contains(state);
boolean cityCheck = city.isEmpty() || location.getCity().contains(city);

return stateCheck && cityCheck;
}
}).toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;

@Getter
@JsonIgnoreProperties(ignoreUnknown = true)
@Slf4j
public class AdminRegionResponse {
@JsonProperty("documents")
private List<KakaoDocument> documents;
Expand All @@ -25,11 +28,13 @@ public String[] getStateAndCityName() {

if (kakaoDocument != null && kakaoDocument.addressName != null) {
String[] splitAddress = kakaoDocument.getAddressName().split(" ");
log.info(Arrays.toString(splitAddress));


if (splitAddress[1].endsWith("시")) {
return new String[]{splitAddress[0], null};
} else {
return new String[]{splitAddress[0], splitAddress[1]};
} else {
return new String[]{splitAddress[0], ""};
}
}

Expand Down

0 comments on commit 5efb2fa

Please sign in to comment.