-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Seoul-Public-Data/feat/3
feat: 500m 내의 heatShelter 정보 반환 api 추가, DistanceUtils 추가
- Loading branch information
Showing
14 changed files
with
226 additions
and
33 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/seouldata/seoul_backend/common/DistanceUtils.java
This file contains 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,27 @@ | ||
package seouldata.seoul_backend.common; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class DistanceUtils { | ||
|
||
/** | ||
* 하버사인 공식을 이용하여 두 지점간의 거리를 계산 | ||
*/ | ||
public static double calculateDistance(double userLon, double userLat, double lon, double lat) { | ||
|
||
double earthRadius = 6371000; // 지구 반지름(미터) | ||
|
||
double dLat = Math.toRadians(lat - userLat); | ||
double dLon = Math.toRadians(lon - userLon); | ||
|
||
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) | ||
+ Math.cos(Math.toRadians(userLat)) * Math.cos(Math.toRadians(lat)) | ||
* Math.sin(dLon / 2) * Math.sin(dLon / 2); | ||
|
||
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | ||
return earthRadius * c; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../application/dto/request/CctvRequest.java → .../application/dto/request/CctvRequest.java
This file contains 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
2 changes: 1 addition & 1 deletion
2
...pplication/dto/response/CctvResponse.java → ...pplication/dto/response/CctvResponse.java
This file contains 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
2 changes: 1 addition & 1 deletion
2
...ul_backend/cctv/domain/entity/AnCctv.java → ...end/domain/cctv/domain/entity/AnCctv.java
This file contains 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
4 changes: 2 additions & 2 deletions
4
...v/domain/repository/AnCctvRepository.java → ...v/domain/repository/AnCctvRepository.java
This file contains 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 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
8 changes: 4 additions & 4 deletions
8
...d/cctv/presentation/AnCctvController.java → ...n/cctv/presentation/AnCctvController.java
This file contains 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
21 changes: 21 additions & 0 deletions
21
...ouldata/seoul_backend/domain/heatshelter/appplication/dto/request/HeatShelterRequest.java
This file contains 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,21 @@ | ||
package seouldata.seoul_backend.domain.heatshelter.appplication.dto.request; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
public class HeatShelterRequest { | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public static class HeatShelterNearRequest { | ||
private double userLongitude; | ||
private double userLatitude; | ||
|
||
@Builder | ||
public HeatShelterNearRequest(double userLongitude, double userLatitude) { | ||
this.userLongitude = userLongitude; | ||
this.userLatitude = userLatitude; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ldata/seoul_backend/domain/heatshelter/appplication/dto/response/HeatShelterResponse.java
This file contains 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,27 @@ | ||
package seouldata.seoul_backend.domain.heatshelter.appplication.dto.response; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Id; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
public class HeatShelterResponse { | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public static class HeatShelterNearResponse { | ||
private String name; | ||
private String address; | ||
private double longitude; | ||
private double latitude; | ||
|
||
@Builder | ||
public HeatShelterNearResponse(String name, String address, double longitude, double latitude) { | ||
this.name = name; | ||
this.address = address; | ||
this.longitude = longitude; | ||
this.latitude = latitude; | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...eouldata/seoul_backend/domain/heatshelter/appplication/service/HeatShelterGetService.java
This file contains 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,46 @@ | ||
package seouldata.seoul_backend.domain.heatshelter.appplication.service; | ||
|
||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import seouldata.seoul_backend.common.DistanceUtils; | ||
import seouldata.seoul_backend.domain.heatshelter.domain.entity.HeatShelter; | ||
import seouldata.seoul_backend.domain.heatshelter.domain.service.HeatShelterQueryService; | ||
import seouldata.seoul_backend.domain.heatshelter.appplication.dto.request.HeatShelterRequest; | ||
import seouldata.seoul_backend.domain.heatshelter.appplication.dto.response.HeatShelterResponse; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class HeatShelterGetService { | ||
|
||
private final HeatShelterQueryService heatShelterQueryService; | ||
|
||
public List<HeatShelterResponse.HeatShelterNearResponse> getHeatShelterNear(HeatShelterRequest.HeatShelterNearRequest heatShelterNearRequest) { | ||
|
||
List<HeatShelter> heatShelters = heatShelterQueryService.findAll(); | ||
double userLatitude = heatShelterNearRequest.getUserLatitude(); | ||
double userLongitude = heatShelterNearRequest.getUserLongitude(); | ||
System.out.println("userLatitude: " + userLatitude); | ||
|
||
List<HeatShelterResponse.HeatShelterNearResponse> heatShelterNearList = new ArrayList<>(); | ||
for (HeatShelter heatShelter : heatShelters) { | ||
double longitude = heatShelter.getLongitude(); | ||
double latitude = heatShelter.getLatitude(); | ||
double distance = DistanceUtils.calculateDistance(userLongitude, userLatitude, longitude, latitude); | ||
if (distance < 500) { | ||
HeatShelterResponse.HeatShelterNearResponse heatShelterNearResponse = HeatShelterResponse.HeatShelterNearResponse.builder() | ||
.name(heatShelter.getName()) | ||
.address(heatShelter.getAddress()) | ||
.longitude(heatShelter.getLongitude()) | ||
.latitude(heatShelter.getLatitude()) | ||
.build(); | ||
heatShelterNearList.add(heatShelterNearResponse); | ||
} | ||
} | ||
return heatShelterNearList; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/seouldata/seoul_backend/domain/heatshelter/domain/entity/HeatShelter.java
This file contains 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,36 @@ | ||
package seouldata.seoul_backend.domain.heatshelter.domain.entity; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Table(name = "an_heat_shelter") | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class HeatShelter { | ||
|
||
@Id @Column(name = "shelter_name") | ||
private String name; | ||
|
||
private String address; | ||
|
||
@Column(name = "lon") | ||
private double longitude; | ||
|
||
@Column(name = "lat") | ||
private double latitude; | ||
|
||
@Builder | ||
public HeatShelter(String name, String address, double longitude, double latitude) { | ||
this.name = name; | ||
this.address = address; | ||
this.longitude = longitude; | ||
this.latitude = latitude; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...a/seouldata/seoul_backend/domain/heatshelter/domain/repository/HeatShelterRepository.java
This file contains 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,7 @@ | ||
package seouldata.seoul_backend.domain.heatshelter.domain.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import seouldata.seoul_backend.domain.heatshelter.domain.entity.HeatShelter; | ||
|
||
public interface HeatShelterRepository extends JpaRepository<HeatShelter, String> { | ||
} |
21 changes: 21 additions & 0 deletions
21
...va/seouldata/seoul_backend/domain/heatshelter/domain/service/HeatShelterQueryService.java
This file contains 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,21 @@ | ||
package seouldata.seoul_backend.domain.heatshelter.domain.service; | ||
|
||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import seouldata.seoul_backend.domain.heatshelter.domain.entity.HeatShelter; | ||
import seouldata.seoul_backend.domain.heatshelter.domain.repository.HeatShelterRepository; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class HeatShelterQueryService { | ||
|
||
private final HeatShelterRepository heatShelterRepository; | ||
|
||
public List<HeatShelter> findAll() { | ||
return heatShelterRepository.findAll(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...n/java/seouldata/seoul_backend/domain/heatshelter/presentation/HeatShelterController.java
This file contains 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,25 @@ | ||
package seouldata.seoul_backend.domain.heatshelter.presentation; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import seouldata.seoul_backend.domain.heatshelter.appplication.dto.request.HeatShelterRequest; | ||
import seouldata.seoul_backend.domain.heatshelter.appplication.dto.response.HeatShelterResponse; | ||
import seouldata.seoul_backend.domain.heatshelter.appplication.service.HeatShelterGetService; | ||
|
||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class HeatShelterController { | ||
|
||
private final HeatShelterGetService heatShelterGetService; | ||
|
||
@GetMapping("/api/heatshelter") | ||
public List<HeatShelterResponse.HeatShelterNearResponse> getHeatShelterNear(@RequestBody HeatShelterRequest.HeatShelterNearRequest heatShelterNearRequest) { | ||
return heatShelterGetService.getHeatShelterNear(heatShelterNearRequest); | ||
} | ||
|
||
} |