From 32f1ae1c1012cad5b647380db9796e88899b86d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8C=E1=85=A9=E1=84=8B=E1=85=A7=E1=86=BC=E1=84=86?= =?UTF-8?q?=E1=85=AE?= Date: Tue, 21 Jan 2025 16:08:55 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=98=AC=ED=95=B4=EC=9D=98=20=EC=98=81?= =?UTF-8?q?=ED=99=94=20100=EA=B0=9C=20=EC=A0=80=EC=9E=A5=ED=95=98=EB=8A=94?= =?UTF-8?q?=20=EA=B8=B0=EB=8A=A5=20#14?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../movie/client/MovieApiClient.java | 73 ++++++------------- .../movie/controller/MovieController.java | 14 ++-- .../movie/service/MovieSchedulerService.java | 30 ++++---- 3 files changed, 41 insertions(+), 76 deletions(-) diff --git a/customer-service/src/main/java/com/example/customerservice/movie/client/MovieApiClient.java b/customer-service/src/main/java/com/example/customerservice/movie/client/MovieApiClient.java index 9eb49ba..1223f20 100644 --- a/customer-service/src/main/java/com/example/customerservice/movie/client/MovieApiClient.java +++ b/customer-service/src/main/java/com/example/customerservice/movie/client/MovieApiClient.java @@ -100,56 +100,27 @@ public KoficMovieInfoDto fetchActor(String movieCode) { /** - * batch 에 사용합니다. - * 요청이 많아지면 API 키 만료 되서 사용 X + * 올해의 영화 100개 불러오기 */ -// public KoficMovieListDto fetchBathMovie(int page, int maxResults) { -// LocalDate today = LocalDate.now(); -// -// String startDate = today.minusYears(2).format(DateTimeFormatter.ofPattern("yyyy")); -// String endDate = today.format(DateTimeFormatter.ofPattern("yyyy")); -// -// URI uri = UriComponentsBuilder.fromUriString(KOFIC_URL + "/searchMovieList.json") -// .queryParam("key", API_KEY) -// .queryParam("curPage", page) -// .queryParam("itemPerPage", maxResults) -// .queryParam("openStartDt", startDate) -// .queryParam("openEndDt", endDate) -// .build() -// .toUri(); -// -// try { -// String response = restTemplate.getForObject(uri, String.class); -// ObjectMapper objectMapper = new ObjectMapper(); -// return objectMapper.readValue(response, KoficMovieListDto.class); -// } catch (Exception e) { -// throw new RuntimeException("Failed", e); -// } -// } - -// public Integer fetchMoviesTotalsCount() { -// LocalDate today = LocalDate.now(); -// -// String startDate = today.minusYears(2).format(DateTimeFormatter.ofPattern("yyyy")); -// String endDate = today.format(DateTimeFormatter.ofPattern("yyyy")); -// -// URI uri = UriComponentsBuilder.fromUriString(KOFIC_URL + "/searchMovieList.json") -// .queryParam("key", API_KEY) -// .queryParam("curPage", "1") -// .queryParam("itemPerPage", "10") -// .queryParam("openStartDt", startDate) -// .queryParam("openEndDt", endDate) -// .build() -// .toUri(); -// -// try { -// String response = restTemplate.getForObject(uri, String.class); -// ObjectMapper objectMapper = new ObjectMapper(); -// KoficMovieListDto koficMovieListDto = objectMapper.readValue(response, KoficMovieListDto.class); -// -// return koficMovieListDto.getMovieListResult().getTotalCount(); -// } catch (Exception e) { -// throw new RuntimeException("Failed", e); -// } -// } + public KoficMovieListDto getMoviesOfThisYearWithLimit100() { + LocalDate today = LocalDate.now(); + + String date = today.format(DateTimeFormatter.ofPattern("yyyy")); + + URI uri = UriComponentsBuilder.fromUriString(KOFIC_URL + "/searchMovieList.json") + .queryParam("key", API_KEY) + .queryParam("curPage", "1") + .queryParam("itemPerPage", "100") + .queryParam("openStartDt", date) + .build() + .toUri(); + + try { + String response = restTemplate.getForObject(uri, String.class); + ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.readValue(response, KoficMovieListDto.class); + } catch (Exception e) { + throw new RuntimeException("Failed", e); + } + } } \ No newline at end of file diff --git a/customer-service/src/main/java/com/example/customerservice/movie/controller/MovieController.java b/customer-service/src/main/java/com/example/customerservice/movie/controller/MovieController.java index 8bdfa9a..744648a 100644 --- a/customer-service/src/main/java/com/example/customerservice/movie/controller/MovieController.java +++ b/customer-service/src/main/java/com/example/customerservice/movie/controller/MovieController.java @@ -4,7 +4,6 @@ import com.example.customerservice.movie.service.MovieSchedulerService; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -18,7 +17,7 @@ public class MovieController { @PostMapping("/start") public ResponseEntity startScheduler() { - SchedulerStatus status = movieSchedulerService.startMovieSync(true); + SchedulerStatus status = movieSchedulerService.startMovieSync(false); return ResponseEntity.ok(status); } @@ -28,10 +27,9 @@ public ResponseEntity stopScheduler() { return ResponseEntity.ok(status); } - /** 요청이 많아지면 API 키 만료 되서 사용 X **/ -// @GetMapping("/batch") -// public String startBath() { -// movieSchedulerService.startBathSaveMovie(); -// return "success"; -// } + @PostMapping("/first/data") + public ResponseEntity saveMoviesFirstData() { + movieSchedulerService.saveMoviesOfThisYearWithLimit100(); + return ResponseEntity.ok("success"); + } } diff --git a/customer-service/src/main/java/com/example/customerservice/movie/service/MovieSchedulerService.java b/customer-service/src/main/java/com/example/customerservice/movie/service/MovieSchedulerService.java index 601259d..c894591 100644 --- a/customer-service/src/main/java/com/example/customerservice/movie/service/MovieSchedulerService.java +++ b/customer-service/src/main/java/com/example/customerservice/movie/service/MovieSchedulerService.java @@ -50,7 +50,7 @@ public class MovieSchedulerService { /** * 스케줄 */ - public SchedulerStatus startMovieSync(boolean isTestMode) { //TODO: 테스트 용 불린값을 제외 해야한다. + public SchedulerStatus startMovieSync(boolean isTestMode) { if (scheduledTask != null && !scheduledTask.isCancelled()) { log.warn("Movie synchronization scheduler is already running."); return SchedulerStatus.ALREADY_RUNNING; @@ -172,21 +172,17 @@ private int checkTotalPages() { /** - * 배치 개발 - * 요청이 많아지면 API 키 만료 되서 사용 X + * 예시 데이터 저장: 올해 영화 총 100 개 저장 */ -// @Transactional -// public void startBathSaveMovie() { -// Integer totalPages = checkTotalPages(); -// for (int page = 1; page < totalPages; page++) { -// KoficMovieListDto koficMovieListDto = movieApiClient.fetchBathMovie(page, maxItemCount); -// List movieList = koficMovieListDto.getMovieListResult().getMovieList(); -// -// movieList.forEach(movieListItem -> { -// if (!contentsRepository.existsByTitle(movieListItem.getMovieName())) { -// processMovieAndActors(movieListItem); -// } -// }); -// } -// } + @Transactional + public void saveMoviesOfThisYearWithLimit100() { + KoficMovieListDto koficMovieListDto = movieApiClient.getMoviesOfThisYearWithLimit100(); + List movieList = koficMovieListDto.getMovieListResult().getMovieList(); + + movieList.forEach(movieListItem -> { + if (!contentsRepository.existsByTitle(movieListItem.getMovieName())) { + processMovieAndActors(movieListItem); + } + }); + } }