[#2] genres
This commit is contained in:
@@ -70,4 +70,9 @@ class Show extends Model
|
||||
{
|
||||
return $this->belongsToMany(ContentRating::class, 'show_content_ratings', 'show_id', 'content_rating_id');
|
||||
}
|
||||
|
||||
public function genres()
|
||||
{
|
||||
return $this->belongsToMany(Genre::class, 'show_genres', 'show_id', 'genre_id');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Models\Comment;
|
||||
use App\Models\ContentRating;
|
||||
use App\Models\Episode;
|
||||
use App\Models\ExternalId;
|
||||
use App\Models\Genre;
|
||||
use App\Models\Image;
|
||||
use App\Models\ItemToFetch;
|
||||
use App\Models\ItemToFetchJobs;
|
||||
@@ -75,7 +76,12 @@ class ShowProcessingService implements ProcessingInterface
|
||||
$this->processSeasons($show, $tmdbShow['seasons']);
|
||||
}
|
||||
|
||||
if (!empty($tmdbShow['genres'])) {
|
||||
$this->processGenres($item, $tmdbShow['genres']);
|
||||
}
|
||||
|
||||
// @todo process genres
|
||||
// @todo process created_by
|
||||
// @todo process networks
|
||||
// @todo process production countries
|
||||
// @todo process production companies
|
||||
@@ -728,4 +734,32 @@ class ShowProcessingService implements ProcessingInterface
|
||||
|
||||
return $itemToFetch;
|
||||
}
|
||||
|
||||
private function processGenres($item, array $genres)
|
||||
{
|
||||
foreach ($genres as $tmdbGenre) {
|
||||
$genre = Genre::where('name', $tmdbGenre['name'])
|
||||
->where('tmdb_id', $tmdbGenre['id'])
|
||||
->first();
|
||||
|
||||
if (!$genre) {
|
||||
$genre = Genre::create([
|
||||
'name' => $tmdbGenre['name'],
|
||||
'slug' => Str::slug($tmdbGenre['name']),
|
||||
'tmdb_id' => $tmdbGenre['id'],
|
||||
'type' => Genre::TYPE_SHOW,
|
||||
]);
|
||||
}
|
||||
|
||||
$show = Show::where('tmdb_id', $item->tmdb_id)->first();
|
||||
|
||||
if ($show->whereHas('genres', function ($query) use ($genre) {
|
||||
$query->where('tmdb_id', '=', $genre->tmdb_id);
|
||||
})->exists()) {
|
||||
return;
|
||||
} else {
|
||||
$show->genres()->attach($genre);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user