-
Notifications
You must be signed in to change notification settings - Fork 2
[#15] 지역 인증 기능, 주변 지역 찾기 기능 구현 #19
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
882dda1
[#15] 지역 인증 기능 구현 - api 테스트 코드
tilsong fa643dc
Merge branch 'main' into feature/15
tilsong 4748030
[#15] 지역 인증 기능 구현 - 외부 api 테스트
tilsong 6df6b53
[#15] 지역 인증 기능 구현 - 도메인 및 레포지토리 생성
tilsong 7b36e27
[#15] 지역 인증 기능 구현 - 도메인 및 레포지토리 수정
tilsong 3ec6edc
[#15] 지역 인증 기능 구현 - 외부 api를 이용을 위한 api 전송 클래스 및 토큰 획득 클래스 생성
tilsong 69c609d
[#15] 지역 인증 기능 구현 - 외부 api를 이용한 지역명(주소) 획득 인터페이스 및 구현체 생성
tilsong fea044c
[#15] 지역 인증 기능 구현 - Controller, Service 및 요청, 응답 객체 생성
tilsong 0a055e5
[#15] 지역 인증 기능 구현 - 외부 api 테스트 추가
tilsong 7acb26e
[#15] 지역 인증 기능 구현 - 지역 좌표 csv 파일 업로드 및 가공 테스트
tilsong ad7d7e5
[#15] 지역 인증 기능 구현 - Region 도메인 변경 및 애플리케이션 시작 시 지역 데이터 저장, 지리 관련 의존성 추가
tilsong 3c14496
[#15] 지역 인증 기능 구현 - UserRegion 도메인 추가 및 그에 따른 로직 변경
tilsong 3d8a126
[#15] 인증된 지역의 주변 지역 목록 찾기 기능 구현 - 좌표 파일의 좌표 단위 변환
tilsong a298196
[#15] 인증된 지역의 주변 지역 목록 찾기 기능 구현 - Region의 id 타입 변경(String -> Long) 및 …
tilsong af0ce61
[#15] 인증된 지역의 주변 지역 목록 찾기 기능 구현 - Controller, Service, Repository
tilsong 88d77c4
[#15] 인증된 지역의 주변 지역 목록 찾기 기능 구현 - 외부 api로 인한 예외 추가 및 spring-retry 의존성 추가
tilsong 5cba290
[#15] 인증된 지역의 주변 지역 목록 찾기 기능 구현 - 외부 api 및 spring-retry 관련 코드 리팩토링
tilsong 715d2a0
[#15] 인증된 지역의 주변 지역 목록 찾기 기능 구현 - 피드백 반영 및 코드 수정
tilsong e21ee7a
[#15] 인증된 지역의 주변 지역 목록 찾기 기능 구현 - 피드백 반영 및 코드 수정2, 실제 애플리케이션에 반영되지 않는…
tilsong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/java/com/directors/application/region/RegionService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package com.directors.application.region; | ||
|
|
||
| import com.directors.domain.region.Region; | ||
| import com.directors.domain.region.RegionRepository; | ||
| import com.directors.domain.user.UserRegion; | ||
| import com.directors.domain.user.UserRegionRepository; | ||
| import jakarta.annotation.PostConstruct; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.locationtech.jts.geom.Coordinate; | ||
| import org.locationtech.jts.geom.GeometryFactory; | ||
| import org.locationtech.jts.geom.Point; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.InputStream; | ||
| import java.io.InputStreamReader; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class RegionService { | ||
|
|
||
| private final RegionRepository regionRepository; | ||
| private final UserRegionRepository userRegionRepository; | ||
|
|
||
| @PostConstruct | ||
| private void loadRegionData() { | ||
| String pathPrefix = "/regionCSV/"; | ||
| String pathSuffix = "_좌표.csv"; | ||
| String[] regions = { | ||
| "강원", "경기", "경남", "경북", "광주", "대구", "대전", "부산", "서울", "세종", "울산", "인천", "전남", "전북", "제주", "충남", "충북" | ||
| }; | ||
|
|
||
| for (int i = 0; i < regions.length; i++) { | ||
| List<String> regionDataLines = null; | ||
|
|
||
| var inputStream = getClass().getResourceAsStream(pathPrefix + regions[i] + pathSuffix); | ||
| var reader = new BufferedReader(new InputStreamReader(inputStream)); | ||
| regionDataLines = reader.lines().collect(Collectors.toList()); | ||
|
|
||
| var collect = regionDataLines.stream() | ||
| .map(this::regionDataLineToRegion) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| regionRepository.saveAll(collect); | ||
| } | ||
| } | ||
|
|
||
| @Transactional | ||
| public List<String> getNearestAddress(String userId, int distance) { | ||
| var userRegion = userRegionRepository | ||
| .findByUserId(userId) | ||
| .orElseThrow(() -> new RuntimeException()); // TODO: 04.05 먼저 지역 인증이 필요하다는 예외가 필요. | ||
| var region = regionRepository.findByRegionId(userRegion.getRegionId()).orElseThrow(); | ||
|
|
||
| return getNearestRegion(region, distance) | ||
| .stream() | ||
| .map(reg -> reg.getAddress().unitAddress()) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private Region regionDataLineToRegion(String regionLine) { | ||
| String[] lineSplit = regionLine.split(","); | ||
| return Region.of(lineSplit[0], lineSplit[1], coordinateToPoint(lineSplit[2], lineSplit[3])); | ||
| } | ||
|
|
||
| private Point coordinateToPoint(String longitude, String latitude) { | ||
| double lon = Double.parseDouble(longitude); | ||
| double lat = Double.parseDouble(latitude); | ||
| return new GeometryFactory().createPoint(new Coordinate(lon, lat)); | ||
| } | ||
|
|
||
| private List<Region> getNearestRegion(Region region, int distance) { | ||
| List<Region> regionWithin = regionRepository.findRegionWithin(region, distance * 1000); | ||
| return regionWithin; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/com/directors/application/user/AuthenticateRegionService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package com.directors.application.user; | ||
|
|
||
| import com.directors.domain.region.Address; | ||
| import com.directors.domain.region.Region; | ||
| import com.directors.domain.region.RegionApiClient; | ||
| import com.directors.domain.region.RegionRepository; | ||
| import com.directors.domain.user.UserRegion; | ||
| import com.directors.domain.user.UserRegionRepository; | ||
| import com.directors.presentation.user.request.AuthenticateRegionRequest; | ||
| import com.directors.presentation.user.response.AuthenticateRegionResponse; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.NoSuchElementException; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class AuthenticateRegionService { | ||
| private final RegionApiClient regionApiClient; | ||
| private final RegionRepository regionRepository; | ||
| private final UserRegionRepository userRegionRepository; | ||
|
|
||
| @Transactional | ||
| public AuthenticateRegionResponse authenticate(AuthenticateRegionRequest request, String userId) { | ||
| var addressByApi = regionApiClient.findRegionAddressByLocation(request.latitude(), request.longitude()); | ||
| var region = regionRepository.findByFullAddress(addressByApi.fullAddress()) | ||
| .orElseThrow(() -> new NoSuchElementException()); | ||
|
|
||
| var userAddress = updateUserRegionAddress(userId, region); | ||
|
|
||
| return new AuthenticateRegionResponse(userAddress.fullAddress(), userAddress.unitAddress()); | ||
| } | ||
|
|
||
| private Address updateUserRegionAddress(String userId, Region region) { | ||
| if (userRegionRepository.existsByUserId(userId)) { | ||
| return updateExistUserRegion(userId, region).getAddress(); | ||
| } | ||
| return saveUserRegion(userId, region).getAddress(); | ||
| } | ||
|
|
||
| private UserRegion saveUserRegion(String userId, Region region) { | ||
| UserRegion savedUserRegion; | ||
| var newUserRegion = UserRegion.of(region.getAddress(), userId, region.getId()); | ||
|
|
||
| savedUserRegion = userRegionRepository.save(newUserRegion); | ||
| return savedUserRegion; | ||
| } | ||
|
|
||
| private UserRegion updateExistUserRegion(String userId, Region region) { | ||
| var userRegion = userRegionRepository.findByUserId(userId).get(); | ||
|
|
||
| userRegion.setAddress(region.getAddress()); | ||
| userRegion.setRegionId(region.getId()); | ||
|
|
||
| return userRegionRepository.save(userRegion); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.directors.domain.region; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| public record Address ( | ||
| String fullAddress, | ||
| String unitAddress | ||
| ){ | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.directors.domain.region; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import org.locationtech.jts.geom.Point; | ||
|
|
||
| @AllArgsConstructor | ||
| @Getter | ||
| @Builder | ||
| public class Region { | ||
| private Long id; | ||
|
|
||
| private Address address; | ||
|
|
||
| private Point point; | ||
|
|
||
| public void setId(Long id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public static Region of(String fullAddress, String unitAddress, Point point) { | ||
| return Region.builder() | ||
| .address(new Address(fullAddress, unitAddress)) | ||
| .point(point) | ||
| .build(); | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/directors/domain/region/RegionApiClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package com.directors.domain.region; | ||
|
|
||
| public interface RegionApiClient { | ||
| Address findRegionAddressByLocation(double latitude, double longitude); | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/directors/domain/region/RegionRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.directors.domain.region; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| public interface RegionRepository { | ||
| Optional<Region> findByFullAddress(String fullAddress); | ||
|
|
||
| Optional<Region> findByRegionId(Long regionId); | ||
|
|
||
| Region save(Region region); | ||
|
|
||
| void saveAll(List<Region> regions); | ||
|
|
||
| List<Region> findRegionWithin(Region region, double distance); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.directors.domain.user; | ||
|
|
||
| import com.directors.domain.region.Address; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
|
|
||
| @AllArgsConstructor | ||
| @Builder | ||
| @Getter | ||
| public class UserRegion { | ||
| private String id; | ||
| private Address address; | ||
| private String userId; | ||
| private Long regionId; | ||
|
|
||
| public void setId(String id) { | ||
| this.id = id; | ||
| } | ||
| public void setRegionId(Long regionId) { | ||
| this.regionId = regionId; | ||
| } | ||
|
|
||
| public void setAddress(Address address) { | ||
| this.address = address; | ||
| } | ||
|
|
||
| public static UserRegion of(Address address, String userId, Long regionId) { | ||
| return UserRegion.builder() | ||
| .address(address) | ||
| .userId(userId) | ||
| .regionId(regionId) | ||
| .build(); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/directors/domain/user/UserRegionRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.directors.domain.user; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface UserRegionRepository { | ||
| Optional<UserRegion> findByUserId(String userId); | ||
|
|
||
| boolean existsByUserId(String userId); | ||
|
|
||
| UserRegion save(UserRegion userRegion); | ||
| } |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/directors/infrastructure/api/ApiSender.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package com.directors.infrastructure.api; | ||
|
|
||
| import org.springframework.core.ParameterizedTypeReference; | ||
| import org.springframework.http.HttpEntity; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.http.HttpMethod; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.retry.annotation.Backoff; | ||
| import org.springframework.retry.annotation.Retryable; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.client.HttpClientErrorException; | ||
| import org.springframework.web.client.HttpServerErrorException; | ||
| import org.springframework.web.client.RestTemplate; | ||
| import org.springframework.web.util.UriComponents; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| @Component | ||
| public class ApiSender { | ||
| @Retryable(retryFor = {HttpClientErrorException.class, HttpServerErrorException.class}, maxAttempts = 3, backoff = @Backoff(delay = 1000)) | ||
| public ResponseEntity<Map<String, Object>> send(HttpMethod method, UriComponents uri) throws HttpClientErrorException, HttpServerErrorException { | ||
| RestTemplate restTemplate = new RestTemplate(); | ||
| HttpHeaders header = new HttpHeaders(); | ||
| HttpEntity<String> entity = new HttpEntity<>(header); | ||
|
|
||
| return restTemplate.exchange(uri.toString(), method, entity, new ParameterizedTypeReference<>() { | ||
| }); | ||
| } | ||
|
Comment on lines
+20
to
+28
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 멘토링 시간에 질문 하셨던게 이 부분이군요. |
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PostConstruct가 붙은 메서드는 생성자와 비슷한 취급이 되어야 하지 않을까요?만약 이 메서드가 생성자였다면, public 메서드와 private 메서드 사이에 위치 시키는게 자연스러울지 궁금합니다. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PostConstruct이 붙은 메서드는 빈 객체가 생성되고, 필드가 초기화된 후, 빈 객체를 초기화하는 기능을 가지므로 생성자와 비슷하게 취급되어야 합니다.
따라서 필드 아래, 메서드 위에 위치시키는 것이 적절할 것 같습니다.