mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
refactored anime season endpoint handlers
This commit is contained in:
parent
37c273fbab
commit
525ac030f3
@ -2,12 +2,13 @@
|
||||
|
||||
namespace App\Features;
|
||||
|
||||
use App\Contracts\AnimeRepository;
|
||||
use App\Contracts\RequestHandler;
|
||||
use App\Dto\QueryAnimeSeasonCommand;
|
||||
use App\Enums\AnimeSeasonEnum;
|
||||
use App\Enums\AnimeTypeEnum;
|
||||
use App\Http\Resources\V4\AnimeCollection;
|
||||
use App\Support\CachedData;
|
||||
use Illuminate\Contracts\Database\Query\Builder;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
||||
@ -18,23 +19,14 @@ 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
|
||||
{
|
||||
/**
|
||||
* @var Carbon $from
|
||||
* @var Carbon $to
|
||||
*/
|
||||
[$from, $to] = $this->getSeasonRangeFrom($request);
|
||||
$type = collect($request->all())->has("filter") ? $request->filter : null;
|
||||
$results = $this->repository->getAiredBetween($from, $to, $type);
|
||||
$results = $this->getSeasonItems($request, $type);
|
||||
$results = $results->paginate($request->limit, ["*"], null, $request->page);
|
||||
|
||||
$animeCollection = new AnimeCollection($results);
|
||||
@ -45,9 +37,10 @@ abstract class QueryAnimeSeasonHandlerBase implements RequestHandler
|
||||
|
||||
/**
|
||||
* @param TRequest $request
|
||||
* @return array
|
||||
* @param ?AnimeTypeEnum $type
|
||||
* @return Builder
|
||||
*/
|
||||
protected abstract function getSeasonRangeFrom($request): array;
|
||||
protected abstract function getSeasonItems($request, ?AnimeTypeEnum $type): Builder;
|
||||
|
||||
protected function getSeasonRange(int $year, AnimeSeasonEnum $season): array
|
||||
{
|
||||
|
@ -2,17 +2,25 @@
|
||||
|
||||
namespace App\Features;
|
||||
|
||||
use App\Contracts\AnimeRepository;
|
||||
use App\Contracts\RequestHandler;
|
||||
use App\Dto\QueryCurrentAnimeSeasonCommand;
|
||||
use App\Enums\AnimeSeasonEnum;
|
||||
use App\Enums\AnimeTypeEnum;
|
||||
use Exception;
|
||||
use Illuminate\Contracts\Database\Query\Builder;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* @implements RequestHandler<QueryCurrentAnimeSeasonCommand, JsonResponse>
|
||||
*/
|
||||
final class QueryCurrentAnimeSeasonHandler extends QueryAnimeSeasonHandlerBase
|
||||
{
|
||||
public function __construct(private readonly AnimeRepository $repository)
|
||||
{
|
||||
}
|
||||
|
||||
public function requestClass(): string
|
||||
{
|
||||
return QueryCurrentAnimeSeasonCommand::class;
|
||||
@ -41,14 +49,14 @@ final class QueryCurrentAnimeSeasonHandler extends QueryAnimeSeasonHandlerBase
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function getSeasonRangeFrom($request): array
|
||||
protected function getSeasonItems($request, ?AnimeTypeEnum $type): Builder
|
||||
{
|
||||
/**
|
||||
* @var AnimeSeasonEnum $season
|
||||
* @var int $year
|
||||
*/
|
||||
[$season, $year] = $this->getCurrentSeason();
|
||||
|
||||
return $this->getSeasonRange($year, $season);
|
||||
/**
|
||||
* @var Carbon $from
|
||||
* @var Carbon $to
|
||||
*/
|
||||
[$to, $from] = $this->getSeasonRange($year, $season);
|
||||
return $this->repository->getAiredBetween($from, $to, $type);
|
||||
}
|
||||
}
|
||||
|
@ -2,23 +2,33 @@
|
||||
|
||||
namespace App\Features;
|
||||
|
||||
use App\Contracts\AnimeRepository;
|
||||
use App\Dto\QuerySpecificAnimeSeasonCommand;
|
||||
use App\Enums\AnimeSeasonEnum;
|
||||
use App\Enums\AnimeTypeEnum;
|
||||
use Illuminate\Contracts\Database\Query\Builder;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
||||
|
||||
/**
|
||||
* @extends QueryAnimeSeasonHandlerBase<QuerySpecificAnimeSeasonCommand>
|
||||
*/
|
||||
final class QuerySpecificAnimeSeasonHandler extends QueryAnimeSeasonHandlerBase
|
||||
{
|
||||
public function __construct(private readonly AnimeRepository $repository)
|
||||
{
|
||||
}
|
||||
|
||||
public function requestClass(): string
|
||||
{
|
||||
return QuerySpecificAnimeSeasonCommand::class;
|
||||
}
|
||||
|
||||
protected function getSeasonRangeFrom($request): array
|
||||
protected function getSeasonItems($request, ?AnimeTypeEnum $type): Builder
|
||||
{
|
||||
return $this->getSeasonRange($request->year, $request->season);
|
||||
/**
|
||||
* @var Carbon $from
|
||||
* @var Carbon $to
|
||||
*/
|
||||
[$to, $from] = $this->getSeasonRange($request->year, $request->season);
|
||||
return $this->repository->getAiredBetween($from, $to, $type);
|
||||
}
|
||||
}
|
||||
|
@ -3,35 +3,26 @@
|
||||
namespace App\Features;
|
||||
|
||||
use App\Contracts\AnimeRepository;
|
||||
use App\Contracts\RequestHandler;
|
||||
use App\Dto\QueryUpcomingAnimeSeasonCommand;
|
||||
use App\Http\Resources\V4\AnimeCollection;
|
||||
use App\Support\CachedData;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Enums\AnimeTypeEnum;
|
||||
use Illuminate\Contracts\Database\Query\Builder;
|
||||
|
||||
/**
|
||||
* @implements RequestHandler<QueryUpcomingAnimeSeasonCommand, JsonResponse>
|
||||
* @extends QueryAnimeSeasonHandlerBase<QueryUpcomingAnimeSeasonCommand>
|
||||
*/
|
||||
final class QueryUpcomingAnimeSeasonHandler implements RequestHandler
|
||||
final class QueryUpcomingAnimeSeasonHandler extends QueryAnimeSeasonHandlerBase
|
||||
{
|
||||
public function __construct(protected readonly AnimeRepository $repository)
|
||||
public function __construct(private readonly AnimeRepository $repository)
|
||||
{
|
||||
}
|
||||
|
||||
public function handle($request): JsonResponse
|
||||
{
|
||||
$type = collect($request->all())->has("filter") ? $request->filter : null;
|
||||
$results = $this->repository->getUpcomingSeasonItems($type);
|
||||
$results = $results->paginate($request->limit, ["*"], null, $request->page);
|
||||
|
||||
$animeCollection = new AnimeCollection($results);
|
||||
$response = $animeCollection->response();
|
||||
|
||||
return $response->addJikanCacheFlags($request->getFingerPrint(), CachedData::from($animeCollection->collection));
|
||||
}
|
||||
|
||||
public function requestClass(): string
|
||||
{
|
||||
return QueryUpcomingAnimeSeasonCommand::class;
|
||||
}
|
||||
|
||||
protected function getSeasonItems($request, ?AnimeTypeEnum $type): Builder
|
||||
{
|
||||
return $this->repository->getUpcomingSeasonItems($type);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user