Skip to content

Commit

Permalink
Merge pull request #20 from ri-naa/feat/message-3
Browse files Browse the repository at this point in the history
[feat] 대피소 목록 가져오기 및 검색 #13
  • Loading branch information
ri-naa authored Jan 11, 2024
2 parents f924dd9 + b1188f2 commit ed01614
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.addAllowedOrigin("http://localhost:3001");
configuration.addAllowedOrigin("http://localhost:3000");
configuration.addAllowedOrigin("http://localhost:5173");
configuration.addAllowedMethod("*");
configuration.addAllowedHeader("*");
configuration.setAllowCredentials(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/disaster")
Expand All @@ -24,14 +25,8 @@ public MessageController(MessageService messageService) {
}

@GetMapping("/message")
public MessageResponse getMessage(@AuthenticationPrincipal User user) throws IOException, ParseException {
// try {
// messageService.message();
// } catch (IOException e) {
// e.printStackTrace();
// } catch (ParseException e) {
// throw new RuntimeException(e);
// }
public List<MessageResponse> getMessage(@AuthenticationPrincipal User user) throws IOException, ParseException {

return messageService.message(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@
@Data
public class MessageResponse {
private String MSG_CN; //메세지 내용
private String trans_MSG_CN; //번역된 내용
private String CREAT_DT; //생성일
private List<String> RCV_AREA_ID; //수신지역 id
private List<String> RCV_AREA_NM; //수신지역명
private String EMRGNCY_STEP_ID; //긴급단계 id
private String DSSTR_SE_ID; //재해구분 id
private String DSSTR_SE_NM; //재해구분 명

public MessageResponse(String MSG_CN, String CREAT_DT, List<String> RCV_AREA_ID, String EMRGNCY_STEP_ID, String DSSTR_SE_ID){
public MessageResponse(String MSG_CN, String trans_MSG_CN, String CREAT_DT, List<String> RCV_AREA_ID, List<String> RCV_AREA_NM, String EMRGNCY_STEP_ID, String DSSTR_SE_ID, String DSSTR_SE_NM){
this.MSG_CN = MSG_CN;
this.trans_MSG_CN = trans_MSG_CN;
this.CREAT_DT = CREAT_DT;
this.RCV_AREA_ID = RCV_AREA_ID;
this.RCV_AREA_NM = RCV_AREA_NM;
this.EMRGNCY_STEP_ID = EMRGNCY_STEP_ID;
this.DSSTR_SE_ID = DSSTR_SE_ID;
this.DSSTR_SE_NM = DSSTR_SE_NM;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand All @@ -29,14 +30,14 @@ public class MessageService {

private final NaverTransService naverTransService;

public MessageResponse message(User user) throws IOException, ParseException {
public List<MessageResponse> message(User user) throws IOException, ParseException {
StringBuilder urlBuilder = new StringBuilder("https://www.safetydata.go.kr/openApi");

urlBuilder.append("/" + URLEncoder.encode("행정안전부_긴급재난문자","UTF-8"));
urlBuilder.append("?serviceKey=" + secretKey);
urlBuilder.append("&returnType=json");
urlBuilder.append("&pageNum=1");
urlBuilder.append("&numRowsPerPage=1");
urlBuilder.append("&numRowsPerPage=4");

URL url = new URL(urlBuilder.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
Expand Down Expand Up @@ -69,23 +70,64 @@ public MessageResponse message(User user) throws IOException, ParseException {
JSONObject jsonObject = (JSONObject) jsonParser.parse(responseBody);

JSONObject responseData = (JSONObject) jsonObject.get("responseData");
JSONArray data = (JSONArray) responseData.get("data");
JSONArray dataArr = (JSONArray) responseData.get("data");

JSONObject dataObject = (JSONObject) data.get(0);
List<MessageResponse> msgResArr = new ArrayList<>();

for(int i=0; i<dataArr.size(); i++){
JSONObject dataObject = (JSONObject) dataArr.get(i);

String CREAT_DT = dataObject.get("CREAT_DT").toString();
String DSSTR_SE_ID = dataObject.get("DSSTR_SE_ID").toString();
String DSSTR_SE_NM = dataObject.get("DSSTR_SE_NM").toString();
String EMRGNCY_STEP_ID = dataObject.get("EMRGNCY_STEP_ID").toString();
String MSG_CN = dataObject.get("MSG_CN").toString();
String RCV_AREA_ID = dataObject.get("RCV_AREA_ID").toString();
String RCV_AREA_NM = dataObject.get("RCV_AREA_NM").toString();

List<String> areaIdArr = Arrays.stream(RCV_AREA_ID.split(",")).toList();
for(int j=0;j<areaIdArr.size();j++){
String a = areaIdArr.get(j);
}

List<String> areaNmArr = Arrays.stream(RCV_AREA_NM.split(",")).toList();
for(int j=0;j<areaNmArr.size();j++){
String a = areaNmArr.get(j);
}

String translatedMSG = naverTransService.getTransSentence(MSG_CN, user);
MessageResponse response = new MessageResponse(MSG_CN, translatedMSG, CREAT_DT, areaIdArr, areaNmArr, EMRGNCY_STEP_ID, DSSTR_SE_ID, DSSTR_SE_NM);

msgResArr.add(response);
}

return msgResArr;

/*
JSONObject dataObject = (JSONObject) dataArr.get(0);
String CREAT_DT = dataObject.get("CREAT_DT").toString();
String DSSTR_SE_ID = dataObject.get("DSSTR_SE_ID").toString();
String DSSTR_SE_NM = dataObject.get("DSSTR_SE_NM").toString();
String EMRGNCY_STEP_ID = dataObject.get("EMRGNCY_STEP_ID").toString();
String MSG_CN = dataObject.get("MSG_CN").toString();
String RCV_AREA_ID = dataObject.get("RCV_AREA_ID").toString();
String RCV_AREA_NM = dataObject.get("RCV_AREA_NM").toString();
List<String> areaIdArr = Arrays.stream(RCV_AREA_ID.split(",")).toList();
for(int i=0;i<areaIdArr.size();i++){
String a = areaIdArr.get(i);
}
List<String> areaArr = Arrays.stream(RCV_AREA_ID.split(",")).toList();
for(int i=0;i<areaArr.size();i++){
String a = areaArr.get(i);
List<String> areaNmArr = Arrays.stream(RCV_AREA_NM.split(",")).toList();
for(int i=0;i<areaNmArr.size();i++){
String a = areaNmArr.get(i);
}
String translatedMSG = naverTransService.getTransSentence(MSG_CN, user);
MessageResponse response = new MessageResponse(translatedMSG, CREAT_DT, areaArr, EMRGNCY_STEP_ID, DSSTR_SE_ID);
return response;
MessageResponse response = new MessageResponse(MSG_CN, translatedMSG, CREAT_DT, areaIdArr, areaNmArr, EMRGNCY_STEP_ID, DSSTR_SE_ID, DSSTR_SE_NM);
*/
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.ALGo.ALGo_server.repository;

import com.ALGo.ALGo_server.entity.Shelter;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ShelterRepository extends JpaRepository<Shelter, Long> {

List<Shelter> findAll();

List<Shelter> findByRegion(String region);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.ALGo.ALGo_server.shelter.controller;

import com.ALGo.ALGo_server.entity.Shelter;
import com.ALGo.ALGo_server.repository.ShelterRepository;
import com.ALGo.ALGo_server.shelter.dto.ShelterResponse;
import com.ALGo.ALGo_server.shelter.service.ShelterService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shelter")
public class ShelterController {

private final ShelterRepository shelterRepository;
private final ShelterService shelterService;

public ShelterController(ShelterRepository shelterRepository, ShelterService shelterService) {
this.shelterRepository = shelterRepository;
this.shelterService = shelterService;
}

@GetMapping
public List<Shelter> getAllShelters() {
return shelterRepository.findAll();
}

@PostMapping("/search/{region}")
public List<ShelterResponse> searchSheltersByRegion(@PathVariable String region) {
return shelterService.searchSheltersByRegion(region);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.ALGo.ALGo_server.shelter.dto;

import jakarta.persistence.Column;
import lombok.Data;

import java.util.List;

@Data
public class ShelterResponse {
private Long shelter_id;
private String region;
private String address;
private String description;

public ShelterResponse(Long shelter_id, String region, String address, String description){
this.shelter_id = shelter_id;
this.region = region;
this.address = address;
this.description = description;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.ALGo.ALGo_server.shelter.service;

import com.ALGo.ALGo_server.entity.Shelter;
import com.ALGo.ALGo_server.repository.ShelterRepository;
import com.ALGo.ALGo_server.shelter.dto.ShelterResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
@RequiredArgsConstructor
@Slf4j
public class ShelterService {

private final ShelterRepository shelterRepository;

public List<ShelterResponse> searchSheltersByRegion(String region) {
List<Shelter> shelters = shelterRepository.findByRegion(region);
return convertToShelterResponseList(shelters);
}

private List<ShelterResponse> convertToShelterResponseList(List<Shelter> shelters) {
List<ShelterResponse> shelterResponses = new ArrayList<>();
for (Shelter shelter : shelters) {
ShelterResponse shelterResponse = new ShelterResponse(
shelter.getShelter_id(),
shelter.getRegion(),
shelter.getAddress(),
shelter.getDescription()
);
shelterResponses.add(shelterResponse);
}
return shelterResponses;
}


}

0 comments on commit ed01614

Please sign in to comment.