From 433cb4ac53c5188db65523f4207a04dd1983e694 Mon Sep 17 00:00:00 2001 From: JeongUijeong Date: Thu, 14 Dec 2023 14:54:51 +0900 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20Open=20API=20=ED=99=9C=EC=9A=A9?= =?UTF-8?q?=EC=84=9C=EB=B9=84=EC=8A=A4=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/service/OpenApiService.java | 43 ++++++------------- 1 file changed, 13 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/fc/shimpyo_be/domain/product/service/OpenApiService.java b/src/main/java/com/fc/shimpyo_be/domain/product/service/OpenApiService.java index a579b4b9..94918113 100644 --- a/src/main/java/com/fc/shimpyo_be/domain/product/service/OpenApiService.java +++ b/src/main/java/com/fc/shimpyo_be/domain/product/service/OpenApiService.java @@ -22,7 +22,6 @@ import java.nio.charset.StandardCharsets; import java.time.LocalTime; import java.util.ArrayList; -import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.json.JSONArray; @@ -70,8 +69,7 @@ public void init() { @Transactional public void getData(int pageSize, int pageNum) throws JSONException { try { - JSONArray stayArr = getAccommodation(pageSize, pageNum).getJSONObject("items") - .getJSONArray("item"); + JSONArray stayArr = getItems(getAccommodation(pageSize, pageNum)); for (int j = 0; j < stayArr.length(); j++) { try { @@ -96,12 +94,13 @@ public void getData(int pageSize, int pageNum) throws JSONException { saveProductImages(product, images); saveRooms(product, introItem, rooms); } catch (InvalidDataException e) { + log.debug("[OpenAPI] " + stayArr); log.error(e.getMessage()); - log.info("다음 숙소를 조회합니다."); + log.info("[OpenAPI] 다음 숙소를 조회합니다."); } } } catch (Exception e) { - log.error(e.getMessage()); + log.error("[OpenAPI] " + e.getMessage()); throw new OpenApiException(); } } @@ -128,11 +127,7 @@ private JSONObject getAccommodation(int pageSize, int pageNum) throws JSONExcept .build(true).toUri(); ResponseEntity response = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, String.class); - log.info("숙박 정보 조회"); - log.info(response.toString()); - return new JSONObject(response.getBody()) - .getJSONObject("response") - .getJSONObject("body"); + return getBody(response.getBody()); } private JSONObject getCommon(long contentId) throws JSONException { @@ -148,10 +143,7 @@ private JSONObject getCommon(long contentId) throws JSONException { .build(true).toUri(); ResponseEntity commonResponse = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, String.class); - log.info(contentId + "번 데이터 공통 정보 조회" + commonResponse.getBody()); - return new JSONObject(commonResponse.getBody()) - .getJSONObject("response") - .getJSONObject("body"); + return getBody(commonResponse.getBody()); } private JSONObject getIntro(long contentId) throws JSONException { @@ -162,10 +154,7 @@ private JSONObject getIntro(long contentId) throws JSONException { .build(true).toUri(); ResponseEntity introResponse = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, String.class); - log.info(contentId + "번 데이터 소개 정보 조회" + introResponse.getBody()); - return new JSONObject(introResponse.getBody()) - .getJSONObject("response") - .getJSONObject("body"); + return getBody(introResponse.getBody()); } private JSONObject getInfo(long contentId) throws JSONException { @@ -176,10 +165,7 @@ private JSONObject getInfo(long contentId) throws JSONException { .build(true).toUri(); ResponseEntity infoResponse = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, String.class); - log.info(contentId + "번 데이터 반복 정보 조회" + infoResponse.getBody()); - return new JSONObject(infoResponse.getBody()) - .getJSONObject("response") - .getJSONObject("body"); + return getBody(infoResponse.getBody()); } private JSONObject getImages(long contentId) throws JSONException { @@ -191,16 +177,17 @@ private JSONObject getImages(long contentId) throws JSONException { .build(true).toUri(); ResponseEntity imageResponse = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, String.class); - log.info(contentId + "번 데이터 이미지 정보 조회" + imageResponse.getBody()); - return new JSONObject(imageResponse.getBody()) - .getJSONObject("response") - .getJSONObject("body"); + return getBody(imageResponse.getBody()); } private JSONArray getItems(JSONObject jsonObject) { return jsonObject.getJSONObject("items").getJSONArray("item"); } + private JSONObject getBody(String source) { + return new JSONObject(source).getJSONObject("response").getJSONObject("body"); + } + private boolean hasRoom(JSONArray info) throws JSONException { boolean hasRoom = false; for (int i = 0; i < info.length(); i++) { @@ -234,7 +221,6 @@ private Product saveProduct(JSONObject base, JSONObject common, JSONObject intro .sports(intro.get("sports").equals("1")) .seminar(intro.get("seminar").equals("1")) .build(); - Product product = Product.builder() .name(base.getString("title")) .address( @@ -257,7 +243,6 @@ private Product saveProduct(JSONObject base, JSONObject common, JSONObject intro } private void saveProductImages(Product product, JSONArray images) { - List productImages = new ArrayList<>(); for (int k = 0; k < images.length(); k++) { productImageRepository.save(ProductImage.builder() .product(product) @@ -274,13 +259,11 @@ private void saveRooms(Product product, JSONObject intro, JSONArray info) throws } if (Integer.parseInt(roomJson.getString("roomcount")) != 0) { for (int j = 0; j < Integer.parseInt(roomJson.getString("roomcount")); j++) { - System.out.println(intro.toString()); String[] stringCheckIn = intro.getString("checkintime").split(":|;|시"); String[] stringCheckOut = intro.getString("checkouttime").split(":|;|시"); LocalTime checkIn = getTimeFromString(stringCheckIn); LocalTime checkOut = getTimeFromString(stringCheckOut); - System.out.println(roomJson); RoomPrice roomPrice = RoomPrice.builder() .offWeekDaysMinFee(Integer.parseInt( roomJson.getString("roomoffseasonminfee1"))) From 55e68448a50e2854a2ac6437c35d87eca5a04721 Mon Sep 17 00:00:00 2001 From: JeongUijeong Date: Thu, 14 Dec 2023 23:29:12 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20debug=20=EB=A1=9C=EA=B9=85=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fc/shimpyo_be/domain/product/service/OpenApiService.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/fc/shimpyo_be/domain/product/service/OpenApiService.java b/src/main/java/com/fc/shimpyo_be/domain/product/service/OpenApiService.java index 94918113..63593ce3 100644 --- a/src/main/java/com/fc/shimpyo_be/domain/product/service/OpenApiService.java +++ b/src/main/java/com/fc/shimpyo_be/domain/product/service/OpenApiService.java @@ -94,9 +94,7 @@ public void getData(int pageSize, int pageNum) throws JSONException { saveProductImages(product, images); saveRooms(product, introItem, rooms); } catch (InvalidDataException e) { - log.debug("[OpenAPI] " + stayArr); log.error(e.getMessage()); - log.info("[OpenAPI] 다음 숙소를 조회합니다."); } } } catch (Exception e) {