54
app/Actions/Titles/Retrieve/GetRelatedTitles.php
Executable file
54
app/Actions/Titles/Retrieve/GetRelatedTitles.php
Executable file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Titles\Retrieve;
|
||||
|
||||
use App\Models\Title;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class GetRelatedTitles
|
||||
{
|
||||
public function execute(Title $title, array $params = []): Collection
|
||||
{
|
||||
$titleIds = $this->getByTags($title, $params);
|
||||
|
||||
if ($titleIds->isNotEmpty()) {
|
||||
return Title::whereIn('id', $titleIds)
|
||||
->with(['primaryVideo'])
|
||||
->when(isset($params['compact']), fn($q) => $q->compact())
|
||||
->get();
|
||||
}
|
||||
|
||||
return collect();
|
||||
}
|
||||
|
||||
private function getByTags(Title $title, array $params): Collection
|
||||
{
|
||||
$prefix = DB::getTablePrefix();
|
||||
$keywordIds = $title->keywords->pluck('id');
|
||||
$genreIds = $title->genres->pluck('id');
|
||||
|
||||
return DB::table('titles')
|
||||
->join('keyword_title as k', 'k.title_id', '=', 'titles.id')
|
||||
->join('genre_title as g', 'g.title_id', '=', 'titles.id')
|
||||
->select(
|
||||
DB::raw(
|
||||
"{$prefix}titles.id, COUNT({$prefix}k.id) + COUNT({$prefix}g.id) as total_count",
|
||||
),
|
||||
)
|
||||
->whereIn('k.keyword_id', $keywordIds)
|
||||
->whereIn('g.genre_id', $genreIds)
|
||||
->when($title->release_date, function ($q) use ($title) {
|
||||
$q->whereBetween('release_date', [
|
||||
$title->release_date->subYears(5)->format('Y-m-d'),
|
||||
$title->release_date->addYears(5)->format('Y-m-d'),
|
||||
]);
|
||||
})
|
||||
->where('titles.id', '!=', $title->id)
|
||||
->groupBy('titles.id')
|
||||
->orderBy('total_count', 'desc')
|
||||
->limit(Arr::get($params, 'limit', 10))
|
||||
->pluck('titles.id');
|
||||
}
|
||||
}
|
||||
40
app/Actions/Titles/Retrieve/PaginateSeasonEpisodes.php
Executable file
40
app/Actions/Titles/Retrieve/PaginateSeasonEpisodes.php
Executable file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Titles\Retrieve;
|
||||
|
||||
use App\Models\Title;
|
||||
use Illuminate\Pagination\AbstractPaginator;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class PaginateSeasonEpisodes
|
||||
{
|
||||
public function execute(
|
||||
Title $title,
|
||||
int $seasonNumber,
|
||||
array $params = [],
|
||||
): AbstractPaginator {
|
||||
$builder = $title->episodes()->where('season_number', $seasonNumber);
|
||||
$builder->with(['primaryVideo']);
|
||||
|
||||
$orderBy = Arr::get($params, 'orderBy', 'episode_number');
|
||||
$orderDir = Arr::get($params, 'orderDir', 'asc');
|
||||
|
||||
$pagination = $builder
|
||||
->orderBy($orderBy, $orderDir)
|
||||
->simplePaginate(Arr::get($params, 'perPage', 30));
|
||||
|
||||
// only show first paragraph of description
|
||||
$pagination->through(function ($episode) use ($params) {
|
||||
$episode->description = explode("\n", $episode->description)[0];
|
||||
if (Arr::get($params, 'excludeDescription')) {
|
||||
$episode->description = null;
|
||||
} elseif (Arr::get($params, 'truncateDescriptions')) {
|
||||
$episode->description = Str::limit($episode->description, 200);
|
||||
}
|
||||
return $episode;
|
||||
});
|
||||
|
||||
return $pagination;
|
||||
}
|
||||
}
|
||||
26
app/Actions/Titles/Retrieve/PaginateTitleSeasons.php
Executable file
26
app/Actions/Titles/Retrieve/PaginateTitleSeasons.php
Executable file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Titles\Retrieve;
|
||||
|
||||
use App\Models\Title;
|
||||
use Illuminate\Pagination\AbstractPaginator;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class PaginateTitleSeasons
|
||||
{
|
||||
public function execute(Title $title, array $params = []): AbstractPaginator
|
||||
{
|
||||
return $title
|
||||
->seasons()
|
||||
->select([
|
||||
'seasons.id',
|
||||
'seasons.poster',
|
||||
'seasons.release_date',
|
||||
'number',
|
||||
'title_id',
|
||||
])
|
||||
->withCount('episodes')
|
||||
->orderBy('number', 'desc')
|
||||
->paginate(Arr::get($params, 'perPage', 8));
|
||||
}
|
||||
}
|
||||
54
app/Actions/Titles/Retrieve/PaginateTitles.php
Executable file
54
app/Actions/Titles/Retrieve/PaginateTitles.php
Executable file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Titles\Retrieve;
|
||||
|
||||
use App\Models\Title;
|
||||
use Common\Database\Datasource\Datasource;
|
||||
use Illuminate\Pagination\AbstractPaginator;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class PaginateTitles
|
||||
{
|
||||
public function execute(array $params, $builder = null): AbstractPaginator
|
||||
{
|
||||
if (!$builder) {
|
||||
$builder = Title::query();
|
||||
}
|
||||
|
||||
if ($type = Arr::get($params, 'type')) {
|
||||
$builder->where('is_series', $type === 'series');
|
||||
}
|
||||
|
||||
$builder->with(['primaryVideo']);
|
||||
|
||||
$datasource = new Datasource($builder, $params);
|
||||
|
||||
// prevent duplicate items when ordering by columns that are not
|
||||
// guaranteed to be unique (budget, popularity , revenue etc.)
|
||||
$datasource->secondaryOrderCol = 'id';
|
||||
|
||||
// only show titles with more than 50 votes when filtering by rating
|
||||
if ($datasource->filters->has('tmdb_vote_average')) {
|
||||
$builder->where('tmdb_vote_count', '>=', 50);
|
||||
}
|
||||
|
||||
$order = $datasource->getOrder();
|
||||
|
||||
$order['col'] = str_replace(
|
||||
['user_score', 'rating'],
|
||||
config('common.site.rating_column'),
|
||||
$order['col'],
|
||||
);
|
||||
|
||||
// show titles with less than 50 votes on tmdb last, regardless of their average
|
||||
if (Str::contains($order['col'], 'tmdb_vote_average')) {
|
||||
$builder->orderBy(DB::raw('tmdb_vote_count > 100'), 'desc');
|
||||
}
|
||||
|
||||
$datasource->order = $order;
|
||||
|
||||
return $datasource->paginate();
|
||||
}
|
||||
}
|
||||
55
app/Actions/Titles/Retrieve/ShowTitle.php
Executable file
55
app/Actions/Titles/Retrieve/ShowTitle.php
Executable file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Titles\Retrieve;
|
||||
|
||||
use App\Models\Title;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class ShowTitle
|
||||
{
|
||||
public function execute(int|string $id, array $params): array
|
||||
{
|
||||
if (defined('SHOULD_PRERENDER')) {
|
||||
$params['skipUpdating'] = true;
|
||||
$params['load'] =
|
||||
'images,genres,productionCountries,keywords,videos,primaryVideo,seasons,compactCredits';
|
||||
$params['loadCount'] = 'seasons';
|
||||
}
|
||||
|
||||
if (is_numeric($id) || ctype_digit($id)) {
|
||||
$title = Title::findOrFail($id);
|
||||
} else {
|
||||
$title = Title::firstOrCreateFromEncodedTmdbId($id);
|
||||
}
|
||||
|
||||
if (!Arr::get($params, 'skipUpdating')) {
|
||||
$title = $title->maybeUpdateFromExternal();
|
||||
if (!$title) {
|
||||
abort(404);
|
||||
}
|
||||
}
|
||||
|
||||
$response = ['title' => $title->loadCount('seasons')];
|
||||
|
||||
foreach (explode(',', Arr::get($params, 'load', '')) as $relation) {
|
||||
$methodName = sprintf('load%s', Str::camel($relation));
|
||||
if (method_exists($this, $methodName)) {
|
||||
$response = $this->$methodName($title, $params, $response);
|
||||
} elseif (method_exists($title, $relation)) {
|
||||
$title->load($relation);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (
|
||||
explode(',', Arr::get($params, 'loadCount', ''))
|
||||
as $relation
|
||||
) {
|
||||
if (method_exists($title, $relation)) {
|
||||
$title->loadCount($relation);
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user