diff --git a/src/main/java/com/catspot/crawler/CrawlerScheduler.java b/src/main/java/com/catspot/crawler/CrawlerScheduler.java index 32527e8..3483892 100644 --- a/src/main/java/com/catspot/crawler/CrawlerScheduler.java +++ b/src/main/java/com/catspot/crawler/CrawlerScheduler.java @@ -21,6 +21,11 @@ public void crawl() { List places = LibraryCrawler.getData(); List studyPlacesToSave = new ArrayList<>(); + if (places.isEmpty()) { + studyPlaceRepository.deleteAll(); + return; + } + for (StudyPlace place : places) { StudyPlace studyPlace = studyPlaceRepository.findById(place.getPlaceIdx()).orElseGet(() -> place); diff --git a/src/main/java/com/catspot/crawler/LibraryCrawler.java b/src/main/java/com/catspot/crawler/LibraryCrawler.java index 5713e37..ddf86de 100644 --- a/src/main/java/com/catspot/crawler/LibraryCrawler.java +++ b/src/main/java/com/catspot/crawler/LibraryCrawler.java @@ -1,6 +1,10 @@ package com.catspot.crawler; +import com.catspot.exceptionhandler.CommonErrorCode; +import com.catspot.exceptionhandler.CustomException; +import com.catspot.exceptionhandler.ErrorCode; import com.catspot.studyplace.StudyPlace; +import lombok.extern.slf4j.Slf4j; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -9,6 +13,7 @@ import java.util.ArrayList; import java.util.List; +@Slf4j public class LibraryCrawler { private static final String baseUrl = "http://203.229.203.240/8080"; private static final String tableUrl = "/Domian5_jythh.asp"; @@ -44,9 +49,10 @@ public static List getData() { data.add(studyPlace); } - if (data.isEmpty()) throw new IOException(); - } catch (IOException e) { - e.printStackTrace(); + + } catch (Exception e) { + log.error("크롤링에 실패했습니다."); + data = new ArrayList<>(); } return data; } diff --git a/src/main/java/com/catspot/exceptionhandler/CommonErrorCode.java b/src/main/java/com/catspot/exceptionhandler/CommonErrorCode.java index 2bc45e1..2deddea 100644 --- a/src/main/java/com/catspot/exceptionhandler/CommonErrorCode.java +++ b/src/main/java/com/catspot/exceptionhandler/CommonErrorCode.java @@ -10,6 +10,7 @@ public enum CommonErrorCode implements ErrorCode{ INVALID_PARAMETER(HttpStatus.BAD_REQUEST, "Invalid parameter included"), RESOURCE_NOT_FOUND(HttpStatus.NOT_FOUND, "Resource not exists"), INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "Internal server error"), + NO_DATA_FOUND(HttpStatus.NOT_FOUND, "No data available") ; private final HttpStatus httpStatus; private final String message; diff --git a/src/main/java/com/catspot/studyplace/StudyPlaceController.java b/src/main/java/com/catspot/studyplace/StudyPlaceController.java index fc4f051..0ccefb7 100644 --- a/src/main/java/com/catspot/studyplace/StudyPlaceController.java +++ b/src/main/java/com/catspot/studyplace/StudyPlaceController.java @@ -2,6 +2,9 @@ import java.util.ArrayList; import java.util.List; + +import com.catspot.exceptionhandler.CommonErrorCode; +import com.catspot.exceptionhandler.CustomException; import lombok.AllArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -16,6 +19,11 @@ public class StudyPlaceController { @GetMapping("/study-seat") public StudyPlaceResponse getAllStudyPlaces() { List allStudyPlaces = studyPlaceRepository.findAll(); + + if(allStudyPlaces.isEmpty()){ + throw new CustomException(CommonErrorCode.NO_DATA_FOUND); + } + List studyPlaceDtos = new ArrayList<>(); for (StudyPlace studyPlace : allStudyPlaces) { diff --git a/src/test/java/com/catspot/crawler/LibraryCrawlerTest.java b/src/test/java/com/catspot/crawler/LibraryCrawlerTest.java index 792b602..7b00f57 100644 --- a/src/test/java/com/catspot/crawler/LibraryCrawlerTest.java +++ b/src/test/java/com/catspot/crawler/LibraryCrawlerTest.java @@ -15,4 +15,5 @@ class LibraryCrawlerTest { System.out.println(data); Assertions.assertFalse(data.isEmpty()); } + }