mirror of
https://github.com/Poniverse/Pony.fm.git
synced 2024-11-28 15:47:59 +01:00
Check if genre exists before creating a new one
This commit is contained in:
parent
66ffd7714d
commit
9b31b48f37
1 changed files with 27 additions and 4 deletions
|
@ -126,10 +126,33 @@ class ParseTrackTagsCommand extends CommandBase
|
|||
* @return int
|
||||
*/
|
||||
protected function getGenreId(string $genreName) {
|
||||
return Genre::firstOrCreate([
|
||||
'name' => $genreName,
|
||||
'slug' => Str::slug($genreName)
|
||||
])->id;
|
||||
$existingGenre = Genre::withTrashed()
|
||||
->where('name', $genreName)->first();
|
||||
|
||||
if ($existingGenre == null) {
|
||||
// Has never existed, create new genre
|
||||
|
||||
return Genre::create([
|
||||
'name' => $genreName,
|
||||
'slug' => Str::slug($genreName)
|
||||
])->id;
|
||||
} else {
|
||||
// Exists in db, has it been deleted?
|
||||
|
||||
$visibleGenre = Genre::where('name', $genreName)->first();
|
||||
|
||||
if ($visibleGenre == null) {
|
||||
// This genre was deleted. Let's bring it back
|
||||
// instead of creating a new one
|
||||
|
||||
$existingGenre->restore();
|
||||
return $existingGenre->id;
|
||||
} else {
|
||||
// It's fine, just return the ID
|
||||
|
||||
return $visibleGenre->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue