Skip to content

Commit

Permalink
Merge pull request #21 from GDSC-snowflowerthon/feat/shelter-13
Browse files Browse the repository at this point in the history
[feat] 대피소 목록 가져오기 및 검색 #13
  • Loading branch information
ri-naa authored Jan 11, 2024
2 parents 485c058 + ed01614 commit 3388981
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.web.bind.annotation.RestController;

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

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

@GetMapping("/message")
public MessageResponse getMessage(@AuthenticationPrincipal User user) throws IOException, ParseException {
public List<MessageResponse> getMessage(@AuthenticationPrincipal User user) throws IOException, ParseException {

return messageService.message(user);
}
Expand Down
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,9 +70,41 @@ 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();
Expand All @@ -94,6 +127,7 @@ public MessageResponse message(User user) throws IOException, ParseException {
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);
return response;
*/
}
}
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 3388981

Please sign in to comment.