Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Eloquent Attributes to enable caching #186

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/Models/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
Expand Down Expand Up @@ -37,9 +38,9 @@ public function approvedFinishedStreams(): HasMany
->finished();
}

public function getHashidAttribute(): string
public function hashid(): Attribute
{
return Hashids::encode((int) $this->attributes['id']);
return Attribute::get(fn() => Hashids::encode($this->id));
}

public function scopeAutoImportEnabled(Builder $query): Builder
Expand Down
37 changes: 21 additions & 16 deletions app/Models/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Services\YouTube\StreamData;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -262,32 +263,36 @@ public function isApproved(): bool
return ! is_null($this->approved_at);
}

public function getDurationAttribute(): ?string
public function duration(): Attribute
{
if (is_null($this->actual_end_time)) {
return null;
}
return Attribute::get(function(): ?string {
if (is_null($this->actual_end_time)) {
return null;
}

$startTime = $this->actual_start_time ?? $this->scheduled_start_time;
$startTime = $this->actual_start_time ?? $this->scheduled_start_time;

return $startTime->diffInHours($this->actual_end_time).'h '.$startTime->diff($this->actual_end_time)->format('%i').'m';
return $startTime->diffInHours($this->actual_end_time).'h '.$startTime->diff($this->actual_end_time)->format('%i').'m';
});
}

public function getStartForHumansAttribute(): string
public function startForHumans(): Attribute
{
if ($this->actual_start_time) {
return "Started {$this->actual_start_time->diffForHumans()}";
}
return Attribute::get(function(): string {
if ($this->actual_start_time) {
return "Started {$this->actual_start_time->diffForHumans()}";
}

if ($this->scheduled_start_time->isPast()) {
return "Started {$this->scheduled_start_time->diffForHumans()}";
}
if ($this->scheduled_start_time->isPast()) {
return "Started {$this->scheduled_start_time->diffForHumans()}";
}

return "Starts {$this->scheduled_start_time->diffForHumans()}";
return "Starts {$this->scheduled_start_time->diffForHumans()}";
});
}

public function getStartForRobotsAttribute(): string
public function startForRobots(): Attribute
{
return $this->actual_start_time?->toIso8601String() ?? $this->scheduled_start_time->toIso8601String();
return Attribute::get(fn() => $this->actual_start_time?->toIso8601String() ?? $this->scheduled_start_time->toIso8601String());
}
}