From 3e9b94c099bc8fe8eaf002c5217c7d1da2279af4 Mon Sep 17 00:00:00 2001 From: Phan An Date: Wed, 23 Oct 2024 15:51:02 +0700 Subject: [PATCH] refactor: use HasUuids trait instead of manual implementation --- app/Models/Playlist.php | 12 ++---------- app/Models/PlaylistFolder.php | 11 ++--------- app/Models/Podcast.php | 15 +++------------ app/Models/Song.php | 12 ++---------- 4 files changed, 9 insertions(+), 41 deletions(-) diff --git a/app/Models/Playlist.php b/app/Models/Playlist.php index 086b8901..81022109 100644 --- a/app/Models/Playlist.php +++ b/app/Models/Playlist.php @@ -8,6 +8,7 @@ use App\Models\Song as Playable; use App\Values\SmartPlaylistRuleGroupCollection; use Carbon\Carbon; use Illuminate\Database\Eloquent\Casts\Attribute; +use Illuminate\Database\Eloquent\Concerns\HasUuids; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -15,7 +16,6 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Support\Arr; use Illuminate\Support\Collection; -use Illuminate\Support\Str; use Laravel\Scout\Searchable; /** @@ -39,6 +39,7 @@ class Playlist extends Model { use Searchable; use HasFactory; + use HasUuids; protected $hidden = ['user_id', 'created_at', 'updated_at']; protected $guarded = []; @@ -48,18 +49,9 @@ class Playlist extends Model 'own_songs_only' => 'bool', ]; - public $incrementing = false; - protected $keyType = 'string'; protected $appends = ['is_smart']; protected $with = ['user', 'collaborators', 'folders']; - protected static function booted(): void - { - static::creating(static function (Playlist $playlist): void { - $playlist->id ??= Str::uuid()->toString(); - }); - } - public function playables(): BelongsToMany { return $this->belongsToMany(Playable::class) diff --git a/app/Models/PlaylistFolder.php b/app/Models/PlaylistFolder.php index de28abb4..d91ef381 100644 --- a/app/Models/PlaylistFolder.php +++ b/app/Models/PlaylistFolder.php @@ -4,11 +4,11 @@ namespace App\Models; use Carbon\Carbon; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Concerns\HasUuids; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; -use Illuminate\Support\Str; /** * @property string $id @@ -21,18 +21,11 @@ use Illuminate\Support\Str; class PlaylistFolder extends Model { use HasFactory; + use HasUuids; - public $incrementing = false; - protected $keyType = 'string'; protected $guarded = ['id']; - protected $with = ['user']; - protected static function booted(): void - { - static::creating(static fn (self $folder) => $folder->id = Str::uuid()->toString()); - } - public function playlists(): BelongsToMany { return $this->belongsToMany(Playlist::class, null, 'folder_id'); diff --git a/app/Models/Podcast.php b/app/Models/Podcast.php index 2fd19d8f..d186835a 100644 --- a/app/Models/Podcast.php +++ b/app/Models/Podcast.php @@ -8,11 +8,11 @@ use App\Casts\Podcast\PodcastMetadataCast; use App\Models\Song as Episode; use Carbon\Carbon; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Concerns\HasUuids; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; -use Illuminate\Support\Str; use Laravel\Scout\Searchable; use PhanAn\Poddle\Values\CategoryCollection; use PhanAn\Poddle\Values\ChannelMetadata; @@ -35,14 +35,12 @@ use PhanAn\Poddle\Values\ChannelMetadata; class Podcast extends Model { use HasFactory; + use HasUuids; use Searchable; protected $hidden = ['created_at', 'updated_at']; protected $guarded = []; - - public $incrementing = false; - - protected $keyType = 'string'; + protected $with = ['subscribers']; protected $casts = [ 'categories' => CategoriesCast::class, @@ -51,13 +49,6 @@ class Podcast extends Model 'explicit' => 'boolean', ]; - protected $with = ['subscribers']; - - protected static function booted(): void - { - static::creating(static fn (self $podcast) => $podcast->id ??= Str::uuid()->toString()); - } - public static function query(): PodcastBuilder { return parent::query(); diff --git a/app/Models/Song.php b/app/Models/Song.php index a1af9ea4..65d400e1 100644 --- a/app/Models/Song.php +++ b/app/Models/Song.php @@ -19,12 +19,12 @@ use App\Values\SongStorageMetadata\SongStorageMetadata; use Carbon\Carbon; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\Attribute; +use Illuminate\Database\Eloquent\Concerns\HasUuids; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; -use Illuminate\Support\Str; use Laravel\Scout\Searchable; use PhanAn\Poddle\Values\EpisodeMetadata; use Throwable; @@ -74,12 +74,11 @@ class Song extends Model use HasFactory; use Searchable; use SupportsDeleteWhereValueNotIn; + use HasUuids; public const ID_REGEX = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'; - public $incrementing = false; protected $guarded = []; - protected $hidden = ['updated_at', 'path', 'mtime']; protected $casts = [ @@ -96,13 +95,6 @@ class Song extends Model protected $with = ['album', 'artist', 'podcast']; - protected $keyType = 'string'; - - protected static function booted(): void - { - static::creating(static fn (Song $song) => $song->id ??= Str::uuid()->toString()); - } - public static function query(?PlayableType $type = null, ?User $user = null): SongBuilder { return parent::query()