Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Open API 활용서비스 리팩토링 #131

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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();
}
}
Expand All @@ -128,11 +127,7 @@ private JSONObject getAccommodation(int pageSize, int pageNum) throws JSONExcept
.build(true).toUri();
ResponseEntity<String> 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 {
Expand All @@ -148,10 +143,7 @@ private JSONObject getCommon(long contentId) throws JSONException {
.build(true).toUri();
ResponseEntity<String> 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 {
Expand All @@ -162,10 +154,7 @@ private JSONObject getIntro(long contentId) throws JSONException {
.build(true).toUri();
ResponseEntity<String> 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 {
Expand All @@ -176,10 +165,7 @@ private JSONObject getInfo(long contentId) throws JSONException {
.build(true).toUri();
ResponseEntity<String> 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 {
Expand All @@ -191,16 +177,17 @@ private JSONObject getImages(long contentId) throws JSONException {
.build(true).toUri();
ResponseEntity<String> 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++) {
Expand Down Expand Up @@ -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(
Expand All @@ -257,7 +243,6 @@ private Product saveProduct(JSONObject base, JSONObject common, JSONObject intro
}

private void saveProductImages(Product product, JSONArray images) {
List<ProductImage> productImages = new ArrayList<>();
for (int k = 0; k < images.length(); k++) {
productImageRepository.save(ProductImage.builder()
.product(product)
Expand All @@ -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")))
Expand Down