Skip to content

Commit

Permalink
Merge branch 'feat/disaster_text' into dev-check
Browse files Browse the repository at this point in the history
  • Loading branch information
nohy6630 committed Nov 11, 2023
2 parents e320569 + 60d395e commit e986912
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.numberone.backend.domain.disaster.service.DisasterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Tag(name = "disasters", description = "재난문자 관련 API")
Expand All @@ -18,13 +20,13 @@ public class DisasterController {
@Operation(summary = "유저와 관련된 가장 최근 재난 문자 가져오기", description =
"""
현재 유저의 GPS 정보와 유저가 등록한 지역을 기준으로 가장 최근 재난 문자에 대한 정보를 가져옵니다.
유저의 GPS 정보는 api 요청시에 위도(latitude), 경도(longitude)를 body에 담아 전달해주세요.
유저가 등록한 지역은 유저가 인증을 위해 같이 보내야하는 jwt 토큰으로부터 알아서 추출해서 처리할 것입니다.
""")
@PostMapping("/latest")
public LatestDisasterResponse getLatestDisaster(@RequestBody LatestDisasterRequest latestDisasterRequest) {
return disasterService.getLatestDisaster(latestDisasterRequest);
public ResponseEntity<LatestDisasterResponse> getLatestDisaster(@Valid @RequestBody LatestDisasterRequest latestDisasterRequest) {
return ResponseEntity.ok(disasterService.getLatestDisaster(latestDisasterRequest));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonIgnoreProperties(ignoreUnknown = true)
public class DisasterDataResponse {

@JsonProperty("DisasterMsg")
private List<Message> disasterMsg;

@Data
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Message {

Expand All @@ -24,7 +29,8 @@ public static class Message {
private List<RowItem> rowItems;
}

@Data
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Head {

Expand All @@ -44,7 +50,8 @@ public static class Head {
private Result result;
}

@Data
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Result {

Expand All @@ -55,7 +62,8 @@ public static class Result {
private String resultMsg;
}

@Data
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonIgnoreProperties(ignoreUnknown = true)
public static class RowItem {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static LatestDisasterResponse of(Disaster disaster) {
category = "상황";
else
category = disaster.getDisasterType().getDescription();
time = disaster.getCreatedAt().format(DateTimeFormatter.ofPattern("a h시 m분", Locale.KOREAN));
time = disaster.getGeneratedAt().format(DateTimeFormatter.ofPattern("a h시 m분", Locale.KOREAN));
return LatestDisasterResponse.builder()
.isExist(true)
.disasterType(disaster.getDisasterType().getDescription())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ public class Disaster {
private Long disasterNum;

@Comment("재난 발생 시각")
private LocalDateTime createdAt;
private LocalDateTime generatedAt;

@Builder
public Disaster(DisasterType disasterType, String location, String msg, Long disasterNum, LocalDateTime createdAt) {
public Disaster(DisasterType disasterType, String location, String msg, Long disasterNum, LocalDateTime generatedAt) {
this.disasterType = disasterType;
this.location = location;
this.msg = msg;
this.disasterNum = disasterNum;
this.createdAt = createdAt;
this.generatedAt = generatedAt;
}

@Builder
public static Disaster of(DisasterType disasterType, String location, String msg, Long disasterNum, LocalDateTime createdAt) {
public static Disaster of(DisasterType disasterType, String location, String msg, Long disasterNum, LocalDateTime generatedAt) {
return Disaster.builder()
.disasterType(disasterType)
.location(location)
.msg(msg)
.disasterNum(disasterNum)
.createdAt(createdAt)
.generatedAt(generatedAt)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ public interface DisasterRepository extends JpaRepository<Disaster, Long> {
Optional<Disaster> findTopByOrderByDisasterNumDesc();

@Query("select d from Disaster d " +
"where :address " +
"like concat(d.location,'%') " +
"and d.createdAt > :time " +
"order by d.createdAt")
"where :address like concat(d.location,'%') " +
"and d.generatedAt > :time " +
"order by d.generatedAt desc")
List<Disaster> findDisastersInAddressAfterTime(String address, LocalDateTime time, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
Expand All @@ -35,10 +36,9 @@
import java.time.LocalDateTime;
import java.util.*;

@Component
@Service
@RequiredArgsConstructor
@Slf4j
@Transactional(readOnly = true)
public class DisasterDataCollector {
private final RestTemplate restTemplate;
private final DisasterProperties disasterProperties;
Expand All @@ -55,12 +55,12 @@ public void init() {
latestDisasterNum = disaster.getDisasterNum();
}
);
log.info("init latestDisasterNum = " + latestDisasterNum);
}

@Scheduled(fixedDelay = 10 * 1000)
@Transactional
@Scheduled(fixedDelay = 60 * 1000)
public void collectData() {
log.info("collectData()");
//log.info("collectData()");
URI uri = UriComponentsBuilder
.fromUriString(disasterProperties.getApiUrl())
.queryParam("ServiceKey", disasterProperties.getSecretKey())
Expand Down Expand Up @@ -91,7 +91,7 @@ public void collectData() {
for (String loc : locations) {
disasterService.save(SaveDisasterRequest.of(
disasterTypeMap.get(disasterNum),
loc,//이 부분은 메시지 내부 파싱하여 더 정확한 주소를 저장하도록 수정해야함
loc.replace(" 전체", ""),//이 부분은 메시지 내부 파싱하여 더 정확한 주소를 저장하도록 수정해야함
disaster.getMsg(),
disasterNum,
disaster.getCreateDate()
Expand Down Expand Up @@ -119,7 +119,7 @@ private void crawlingDisasterType() {
Elements types = doc.select("[id^=disasterSms_tr_][id$=_DSSTR_SE_NM]");
for (int i = 0; i < nums.size(); i++) {
disasterTypeMap.put(
Long.parseLong(nums.get(i).text()),
Long.parseLong(nums.get(i).text()),
DisasterType.kor2code(types.get(i).text())
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import com.numberone.backend.domain.disaster.dto.response.LatestDisasterResponse;
import com.numberone.backend.domain.disaster.entity.Disaster;
import com.numberone.backend.domain.disaster.repository.DisasterRepository;
import com.numberone.backend.domain.disaster.util.DisasterType;
import com.numberone.backend.util.LocationUtil;
import com.numberone.backend.util.LocationProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -24,10 +22,10 @@
@Slf4j
public class DisasterService {
private final DisasterRepository disasterRepository;
private final LocationUtil locationUtil;
private final LocationProvider locationProvider;

public LatestDisasterResponse getLatestDisaster(LatestDisasterRequest latestDisasterRequest) {
String address = locationUtil.pos2address(latestDisasterRequest.getLatitude(), latestDisasterRequest.getLongitude());
String address = locationProvider.pos2address(latestDisasterRequest.getLatitude(), latestDisasterRequest.getLongitude());
LocalDateTime time = LocalDateTime.now().minusDays(1);
List<Disaster> disasters = disasterRepository.findDisastersInAddressAfterTime(address, time, PageRequest.of(0, 1));
if (!disasters.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@RequiredArgsConstructor
@Slf4j
//위치(주소나 GPS) 관련 기능들 작성할 util함수
public class LocationUtil {
public class LocationProvider {
private final KakaoProperties kakaoProperties;
private final RestTemplate restTemplate;

Expand Down
9 changes: 0 additions & 9 deletions src/main/java/com/numberone/backend/util/Position.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@

import java.util.List;

@Data
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonIgnoreProperties(ignoreUnknown = true)
public class MapApiResponse {
private Meta meta;
private List<Doc> documents;

@Data
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Meta{
@JsonProperty("total_count")
private Integer totalCount;
}

@Data
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Doc{
@JsonProperty("result_type")
Expand Down

0 comments on commit e986912

Please sign in to comment.