optimize: torrents list

Eager load the bookmarks and fl tokens
This commit is contained in:
Roardom
2023-02-15 06:01:51 -06:00
parent 4b9603eca3
commit dc4a161fc0
9 changed files with 63 additions and 68 deletions
+7 -15
View File
@@ -14,40 +14,32 @@
namespace App\Http\Livewire;
use App\Models\Torrent;
use App\Models\User;
use Livewire\Component;
class BookmarkButton extends Component
{
public $torrent;
public ?\Illuminate\Contracts\Auth\Authenticatable $user = null;
final public function mount($torrent): void
{
$this->user = auth()->user();
$this->torrent = Torrent::withAnyStatus()->findOrFail($torrent);
}
final public function getIsBookmarkedProperty(): int
{
return $this->torrent->bookmarked() ? 1 : 0;
}
public Torrent $torrent;
public User $user;
public bool $isBookmarked;
final public function store(): void
{
if ($this->user->isBookmarked($this->torrent->id)) {
if ($this->user->bookmarks()->where('torrent_id', '=', $this->torrent->id)->exists()) {
$this->dispatchBrowserEvent('error', ['type' => 'error', 'message' => 'Torrent Has Already Been Bookmarked!']);
return;
}
$this->user->bookmarks()->attach($this->torrent->id);
$this->isBookmarked = true;
$this->dispatchBrowserEvent('success', ['type' => 'success', 'message' => 'Torrent Has Been Bookmarked Successfully!']);
}
final public function destroy(): void
{
$this->user->bookmarks()->detach($this->torrent->id);
$this->isBookmarked = false;
$this->dispatchBrowserEvent('success', ['type' => 'success', 'message' => 'Torrent Has Been Unbookmarked Successfully!']);
}