Files

124 lines
3.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Genre;
use App\Models\Season;
use App\Models\Show;
use Illuminate\Http\Request;
class ShowController extends Controller
{
public function index(Request $request)
{
$query = Show::query();
// Search by title
if ($request->filled('title')) {
$query->where('name', 'LIKE', '%' . $request->title . '%');
}
// Filter by genre
if ($request->filled('genre')) {
$query->whereHas('genres', function ($q) use ($request) {
$q->where('genre_id', $request->genre);
});
}
// Filter by actor
if ($request->filled('actor')) {
$query->whereHas('castList', function ($q) use ($request) {
$q->where('person_id', $request->actor);
});
}
// Filter by network
if ($request->filled('network')) {
$query->whereHas('networks', function ($q) use ($request) {
$q->where('network_id', $request->network);
});
}
// Filter by rating
if ($request->filled('rating_min')) {
$query->where('vote_average', '>=', $request->rating_min);
}
if ($request->filled('rating_max')) {
$query->where('vote_average', '<=', $request->rating_max);
}
// Filter by first air year
if ($request->filled('year_min')) {
$query->whereYear('first_air_date', '>=', $request->year_min);
}
if ($request->filled('year_max')) {
$query->whereYear('first_air_date', '<=', $request->year_max);
}
// Filter by status
if ($request->filled('status')) {
$query->where('status', $request->status);
}
// Sorting
$sortBy = $request->get('sort_by', 'first_air_date');
$sortOrder = $request->get('sort_order', 'DESC');
$allowedSorts = ['name', 'first_air_date', 'vote_average', 'vote_count'];
if (in_array($sortBy, $allowedSorts)) {
$query->orderBy($sortBy, $sortOrder);
}
$shows = $query->paginate(48)->appends($request->except('page'));
// Get all genres for filter dropdowns (only TV show genres)
$genres = Genre::where('type', Genre::TYPE_SHOW)->orderBy('name')->get();
// Get all networks for filter dropdown
$networks = \App\Models\Network::orderBy('name')->get();
return view('show.index', compact('shows', 'genres', 'networks'));
}
public function show(string $slug)
{
$show = Show::with('posters', 'backdrops', 'externalIds', 'networks', 'genres', 'productionCompanies')->where('slug', $slug)->first();
$comments = [];
$seasons = Season::where('show_id', $show->id)->orderBy('season_number', 'ASC')->get();
return view('show.show', compact('show', 'comments', 'seasons'));
}
public function season(string $slug, int $seasonNumber)
{
$show = Show::with('posters', 'backdrops', 'externalIds', 'networks', 'genres', 'productionCompanies')->where('slug', $slug)->first();
if (!$show) {
abort(404);
}
$seasons = Season::where('show_id', $show->id)->orderBy('season_number', 'ASC')->get();
$currentSeason = Season::where('show_id', $show->id)->where('season_number', $seasonNumber)->first();
if (!$currentSeason) {
abort(404);
}
$episodes = \App\Models\Episode::where('show_id', $show->id)
->where('season_id', $currentSeason->id)
->orderBy('episode_number', 'ASC')
->get();
$comments = [];
return view('show.season', compact('show', 'seasons', 'currentSeason', 'episodes', 'comments'));
}
public function episode(string $slug, int $seasonNumber, int $episodeNumber)
{
}
}