Skip to content

Commit

Permalink
Merge pull request #32 from syyling/feat/27
Browse files Browse the repository at this point in the history
[feat/refactor/fix] 영화 제목 리스트 검색 기능 추가, 영화 제목 검색 결과 조회 기능 추가, kofic api 파라미터 추가, infra 패키지 구조 변경
  • Loading branch information
syyling authored May 25, 2024
2 parents cf85a6c + 05f67fa commit e58f0cf
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,34 @@ public MovieDetailResponse(String title, String director, String genre, String y
this.tagline = tagline;
}
}

@Getter
@NoArgsConstructor
public static class MovieTitleResponse {
private String title;

@Builder
public MovieTitleResponse(String title) {
this.title = title;
}
}

@Getter
@NoArgsConstructor
public static class MovieTitleSearchResponse {
private String title;
private String director;
private String year;
private String poster;
private String nation;

@Builder
public MovieTitleSearchResponse(String title, String director, String year, String poster, String nation) {
this.title = title;
this.director = director;
this.year = year;
this.poster = poster;
this.nation = nation;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.mookive.mookive_backend.movie.domain.entity.Movie;
import com.mookive.mookive_backend.movie.domain.service.MovieQueryService;
import com.mookive.mookive_backend.movie.domain.service.MovieSaveService;
import com.mookive.mookive_backend.movie.infra.MovieSearchService;
import com.mookive.mookive_backend.movie.infra.service.MovieEntityGetService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.json.JSONException;
Expand All @@ -17,13 +17,13 @@
public class MovieGetService {

private final MovieQueryService movieQueryService;
private final MovieSearchService movieSearchService;
private final MovieEntityGetService movieEntityGetService;
private final MovieSaveService movieSaveService;

public MovieResponse.MovieDetailResponse getMovieDetail(String tmdbId) throws JSONException {
Movie movie = movieQueryService.findByTmdbId(tmdbId);
if(movie == null) {
movie = movieSearchService.searchMovie(tmdbId);
movie = movieEntityGetService.searchMovie(tmdbId);
movieSaveService.save(movie);
}
return MovieMapper.mapToMovieDetailResponse(movie);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.mookive.mookive_backend.movie.application.service;

import com.mookive.mookive_backend.movie.application.dto.response.MovieResponse;
import com.mookive.mookive_backend.movie.infra.component.KoficComponent;
import com.mookive.mookive_backend.movie.infra.component.TmdbSearchComponent;
import lombok.*;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class MovieSearchService {

private final TmdbSearchComponent tmdbSearchComponent;
private final KoficComponent koficComponent;

@Value("${kofic.service-key}")
private String koficKey;

public List<MovieResponse.MovieTitleResponse> getMovieTitleList(String title) {
List<MovieResponse.MovieTitleResponse> tmdbMovieTtitleResponseList = new ArrayList<>();
JSONObject jsonObject = new JSONObject(tmdbSearchComponent.findMovieTitleList(title, "ko-KR"));
JSONArray results = (JSONArray) jsonObject.get("results");
for(int i=0; i<results.length(); i++) {
JSONObject result = (JSONObject) results.get(i);
MovieResponse.MovieTitleResponse tmdbMovieTitleResponse = MovieResponse.MovieTitleResponse.builder()
.title(result.get("title").toString())
.build();
tmdbMovieTtitleResponseList.add(tmdbMovieTitleResponse);
}
return tmdbMovieTtitleResponseList;
}

public List<MovieResponse.MovieTitleSearchResponse> getMovieTitleSearchResult(String title) {
List<MovieResponse.MovieTitleSearchResponse> tmdbMovieTitleSearchResponseList = new ArrayList<>();
JSONObject jsonObject = new JSONObject(tmdbSearchComponent.findMovieTitleList(title, "ko-KR"));
JSONArray results = (JSONArray) jsonObject.get("results");
for(int i=0; i<results.length(); i++) {
JSONObject result = (JSONObject) results.get(i);
String titleResult = result.get("title").toString();
String year = result.get("release_date").toString().substring(0, 4);
JSONObject koficResponse = new JSONObject(koficComponent.findMovieByTitleAndYear(koficKey, titleResult, year, year));

JSONObject movieListResult = (JSONObject) koficResponse.get("movieListResult");
JSONArray dataResult = (JSONArray) movieListResult.get("movieList");
if(!dataResult.isEmpty()) {
JSONObject movie = (JSONObject) dataResult.get(0);
String nation = movie.get("nationAlt").toString();
JSONArray directors = (JSONArray) movie.get("directors");
StringBuilder director = new StringBuilder();
for(int j=0; j<directors.length(); j++) {
JSONObject directorObject = (JSONObject) directors.get(j);
director.append(directorObject.get("peopleNm"));
if(j<directors.length()-1) director.append(",");
}
MovieResponse.MovieTitleSearchResponse tmdbMovieTitleSearchResponse = MovieResponse.MovieTitleSearchResponse.builder()
.title(titleResult)
.year(year)
.poster("https://image.tmdb.org/t/p/original" + result.get("poster_path"))
.director(director.toString())
.nation(nation)
.build();
tmdbMovieTitleSearchResponseList.add(tmdbMovieTitleSearchResponse);
}
}
return tmdbMovieTitleSearchResponseList;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mookive.mookive_backend.movie.infra;
package com.mookive.mookive_backend.movie.infra.component;


import org.springframework.stereotype.Component;
Expand All @@ -11,5 +11,6 @@
public interface KoficComponent {

@GetExchange()
String findMovieByTitleAndYear(@RequestParam String key, @RequestParam String movieNm, @RequestParam String openStartDt);
String findMovieByTitleAndYear(@RequestParam String key, @RequestParam String movieNm,
@RequestParam String openStartDt, @RequestParam String openEndDt);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mookive.mookive_backend.movie.infra;
package com.mookive.mookive_backend.movie.infra.component;


import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mookive.mookive_backend.movie.infra.component;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.HttpExchange;

@Component
@HttpExchange
public interface TmdbSearchComponent {

@GetExchange()
String findMovieTitleList (@RequestParam("query") String title, @RequestParam String language);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.mookive.mookive_backend.movie.infra;
package com.mookive.mookive_backend.movie.infra.config;

import com.mookive.mookive_backend.movie.infra.component.KoficComponent;
import com.mookive.mookive_backend.movie.infra.component.TmdbComponent;
import com.mookive.mookive_backend.movie.infra.component.TmdbSearchComponent;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -36,4 +39,17 @@ public TmdbComponent tmdbService() {
return factory.createClient(TmdbComponent.class);

}

@Bean
public TmdbSearchComponent tmdbSearchService() {
RestClient restClient = RestClient.builder()
.baseUrl("https://api.themoviedb.org/3/search/movie")
.defaultHeader("Authorization", token)
.defaultHeader("accept", "application/json")
.build();
RestClientAdapter adapter = RestClientAdapter.create(restClient);
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();
return factory.createClient(TmdbSearchComponent.class);

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mookive.mookive_backend.movie.infra;
package com.mookive.mookive_backend.movie.infra.service;

import com.mookive.mookive_backend.movie.domain.entity.Movie;
import com.mookive.mookive_backend.movie.infra.component.KoficComponent;
import com.mookive.mookive_backend.movie.infra.component.TmdbComponent;
import lombok.*;
import org.json.JSONArray;
import org.json.JSONObject;
Expand All @@ -9,7 +11,7 @@

@Service
@RequiredArgsConstructor
public class MovieSearchService {
public class MovieEntityGetService {

private final KoficComponent koficComponent;
private final TmdbComponent tmdbComponent;
Expand Down Expand Up @@ -60,7 +62,7 @@ private TmdbResponse getTmdbResponse(String tmdbMovieId) {
}

private KoficResponse getKoficResponse(String title, String year) {
JSONObject apiResponse = new JSONObject(koficComponent.findMovieByTitleAndYear(koficKey, title, year));
JSONObject apiResponse = new JSONObject(koficComponent.findMovieByTitleAndYear(koficKey, title, year, year));
JSONObject jsonObject = (JSONObject) apiResponse.get("movieListResult");
JSONArray dataResult = (JSONArray) jsonObject.get("movieList");
JSONObject movie = (JSONObject) dataResult.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,34 @@

import com.mookive.mookive_backend.movie.application.dto.response.MovieResponse;
import com.mookive.mookive_backend.movie.application.service.MovieGetService;
import com.mookive.mookive_backend.movie.application.service.MovieSearchService;
import lombok.RequiredArgsConstructor;
import org.json.JSONException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class MovieController {

private final MovieGetService movieGetService;
private final MovieSearchService movieSearchService;

@GetMapping("/movie/detail")
public MovieResponse.MovieDetailResponse getMovieDetail(@RequestParam String tmdbId) throws JSONException {
return movieGetService.getMovieDetail(tmdbId);
}

@GetMapping("/movie/title/list")
public List<MovieResponse.MovieTitleResponse> getMovieTitleList(@RequestParam String title) {
return movieSearchService.getMovieTitleList(title);
}

@GetMapping("/movie/title/result")
public List<MovieResponse.MovieTitleSearchResponse> getMovieTitleSearchResult(@RequestParam String title) {
return movieSearchService.getMovieTitleSearchResult(title);
}
}

0 comments on commit e58f0cf

Please sign in to comment.