update: reduce queries on playlist show

This commit is contained in:
Roardom
2024-05-26 15:38:38 +00:00
parent ef047ef3a4
commit 067cc0e009
2 changed files with 28 additions and 25 deletions
+27 -9
View File
@@ -22,6 +22,7 @@ use App\Models\Movie;
use App\Models\Playlist;
use App\Models\Tv;
use App\Repositories\ChatRepository;
use App\Traits\TorrentMeta;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Exception;
@@ -31,6 +32,8 @@ use Exception;
*/
class PlaylistController extends Controller
{
use TorrentMeta;
/**
* PlaylistController Constructor.
*/
@@ -93,19 +96,34 @@ class PlaylistController extends Controller
{
abort_if($playlist->is_private && $playlist->user_id !== $request->user()->id, 403, trans('playlist.private-error'));
$randomTorrent = $playlist->torrents()->inRandomOrder()->first();
$randomTorrent = $playlist->torrents()->inRandomOrder()->with('category')->first();
$torrents = $playlist->torrents()
->select('*')
->selectRaw("
CASE
WHEN category_id IN (SELECT `id` from `categories` where `movie_meta` = 1) THEN 'movie'
WHEN category_id IN (SELECT `id` from `categories` where `tv_meta` = 1) THEN 'tv'
WHEN category_id IN (SELECT `id` from `categories` where `game_meta` = 1) THEN 'game'
WHEN category_id IN (SELECT `id` from `categories` where `music_meta` = 1) THEN 'music'
WHEN category_id IN (SELECT `id` from `categories` where `no_meta` = 1) THEN 'no'
END as meta
")
->with(['category', 'resolution', 'type', 'user.group'])
->orderBy('name')
->paginate(26);
// See app/Traits/TorrentMeta.php
$this->scopeMeta($torrents);
return view('playlist.show', [
'playlist' => $playlist,
'meta' => match(true) {
$randomTorrent?->category?->tv_meta => Tv::with('genres', 'networks', 'seasons')->find($randomTorrent->tmdb),
$randomTorrent?->category?->movie_meta => Movie::with('genres', 'companies', 'collection')->find($randomTorrent->tmdb),
'playlist' => $playlist->load('user.group'),
'meta' => match (true) {
$randomTorrent?->category?->tv_meta => Tv::find($randomTorrent->tmdb),
$randomTorrent?->category?->movie_meta => Movie::find($randomTorrent->tmdb),
default => null,
},
'torrents' => $playlist->torrents()
->with(['category', 'resolution', 'type', 'user.group'])
->orderBy('name')
->paginate(26),
'torrents' => $torrents,
]);
}
+1 -16
View File
@@ -163,23 +163,8 @@
<h2 class="panel__heading">{{ __('torrent.torrents') }}</h2>
<div class="panel__body playlist__torrents">
@foreach ($torrents as $torrent)
@php
$meta = match (true) {
$torrent->category->tv_meta => App\Models\Tv::query()
->with('genres', 'networks', 'seasons')
->find($torrent->tmdb ?? 0),
$torrent->category->movie_meta => App\Models\Movie::query()
->with('genres', 'companies', 'collection')
->find($torrent->tmdb ?? 0),
$torrent->category->game_meta => MarcReichel\IGDBLaravel\Models\Game::query()
->with(['artworks' => ['url', 'image_id'], 'genres' => ['name']])
->find((int) $playlistTorrent->torrent->igdb),
default => null,
};
@endphp
<div class="playlist__torrent-container">
<x-torrent.card :meta="$meta" :torrent="$torrent" />
<x-torrent.card :meta="$torrent->meta" :torrent="$torrent" />
@if (auth()->id() === $playlist->user_id || auth()->user()->group->is_modo)
<form
action="{{ route('playlist_torrents.destroy', ['playlistTorrent' => $torrent->pivot]) }}"