You've already forked Atomcms-edit
67 lines
1.5 KiB
PHP
Executable File
67 lines
1.5 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class RadioGiveaway extends Model
|
|
{
|
|
#[\Override]
|
|
protected $table = 'radio_giveaways';
|
|
|
|
#[\Override]
|
|
protected $guarded = ['id', 'created_at', 'updated_at', 'user_id', 'prize'];
|
|
|
|
/** @var array<string, string> */
|
|
#[\Override]
|
|
protected $casts = [
|
|
'starts_at' => 'datetime',
|
|
'ends_at' => 'datetime',
|
|
'is_active' => 'boolean',
|
|
'is_ended' => 'boolean',
|
|
'winner_announced' => 'boolean',
|
|
'prize_value' => 'decimal:2',
|
|
];
|
|
|
|
public function winner(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'winner_id');
|
|
}
|
|
|
|
public function participants(): HasMany
|
|
{
|
|
return $this->hasMany(RadioGiveawayParticipant::class);
|
|
}
|
|
|
|
public function isActive(): bool
|
|
{
|
|
return $this->is_active &&
|
|
now()->gte($this->starts_at) &&
|
|
now()->lte($this->ends_at) &&
|
|
! $this->is_ended;
|
|
}
|
|
|
|
public function hasEnded(): bool
|
|
{
|
|
return now()->isAfter($this->ends_at) || $this->is_ended;
|
|
}
|
|
|
|
public function announceWinner(User $user): void
|
|
{
|
|
$this->update([
|
|
'winner_id' => $user->id,
|
|
'winner_announced' => true,
|
|
'is_ended' => true,
|
|
]);
|
|
}
|
|
|
|
public function participantCount(): int
|
|
{
|
|
return $this->participants()->count();
|
|
}
|
|
}
|