mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
refactoring and code cleanup
This commit is contained in:
parent
a637e697e1
commit
6732de1188
@ -21,11 +21,11 @@ interface AnimeRepository extends Repository
|
||||
|
||||
public function exceptItemsWithAdultRating(): EloquentBuilder|ScoutBuilder;
|
||||
|
||||
public function excludeKidsItems(&$builder): Collection|EloquentBuilder|ScoutBuilder;
|
||||
public function excludeKidsItems($builder): Collection|EloquentBuilder|ScoutBuilder;
|
||||
|
||||
public function excludeNsfwItems(&$builder): Collection|EloquentBuilder|ScoutBuilder;
|
||||
public function excludeNsfwItems($builder): Collection|EloquentBuilder|ScoutBuilder;
|
||||
|
||||
public function excludeUnapprovedItems(&$builder): Collection|EloquentBuilder|ScoutBuilder;
|
||||
public function excludeUnapprovedItems($builder): Collection|EloquentBuilder|ScoutBuilder;
|
||||
|
||||
public function orderByPopularity(): EloquentBuilder|ScoutBuilder;
|
||||
|
||||
@ -34,19 +34,13 @@ interface AnimeRepository extends Repository
|
||||
public function orderByRank(): EloquentBuilder|ScoutBuilder;
|
||||
|
||||
public function getCurrentlyAiring(
|
||||
?AnimeScheduleFilterEnum $filter = null,
|
||||
bool $kids = false,
|
||||
bool $sfw = false,
|
||||
?bool $unapproved = false
|
||||
?AnimeScheduleFilterEnum $filter = null
|
||||
): EloquentBuilder;
|
||||
|
||||
public function getAiredBetween(
|
||||
Carbon $from,
|
||||
Carbon $to,
|
||||
?AnimeTypeEnum $type = null,
|
||||
?bool $kids = false,
|
||||
?bool $sfw = false,
|
||||
?bool $unapproved = false
|
||||
?AnimeTypeEnum $type = null
|
||||
): EloquentBuilder;
|
||||
|
||||
public function getUpcomingSeasonItems(?AnimeTypeEnum $type = null): EloquentBuilder;
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Dto;
|
||||
|
||||
use App\Casts\ContextualBooleanCast;
|
||||
use App\Contracts\DataRequest;
|
||||
use App\Dto\Concerns\HasSfwParameter;
|
||||
use App\Enums\AnimeOrderByEnum;
|
||||
@ -13,7 +12,6 @@ use App\Http\Resources\V4\AnimeCollection;
|
||||
use App\Rules\Attributes\EnumValidation;
|
||||
use Spatie\LaravelData\Attributes\MapInputName;
|
||||
use Spatie\LaravelData\Attributes\MapOutputName;
|
||||
use Spatie\LaravelData\Attributes\Validation\BooleanType;
|
||||
use Spatie\LaravelData\Attributes\Validation\IntegerType;
|
||||
use Spatie\LaravelData\Attributes\Validation\Min;
|
||||
use Spatie\LaravelData\Attributes\Validation\Prohibits;
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Features;
|
||||
|
||||
use App\Contracts\AnimeRepository;
|
||||
use App\Contracts\RequestHandler;
|
||||
use App\Dto\QueryAnimeSeasonCommand;
|
||||
use App\Enums\AnimeSeasonEnum;
|
||||
@ -19,14 +20,36 @@ use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
||||
*/
|
||||
abstract class QueryAnimeSeasonHandlerBase implements RequestHandler
|
||||
{
|
||||
public function __construct(protected readonly AnimeRepository $repository)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param QueryAnimeSeasonCommand $request
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function handle($request): JsonResponse
|
||||
{
|
||||
$type = collect($request->all())->has("filter") ? $request->filter : null;
|
||||
$results = $this->getSeasonItems($request, $type, $request->kids, $request->sfw);
|
||||
$requestParams = collect($request->all());
|
||||
$type = $requestParams->has("filter") ? $request->filter : null;
|
||||
$results = $this->getSeasonItems($request, $type);
|
||||
|
||||
$includeUnapproved = $requestParams->get("unapproved", false);
|
||||
$includeKids = $requestParams->get("kids", false);
|
||||
$shouldBeSfw = $requestParams->get("sfw", false);
|
||||
|
||||
if (!$includeUnapproved) {
|
||||
$results = $this->repository->excludeUnapprovedItems($results);
|
||||
}
|
||||
|
||||
if (!$includeKids) {
|
||||
$results = $this->repository->excludeKidsItems($results);
|
||||
}
|
||||
|
||||
if ($shouldBeSfw) {
|
||||
$results = $this->repository->excludeNsfwItems($results);
|
||||
}
|
||||
|
||||
$results = $results->paginate($request->limit, ["*"], null, $request->page);
|
||||
|
||||
$animeCollection = new AnimeCollection($results);
|
||||
|
@ -17,10 +17,6 @@ use Illuminate\Support\Carbon;
|
||||
*/
|
||||
final class QueryCurrentAnimeSeasonHandler extends QueryAnimeSeasonHandlerBase
|
||||
{
|
||||
public function __construct(private readonly AnimeRepository $repository)
|
||||
{
|
||||
}
|
||||
|
||||
public function requestClass(): string
|
||||
{
|
||||
return QueryCurrentAnimeSeasonCommand::class;
|
||||
|
@ -2,8 +2,8 @@
|
||||
|
||||
namespace App\Features;
|
||||
|
||||
use App\Contracts\AnimeRepository;
|
||||
use App\Dto\QuerySpecificAnimeSeasonCommand;
|
||||
use App\Enums\AnimeStatusEnum;
|
||||
use App\Enums\AnimeTypeEnum;
|
||||
use Illuminate\Contracts\Database\Query\Builder;
|
||||
use Illuminate\Support\Carbon;
|
||||
@ -13,10 +13,6 @@ use Illuminate\Support\Carbon;
|
||||
*/
|
||||
final class QuerySpecificAnimeSeasonHandler extends QueryAnimeSeasonHandlerBase
|
||||
{
|
||||
public function __construct(private readonly AnimeRepository $repository)
|
||||
{
|
||||
}
|
||||
|
||||
public function requestClass(): string
|
||||
{
|
||||
return QuerySpecificAnimeSeasonCommand::class;
|
||||
@ -30,6 +26,7 @@ final class QuerySpecificAnimeSeasonHandler extends QueryAnimeSeasonHandlerBase
|
||||
*/
|
||||
|
||||
[$from, $to] = $this->getSeasonRange($request->year, $request->season);
|
||||
return $this->repository->getAiredBetween($from, $to, $type, $request->kids, $request->sfw, $request->unapproved);
|
||||
return $this->repository->getAiredBetween($from, $to, $type)
|
||||
->where("status", "!=", AnimeStatusEnum::upcoming()->label);
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Features;
|
||||
|
||||
use App\Contracts\AnimeRepository;
|
||||
use App\Dto\QueryUpcomingAnimeSeasonCommand;
|
||||
use App\Enums\AnimeTypeEnum;
|
||||
use Illuminate\Contracts\Database\Query\Builder;
|
||||
@ -12,10 +11,6 @@ use Illuminate\Contracts\Database\Query\Builder;
|
||||
*/
|
||||
final class QueryUpcomingAnimeSeasonHandler extends QueryAnimeSeasonHandlerBase
|
||||
{
|
||||
public function __construct(private readonly AnimeRepository $repository)
|
||||
{
|
||||
}
|
||||
|
||||
public function requestClass(): string
|
||||
{
|
||||
return QueryUpcomingAnimeSeasonCommand::class;
|
||||
|
@ -52,20 +52,20 @@ final class DefaultAnimeRepository extends DatabaseRepository implements AnimeRe
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function excludeNsfwItems(&$builder): EloquentBuilder|ScoutBuilder
|
||||
public function excludeNsfwItems($builder): EloquentBuilder|ScoutBuilder
|
||||
{
|
||||
return $builder
|
||||
->where("demographics.mal_id", "!=", Constants::GENRE_ANIME_HENTAI)
|
||||
->where("demographics.mal_id", "!=", Constants::GENRE_ANIME_EROTICA);
|
||||
}
|
||||
|
||||
public function excludeUnapprovedItems(&$builder): Collection|EloquentBuilder|ScoutBuilder
|
||||
public function excludeUnapprovedItems($builder): Collection|EloquentBuilder|ScoutBuilder
|
||||
{
|
||||
return $builder
|
||||
->where("approved", true);
|
||||
}
|
||||
|
||||
public function excludeKidsItems(&$builder): EloquentBuilder|ScoutBuilder
|
||||
public function excludeKidsItems($builder): EloquentBuilder|ScoutBuilder
|
||||
{
|
||||
return $builder
|
||||
->where("demographics.mal_id", "!=", Constants::GENRE_ANIME_KIDS);
|
||||
@ -90,10 +90,7 @@ final class DefaultAnimeRepository extends DatabaseRepository implements AnimeRe
|
||||
}
|
||||
|
||||
public function getCurrentlyAiring(
|
||||
?AnimeScheduleFilterEnum $filter = null,
|
||||
?bool $kids = false,
|
||||
?bool $sfw = false,
|
||||
?bool $unapproved = false
|
||||
?AnimeScheduleFilterEnum $filter = null
|
||||
): EloquentBuilder
|
||||
{
|
||||
/*
|
||||
@ -106,18 +103,6 @@ final class DefaultAnimeRepository extends DatabaseRepository implements AnimeRe
|
||||
->where("type", AnimeTypeEnum::tv()->label)
|
||||
->where("status", AnimeStatusEnum::airing()->label);
|
||||
|
||||
if (!$unapproved) {
|
||||
$this->excludeUnapprovedItems($queryable);
|
||||
}
|
||||
|
||||
if (!$kids) {
|
||||
$this->excludeKidsItems($queryable);
|
||||
}
|
||||
|
||||
if ($sfw) {
|
||||
$this->excludeNsfwItems($queryable);
|
||||
}
|
||||
|
||||
if (!is_null($filter)) {
|
||||
if ($filter->isWeekDay()) {
|
||||
$queryable = $queryable->where("broadcast", "like", "{$filter->label}%");
|
||||
@ -133,10 +118,7 @@ final class DefaultAnimeRepository extends DatabaseRepository implements AnimeRe
|
||||
public function getAiredBetween(
|
||||
Carbon $from,
|
||||
Carbon $to,
|
||||
?AnimeTypeEnum $type = null,
|
||||
?bool $kids = false,
|
||||
?bool $sfw = false,
|
||||
?bool $unapproved = false
|
||||
?AnimeTypeEnum $type = null
|
||||
): EloquentBuilder
|
||||
{
|
||||
$queryable = $this->queryable(true)->whereBetween("aired.from", [
|
||||
@ -148,26 +130,11 @@ final class DefaultAnimeRepository extends DatabaseRepository implements AnimeRe
|
||||
$queryable = $queryable->where("type", $type->label);
|
||||
}
|
||||
|
||||
if (!$unapproved) {
|
||||
$this->excludeUnapprovedItems($queryable);
|
||||
}
|
||||
|
||||
if (!$kids) {
|
||||
$this->excludeKidsItems($queryable);
|
||||
}
|
||||
|
||||
if ($sfw) {
|
||||
$this->excludeNsfwItems($queryable);
|
||||
}
|
||||
|
||||
return $queryable->orderBy("members", "desc");
|
||||
}
|
||||
|
||||
public function getUpcomingSeasonItems(
|
||||
?AnimeTypeEnum $type = null,
|
||||
?bool $kids = false,
|
||||
?bool $sfw = false,
|
||||
?bool $unapproved = false
|
||||
?AnimeTypeEnum $type = null
|
||||
): EloquentBuilder
|
||||
{
|
||||
$queryable = $this->queryable(true)->where("status", AnimeStatusEnum::upcoming()->label);
|
||||
@ -176,18 +143,6 @@ final class DefaultAnimeRepository extends DatabaseRepository implements AnimeRe
|
||||
$queryable = $queryable->where("type", $type->label);
|
||||
}
|
||||
|
||||
if (!$unapproved) {
|
||||
$this->excludeUnapprovedItems($queryable);
|
||||
}
|
||||
|
||||
if (!$kids) {
|
||||
$this->excludeKidsItems($queryable);
|
||||
}
|
||||
|
||||
if ($sfw) {
|
||||
$this->excludeNsfwItems($queryable);
|
||||
}
|
||||
|
||||
return $queryable->orderBy("members", "desc");
|
||||
}
|
||||
}
|
||||
|
@ -55,17 +55,4 @@ final class DefaultMangaRepository extends DatabaseRepository implements MangaRe
|
||||
->where("demographics.mal_id", "!=", Constants::GENRE_MANGA_HENTAI)
|
||||
->where("demographics.mal_id", "!=", Constants::GENRE_MANGA_EROTICA);
|
||||
}
|
||||
|
||||
public function excludeNsfwItems(&$builder): EloquentBuilder|ScoutBuilder
|
||||
{
|
||||
return $builder
|
||||
->where("demographics.mal_id", "!=", Constants::GENRE_MANGA_HENTAI)
|
||||
->where("demographics.mal_id", "!=", Constants::GENRE_MANGA_EROTICA);
|
||||
}
|
||||
|
||||
public function excludeUnapprovedItems(&$builder): Collection|EloquentBuilder|ScoutBuilder
|
||||
{
|
||||
return $builder
|
||||
->where("approved", true);
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ class TypeSenseScoutSearchService implements ScoutSearchService
|
||||
// which will make Typesense consider all variations of prefixes and typo corrections of the words
|
||||
// in the query exhaustively, without stopping early when enough results are found.
|
||||
$options['exhaustive_search'] = env('TYPESENSE_SEARCH_EXHAUSTIVE', "true");
|
||||
$options['search_cutoff_ms'] = (int) env('TYPESENSE_SEARCH_EXHAUSTIVE', 450);
|
||||
$options['search_cutoff_ms'] = (int) env('TYPESENSE_SEARCH_CUTOFF_MS', 450);
|
||||
|
||||
if (array_key_exists('per_page', $options) && $options['per_page'] > 250) {
|
||||
$options['per_page'] = min($this->maxItemsPerPage, 250);
|
||||
@ -49,6 +49,7 @@ class TypeSenseScoutSearchService implements ScoutSearchService
|
||||
}
|
||||
|
||||
// if the model specifies search index sort order, use it
|
||||
// this is the default sort order for the model
|
||||
$sortByFields = $modelInstance->getSearchIndexSortBy();
|
||||
if (!is_null($sortByFields)) {
|
||||
$sortBy = "";
|
||||
@ -62,11 +63,6 @@ class TypeSenseScoutSearchService implements ScoutSearchService
|
||||
|
||||
// override ordering field
|
||||
if (!is_null($orderByField)) {
|
||||
$options['sort_by'] = "_text_match:desc,$orderByField:" . ($sortDirectionDescending ? "desc" : "asc");
|
||||
}
|
||||
|
||||
// if order_by is a user supplied value make it a priority over _text_match
|
||||
if ($orderByField !== 'members' && !is_null($orderByField)) {
|
||||
$options['sort_by'] = "$orderByField:" . ($sortDirectionDescending ? "desc" : "asc") . ",_text_match:desc";
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user