diff --git a/app/Models/MovieRecommendation.php b/app/Models/MovieRecommendation.php new file mode 100644 index 0000000..63a7586 --- /dev/null +++ b/app/Models/MovieRecommendation.php @@ -0,0 +1,13 @@ +removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_LISTS); } + private function getRecommendations($item) + { + $tmdbRecommendations = $this->moviesApi->getRecommendations($item->tmdb_id); + + if (empty($tmdbRecommendations) || empty($tmdbRecommendations['results'])) { + return; + } + + foreach ($tmdbRecommendations['results'] as $tmdbRecommendation) { + $itemToFetch = ItemToFetch::where('tmdb_id', $tmdbRecommendation['id'])->where('type', ItemToFetch::TYPE_MOVIE)->first(); + + if ($itemToFetch) { + $this->getDetails($itemToFetch); + continue; + } + + $movie = Movie::where('tmdb_id', $tmdbRecommendation['id'])->first(); + + if ($movie) { + continue; + } + + + $itemToFetch = ItemToFetch::create([ + 'tmdb_id' => $tmdbRecommendation['id'], + 'type' => ItemToFetch::TYPE_MOVIE, + ]); + + $jobs = ItemToFetchJobs::JOBS; + $jobs[] = "Details"; + + ItemToFetchJobs::create([ + 'tmdb_id' => $tmdbRecommendation['id'], + 'type' => ItemToFetch::TYPE_MOVIE, + 'remaining_jobs' => json_encode($jobs), + ]); + + $this->getDetails($itemToFetch); + + $movie = Movie::where('tmdb_id', $item->tmdb_id)->first(); + $recommendedMovie = Movie::where('tmdb_id', $tmdbRecommendation['id'])->first(); + + MovieRecommendation::create([ + 'movie_id' => $movie->id, + 'recommended_movie_id' => $recommendedMovie->id, + ]); + } + } + private function processMovieCollection(int $movieId, $belongsToCollection) { $movieCollection = MovieCollection::where('tmdb_id', $belongsToCollection['id'])->first(); diff --git a/database/migrations/2024_12_30_214239_create_movie_recommendations_table.php b/database/migrations/2024_12_30_214239_create_movie_recommendations_table.php new file mode 100644 index 0000000..506a3f4 --- /dev/null +++ b/database/migrations/2024_12_30_214239_create_movie_recommendations_table.php @@ -0,0 +1,29 @@ +id(); + $table->unsignedBigInteger('movie_id'); + $table->unsignedBigInteger('recommended_movie_id'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('movie_recommendations'); + } +};