Skip to content

Commit

Permalink
Merge pull request #14 from syyling/feat/9
Browse files Browse the repository at this point in the history
[feat] 영화 검색 후 상세 정보 조회 기능 추가
  • Loading branch information
syyling authored Mar 12, 2024
2 parents 3732caa + c0624d0 commit 26e9344
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@ public class MovieResponse {
@Getter
@NoArgsConstructor
public static class MovieDetailResponse {

private String title;
private String director;
private String genre;
private String year;
private String plot;
private String poster;
private String nation;

@Builder
public MovieDetailResponse(String title, String director, String genre, String year, String plot) {
public MovieDetailResponse(String title, String director, String genre, String year, String plot, String poster, String nation) {
this.title = title;
this.director = director;
this.genre = genre;
this.year = year;
this.plot = plot;
this.poster = poster;
this.nation = nation;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public static MovieResponse.MovieDetailResponse mapToMovieDetailResponse(Movie m
.genre(movie.getGenre())
.year(movie.getYear())
.plot(movie.getPlot())
.nation(movie.getNation())
.poster(movie.getPoster())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package com.mookive.mookive_backend.movie.application.service;

import com.mookive.mookive_backend.movie.application.dto.response.MovieResponse;
import com.mookive.mookive_backend.movie.application.infra.KmdbComponent;
import com.mookive.mookive_backend.movie.application.infra.KoficComponent;
import com.mookive.mookive_backend.movie.application.mapper.MovieMapper;
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 lombok.*;
import com.mookive.mookive_backend.movie.infra.MovieSearchService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -21,113 +17,15 @@
public class MovieGetService {

private final MovieQueryService movieQueryService;
private final MovieSearchService movieSearchService;
private final MovieSaveService movieSaveService;
private final KmdbComponent kmdbComponent;
private final KoficComponent koficComponent;

@Value("${kmdb.service-key}")
private String kmdbServiceKey;

@Value("${kmdb.collection}")
private String collection;

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

/**
TODO: 예외처리
*/
public MovieResponse.MovieDetailResponse getMovieInfo(String title) throws JSONException {
Movie movie = movieQueryService.findByTitle(title);
public MovieResponse.MovieDetailResponse getMovieDetail(String tmdbId) throws JSONException {
Movie movie = movieQueryService.findByTmdbId(tmdbId);
if(movie == null) {
KmdbResponse kmdbResponse = getKmdbResponse(title);
KoficResponse koficResponse = getKoficResponse(title);
movie = Movie.builder()
.title(koficResponse.getTitle())
.director(kmdbResponse.getDirector())
.genre(kmdbResponse.getGenre())
.plot(kmdbResponse.getPlot())
.year(koficResponse.getYear())
.build();
movieSaveService.saveMovie(movie);
movie = movieSearchService.searchMovie(tmdbId);
movieSaveService.save(movie);
}
return MovieMapper.mapToMovieDetailResponse(movie);
}

private JSONObject getDataResponse(String title) {
JSONObject json = new JSONObject(kmdbComponent.findMovieByTitle(collection, kmdbServiceKey, title, "N"));
JSONArray dataArray = (JSONArray) json.get("Data");
JSONObject dataObject = (JSONObject) dataArray.get(0);
JSONArray dataResult = (JSONArray) dataObject.get("Result");
return (JSONObject) dataResult.get(0);
}

private KmdbResponse getKmdbResponse(String title) {
JSONObject dataObject = getDataResponse(title);
System.out.println(dataObject.toString());
Object genre = dataObject.get("genre");
Object director = getDirector(dataObject);
Object plot = getPlotText(dataObject);
return KmdbResponse.builder()
.director((String) director)
.genre((String) genre)
.plot((String) plot)
.build();
}

private static Object getPlotText(JSONObject dataObject) {
JSONObject plotObject = (JSONObject) dataObject.get("plots");
JSONArray plotArray = (JSONArray) plotObject.get("plot");
JSONObject plotData = (JSONObject) plotArray.get(0);
return plotData.get("plotText");
}

private static Object getDirector(JSONObject dataObject) {
JSONObject directorObject = (JSONObject) dataObject.get("directors");
JSONArray directorArray = (JSONArray) directorObject.get("director");
JSONObject directorData = (JSONObject) directorArray.get(0);
return directorData.get("directorNm");
}

private KoficResponse getKoficResponse(String movieTitle) {
JSONObject json = new JSONObject(koficComponent.findMovieByTitle(koficKey, movieTitle));
JSONObject jsonObject = (JSONObject) json.get("movieListResult");
JSONArray dataResult = (JSONArray) jsonObject.get("movieList");
JSONObject movie = (JSONObject) dataResult.get(0); //결과값이 여러개일 때 문제
String title = movie.get("movieNm").toString();
System.out.println(title);
String year = movie.get("openDt").toString().substring(0, 4);
return KoficResponse.builder()
.title(title)
.year(year)
.build();
}

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
private static class KmdbResponse {
private String director;
private String genre;
private String plot;

@Builder
public KmdbResponse(String director, String genre, String plot) {
this.director = director;
this.genre = genre;
this.plot = plot;
}
}

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
private static class KoficResponse {
private String title;
private String year;

@Builder
public KoficResponse(String title, String year) {
this.title = title;
this.year = year;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@ public class Movie {
private String director;
private String genre;
private String plot;
private String poster;
private String nation;
private String tmdbId;


@Builder
public Movie(String title, String year, String director, String genre, String plot) {
public Movie(String title, String year, String director, String genre,
String plot, String poster, String nation, String tmdbId) {
this.title = title;
this.year = year;
this.director = director;
this.genre = genre;
this.plot = plot;
this.poster = poster;
this.nation = nation;
this.tmdbId = tmdbId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
public interface MovieRepository extends JpaRepository<Movie, Long> {

Movie findByTitle(String title);

Movie findByTmdbId(String tmdbId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ public Movie findById(Long id) {
return movieRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Movie not found"));
}

public Movie findByTmdbId(String tmdbId) {
return movieRepository.findByTmdbId(tmdbId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class MovieSaveService {

private final MovieRepository movieRepository;

public void saveMovie(Movie movie) {
public void save(Movie movie) {
movieRepository.save(movie);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mookive.mookive_backend.movie.application.infra;
package com.mookive.mookive_backend.movie.infra;


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

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

import com.mookive.mookive_backend.movie.domain.entity.Movie;
import lombok.*;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class MovieSearchService {

private final KoficComponent koficComponent;
private final TmdbComponent tmdbComponent;


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

public Movie searchMovie(String tmdbId) {
TmdbResponse tmdbResponse = getTmdbResponse(tmdbId);
KoficResponse koficResponse = getKoficResponse(tmdbResponse.getTitle(), tmdbResponse.getYear());
return Movie.builder()
.tmdbId(tmdbId)
.title(tmdbResponse.getTitle())
.year(tmdbResponse.getYear())
.genre(tmdbResponse.getGenre())
.plot(tmdbResponse.getPlot())
.poster(tmdbResponse.getPoster())
.nation(koficResponse.getNation())
.director(koficResponse.getDirector())
.build();
}

private TmdbResponse getTmdbResponse(String tmdbMovieId) {
JSONObject jsonObject = new JSONObject(tmdbComponent.findMovieById(tmdbMovieId, "ko"));
Object title = jsonObject.get("title");
JSONArray genres = (JSONArray) jsonObject.get("genres");
StringBuilder genre = new StringBuilder();
for(int i=0; i<2; i++) {
JSONObject genreObject = (JSONObject) genres.get(i);
genre.append(genreObject.get("name"));
if(i==0) genre.append(",");
}
Object plot = jsonObject.get("overview");
Object poster = "https://image.tmdb.org/t/p/original" + jsonObject.get("poster_path");
Object year = jsonObject.get("release_date").toString().substring(0, 4);

return TmdbResponse.builder()
.title(title.toString())
.year(year.toString())
.genre(genre.toString())
.plot(plot.toString())
.poster(poster.toString())
.build();
}

private KoficResponse getKoficResponse(String title, String year) {
JSONObject apiResponse = new JSONObject(koficComponent.findMovieByTitleAndYear(koficKey, title, year));
JSONObject jsonObject = (JSONObject) apiResponse.get("movieListResult");
JSONArray dataResult = (JSONArray) jsonObject.get("movieList");
JSONObject movie = (JSONObject) dataResult.get(0);
String nation = movie.get("nationAlt").toString();
JSONArray directors = (JSONArray) movie.get("directors");
StringBuilder director = new StringBuilder();
for(int i=0; i<directors.length(); i++) {
JSONObject directorObject = (JSONObject) directors.get(i);
director.append(directorObject.get("peopleNm"));
if(i<directors.length()-1) director.append(",");
}
return KoficResponse.builder()
.nation(nation)
.director(director.toString())
.build();
}


@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
private static class TmdbResponse {
private String title;
private String year;
private String genre;
private String plot;
private String poster;

@Builder
public TmdbResponse(String title, String year, String genre, String plot, String poster) {
this.title = title;
this.year = year;
this.genre = genre;
this.plot = plot;
this.poster = poster;
}
}

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
private static class KoficResponse {
private String director;
private String nation;

@Builder
public KoficResponse(String director, String nation) {
this.director = director;
this.nation = nation;
}
}
}
Loading

0 comments on commit 26e9344

Please sign in to comment.