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 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)

View file

@ -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');

View file

@ -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();

View file

@ -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()