You've already forked Atomcms-edit
Initial commit
This commit is contained in:
Executable
+113
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Concerns\BelongsToUser;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class RadioSongRequest extends Model
|
||||
{
|
||||
use BelongsToUser;
|
||||
|
||||
#[\Override]
|
||||
protected $table = 'radio_song_requests';
|
||||
|
||||
#[\Override]
|
||||
protected $guarded = ['id', 'created_at', 'updated_at', 'user_id'];
|
||||
|
||||
/** @var array<string, string> */
|
||||
#[\Override]
|
||||
protected $casts = [
|
||||
'submitted_at' => 'datetime',
|
||||
'played_at' => 'datetime',
|
||||
'is_played' => 'boolean',
|
||||
'is_approved' => 'boolean',
|
||||
'votes' => 'integer',
|
||||
];
|
||||
|
||||
public function votes(): HasMany
|
||||
{
|
||||
return $this->hasMany(RadioSongVote::class);
|
||||
}
|
||||
|
||||
public function isPending(): bool
|
||||
{
|
||||
return ! $this->is_played && ! $this->played_at;
|
||||
}
|
||||
|
||||
public function isQueued(): bool
|
||||
{
|
||||
return $this->is_approved && ! $this->is_played;
|
||||
}
|
||||
|
||||
public function isPlayed(): bool
|
||||
{
|
||||
return $this->is_played || $this->played_at !== null;
|
||||
}
|
||||
|
||||
public function approve(): void
|
||||
{
|
||||
$this->update(['is_approved' => true]);
|
||||
}
|
||||
|
||||
public function markAsPlayed(): void
|
||||
{
|
||||
$this->update([
|
||||
'is_played' => true,
|
||||
'played_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function voteCount(): int
|
||||
{
|
||||
return $this->votes()->count();
|
||||
}
|
||||
|
||||
public function hasUserVoted(User $user): bool
|
||||
{
|
||||
return $this->votes()->where('user_id', $user->id)->exists();
|
||||
}
|
||||
|
||||
public function addVote(User $user): bool
|
||||
{
|
||||
if ($this->hasUserVoted($user)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RadioSongVote::create([
|
||||
'song_request_id' => $this->id,
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$this->increment('votes');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getQueuePosition(): int
|
||||
{
|
||||
return self::where('is_approved', true)
|
||||
->where('is_played', false)
|
||||
->orderBy('votes', 'desc')
|
||||
->orderBy('submitted_at', 'asc')
|
||||
->pluck('id')
|
||||
->search($this->id) + 1;
|
||||
}
|
||||
|
||||
public function scopeQueued($query)
|
||||
{
|
||||
return $query->where('is_approved', true)
|
||||
->where('is_played', false)
|
||||
->orderBy('votes', 'desc')
|
||||
->orderBy('submitted_at', 'asc');
|
||||
}
|
||||
|
||||
public function scopePending($query)
|
||||
{
|
||||
return $query->where('is_approved', false)
|
||||
->orderBy('submitted_at', 'desc');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user