refactor: use HasUuids trait instead of manual implementation

This commit is contained in:
Phan An 2024-10-23 15:51:02 +07:00
parent bd246c37dd
commit 3e9b94c099
4 changed files with 9 additions and 41 deletions

View file

@ -8,6 +8,7 @@ use App\Models\Song as Playable;
use App\Values\SmartPlaylistRuleGroupCollection; use App\Values\SmartPlaylistRuleGroupCollection;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
@ -15,7 +16,6 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Laravel\Scout\Searchable; use Laravel\Scout\Searchable;
/** /**
@ -39,6 +39,7 @@ class Playlist extends Model
{ {
use Searchable; use Searchable;
use HasFactory; use HasFactory;
use HasUuids;
protected $hidden = ['user_id', 'created_at', 'updated_at']; protected $hidden = ['user_id', 'created_at', 'updated_at'];
protected $guarded = []; protected $guarded = [];
@ -48,18 +49,9 @@ class Playlist extends Model
'own_songs_only' => 'bool', 'own_songs_only' => 'bool',
]; ];
public $incrementing = false;
protected $keyType = 'string';
protected $appends = ['is_smart']; protected $appends = ['is_smart'];
protected $with = ['user', 'collaborators', 'folders']; 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 public function playables(): BelongsToMany
{ {
return $this->belongsToMany(Playable::class) return $this->belongsToMany(Playable::class)

View file

@ -4,11 +4,11 @@ namespace App\Models;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Str;
/** /**
* @property string $id * @property string $id
@ -21,18 +21,11 @@ use Illuminate\Support\Str;
class PlaylistFolder extends Model class PlaylistFolder extends Model
{ {
use HasFactory; use HasFactory;
use HasUuids;
public $incrementing = false;
protected $keyType = 'string';
protected $guarded = ['id']; protected $guarded = ['id'];
protected $with = ['user']; protected $with = ['user'];
protected static function booted(): void
{
static::creating(static fn (self $folder) => $folder->id = Str::uuid()->toString());
}
public function playlists(): BelongsToMany public function playlists(): BelongsToMany
{ {
return $this->belongsToMany(Playlist::class, null, 'folder_id'); return $this->belongsToMany(Playlist::class, null, 'folder_id');

View file

@ -8,11 +8,11 @@ use App\Casts\Podcast\PodcastMetadataCast;
use App\Models\Song as Episode; use App\Models\Song as Episode;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;
use Laravel\Scout\Searchable; use Laravel\Scout\Searchable;
use PhanAn\Poddle\Values\CategoryCollection; use PhanAn\Poddle\Values\CategoryCollection;
use PhanAn\Poddle\Values\ChannelMetadata; use PhanAn\Poddle\Values\ChannelMetadata;
@ -35,14 +35,12 @@ use PhanAn\Poddle\Values\ChannelMetadata;
class Podcast extends Model class Podcast extends Model
{ {
use HasFactory; use HasFactory;
use HasUuids;
use Searchable; use Searchable;
protected $hidden = ['created_at', 'updated_at']; protected $hidden = ['created_at', 'updated_at'];
protected $guarded = []; protected $guarded = [];
protected $with = ['subscribers'];
public $incrementing = false;
protected $keyType = 'string';
protected $casts = [ protected $casts = [
'categories' => CategoriesCast::class, 'categories' => CategoriesCast::class,
@ -51,13 +49,6 @@ class Podcast extends Model
'explicit' => 'boolean', '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 public static function query(): PodcastBuilder
{ {
return parent::query(); return parent::query();

View file

@ -19,12 +19,12 @@ use App\Values\SongStorageMetadata\SongStorageMetadata;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;
use Laravel\Scout\Searchable; use Laravel\Scout\Searchable;
use PhanAn\Poddle\Values\EpisodeMetadata; use PhanAn\Poddle\Values\EpisodeMetadata;
use Throwable; use Throwable;
@ -74,12 +74,11 @@ class Song extends Model
use HasFactory; use HasFactory;
use Searchable; use Searchable;
use SupportsDeleteWhereValueNotIn; 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 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 $guarded = [];
protected $hidden = ['updated_at', 'path', 'mtime']; protected $hidden = ['updated_at', 'path', 'mtime'];
protected $casts = [ protected $casts = [
@ -96,13 +95,6 @@ class Song extends Model
protected $with = ['album', 'artist', 'podcast']; 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 public static function query(?PlayableType $type = null, ?User $user = null): SongBuilder
{ {
return parent::query() return parent::query()