From 1597c924283262b6109ae2bd6f6e6552cbb9082c Mon Sep 17 00:00:00 2001 From: Irfan Date: Sun, 10 Nov 2024 00:22:53 +0500 Subject: [PATCH 01/11] poc --- app/Dto/Concerns/PreparesData.php | 9 +++++- app/Dto/QueryRandomAnimeCommand.php | 10 ++++-- app/Features/QueryRandomAnimeHandler.php | 18 ++++++++--- app/Http/Resources/V4/AnimeCollection.php | 38 +++++++++++++++-------- 4 files changed, 54 insertions(+), 21 deletions(-) diff --git a/app/Dto/Concerns/PreparesData.php b/app/Dto/Concerns/PreparesData.php index 10ef1d1a..e2f32f96 100644 --- a/app/Dto/Concerns/PreparesData.php +++ b/app/Dto/Concerns/PreparesData.php @@ -16,7 +16,14 @@ trait PreparesData public static function prepareForPipeline(Collection $properties): Collection { // let's always set the limit parameter to the globally configured default value - if (property_exists(static::class, "limit") && !$properties->has("limit")) { + // // commented out to prevent override for backwards compatibility in /random + //if (property_exists(static::class, "limit") && !$properties->has("limit")) { + // $properties->put("limit", max_results_per_page( + // property_exists(static::class, "defaultLimit") ? static::$defaultLimit : null)); + //} + + // apply defaultLimit for endpoints that have `limit` supplied + if (property_exists(static::class, "limit") && $properties->has("limit")) { $properties->put("limit", max_results_per_page( property_exists(static::class, "defaultLimit") ? static::$defaultLimit : null)); } diff --git a/app/Dto/QueryRandomAnimeCommand.php b/app/Dto/QueryRandomAnimeCommand.php index 023ac9ed..82ca1382 100644 --- a/app/Dto/QueryRandomAnimeCommand.php +++ b/app/Dto/QueryRandomAnimeCommand.php @@ -3,15 +3,21 @@ namespace App\Dto; use App\Contracts\DataRequest; +use App\Dto\Concerns\HasLimitParameter; use App\Dto\Concerns\HasSfwParameter; use App\Dto\Concerns\HasUnapprovedParameter; +use App\Http\Resources\V4\AnimeCollection; use App\Http\Resources\V4\AnimeResource; use Spatie\LaravelData\Data; /** - * @implements DataRequest + * @implements DataRequest */ final class QueryRandomAnimeCommand extends Data implements DataRequest { - use HasSfwParameter, HasUnapprovedParameter; + use HasSfwParameter, + HasUnapprovedParameter, + HasLimitParameter; + + protected static int $defaultLimit = 5; } diff --git a/app/Features/QueryRandomAnimeHandler.php b/app/Features/QueryRandomAnimeHandler.php index 1ad4abbc..8a9073d0 100644 --- a/app/Features/QueryRandomAnimeHandler.php +++ b/app/Features/QueryRandomAnimeHandler.php @@ -3,30 +3,38 @@ namespace App\Features; use App\Anime; +use App\Contracts\AnimeRepository; use App\Contracts\RequestHandler; use App\Dto\QueryRandomAnimeCommand; +use App\Http\Resources\V4\AnimeCollection; use App\Http\Resources\V4\AnimeResource; +use App\Services\QueryBuilderPaginatorService; +use MongoDB\Collection; use Spatie\LaravelData\Optional; +use function Amp\Iterator\merge; /** - * @implements RequestHandler + * @implements RequestHandler */ final class QueryRandomAnimeHandler implements RequestHandler { /** * @inheritDoc */ - public function handle($request): AnimeResource + public function handle($request): AnimeResource|AnimeCollection { $queryable = Anime::query(); $o = Optional::create(); $sfwParam = $request->sfw === $o ? false : $request->sfw; $unapprovedParam = $request->unapproved === $o ? false : $request->unapproved; + $limit = $request->limit instanceof $o ? 1 : $request->limit; - return new AnimeResource( - $queryable->random(1, $sfwParam, $unapprovedParam)->first() - ); + $results = $queryable->random($limit, $sfwParam, $unapprovedParam); + + return $results->count() === 1 + ? new AnimeResource($results->first()) + : new AnimeCollection($results, false); } /** diff --git a/app/Http/Resources/V4/AnimeCollection.php b/app/Http/Resources/V4/AnimeCollection.php index 12ddfcb4..d1109fe5 100644 --- a/app/Http/Resources/V4/AnimeCollection.php +++ b/app/Http/Resources/V4/AnimeCollection.php @@ -37,20 +37,26 @@ class AnimeCollection extends ResourceCollection private $pagination; - public function __construct($resource) + public function __construct($resource, bool $paginated = true) { - $this->pagination = [ - 'last_visible_page' => $resource->lastPage(), - 'has_next_page' => $resource->hasMorePages(), - 'current_page' => $resource->currentPage(), - 'items' => [ - 'count' => $resource->count(), - 'total' => $resource->total(), - 'per_page' => $resource->perPage(), - ], - ]; + if ($paginated) { + $this->pagination = [ + 'last_visible_page' => $resource->lastPage(), + 'has_next_page' => $resource->hasMorePages(), + 'current_page' => $resource->currentPage(), + 'items' => [ + 'count' => $resource->count(), + 'total' => $resource->total(), + 'per_page' => $resource->perPage(), + ], + ]; + + $this->collection = $resource->getCollection(); + } - $this->collection = $resource->getCollection(); + if (!$paginated) { + $this->collection = $resource; + } parent::__construct($resource); } @@ -63,6 +69,12 @@ public function __construct($resource) */ public function toArray($request) { + if ($this->pagination === null) { + return [ + 'data' => $this->collection + ]; + } + return [ 'pagination' => $this->pagination, 'data' => $this->collection @@ -75,4 +87,4 @@ public function withResponse($request, $response) unset($jsonResponse['links'],$jsonResponse['meta']); $response->setContent(json_encode($jsonResponse)); } -} \ No newline at end of file +} From aae5512fd6cb50991d4e61e202eeb98928323932 Mon Sep 17 00:00:00 2001 From: Irfan Date: Sun, 10 Nov 2024 01:41:42 +0500 Subject: [PATCH 02/11] allow max limit override --- app/Rules/Attributes/MaxLimitWithFallback.php | 4 ++-- app/Rules/MaxResultsPerPageRule.php | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/Rules/Attributes/MaxLimitWithFallback.php b/app/Rules/Attributes/MaxLimitWithFallback.php index 15606262..c1bf75c3 100644 --- a/app/Rules/Attributes/MaxLimitWithFallback.php +++ b/app/Rules/Attributes/MaxLimitWithFallback.php @@ -9,8 +9,8 @@ #[Attribute(Attribute::TARGET_PROPERTY)] final class MaxLimitWithFallback extends Rule { - public function __construct() + public function __construct(?int $fallbackLimit = null) { - parent::__construct(new MaxResultsPerPageRule(max_results_per_page())); + parent::__construct(new MaxResultsPerPageRule($fallbackLimit ?? max_results_per_page())); } } diff --git a/app/Rules/MaxResultsPerPageRule.php b/app/Rules/MaxResultsPerPageRule.php index bac6ccda..f1b2217a 100644 --- a/app/Rules/MaxResultsPerPageRule.php +++ b/app/Rules/MaxResultsPerPageRule.php @@ -9,14 +9,14 @@ final class MaxResultsPerPageRule implements Rule private mixed $value; private int $fallbackLimit; - public function __construct($fallbackLimit = 25) + public function __construct(?int $fallbackLimit = null) { - $this->fallbackLimit = $fallbackLimit; + $this->fallbackLimit = $fallbackLimit ?? max_results_per_page(); } public function passes($attribute, $value): bool { - $this->value = $value; + $this->value = $value; // $value is being override to 25 here if (!is_numeric($value)) { return false; @@ -26,7 +26,7 @@ public function passes($attribute, $value): bool $value = intval($value); } - if ($value > max_results_per_page()) { + if ($value > $this->fallbackLimit) { return false; } @@ -36,6 +36,6 @@ public function passes($attribute, $value): bool public function message(): array|string { $mrpp = max_results_per_page($this->fallbackLimit); - return "Value {$this->value} is higher than the configured '$mrpp' max value."; + return "Value {$this->value} is higher than the configured {$this->fallbackLimit} max value."; } } From 41a30915e4193d4c29390264b9500ba6ab857073 Mon Sep 17 00:00:00 2001 From: Irfan Date: Sun, 10 Nov 2024 01:42:00 +0500 Subject: [PATCH 03/11] remove unused random code --- app/Contracts/Repository.php | 2 -- app/Repositories/DatabaseRepository.php | 5 ----- 2 files changed, 7 deletions(-) diff --git a/app/Contracts/Repository.php b/app/Contracts/Repository.php index d198cc0c..91ddd1d6 100644 --- a/app/Contracts/Repository.php +++ b/app/Contracts/Repository.php @@ -33,6 +33,4 @@ public function tableName(): string; public function scrape(int|string $id): array; public function insert(array $attributes): bool; - - public function random(int $numberOfRandomItems = 1): Collection; } diff --git a/app/Repositories/DatabaseRepository.php b/app/Repositories/DatabaseRepository.php index 200b59e8..844cedd6 100644 --- a/app/Repositories/DatabaseRepository.php +++ b/app/Repositories/DatabaseRepository.php @@ -74,11 +74,6 @@ public function insert(array $attributes): bool return true; } - public function random(int $numberOfRandomItems = 1): Collection - { - return $this->queryable(true)->random($numberOfRandomItems); - } - private function getModelClass(): string { return get_class($this->queryable(true)->newModelInstance()); From ff2c1c98033bcab37a7217616c12b2eedccb8492 Mon Sep 17 00:00:00 2001 From: Irfan Date: Sun, 10 Nov 2024 01:42:11 +0500 Subject: [PATCH 04/11] Create HasLimitParameterWithSmallerMax.php --- .../HasLimitParameterWithSmallerMax.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 app/Dto/Concerns/HasLimitParameterWithSmallerMax.php diff --git a/app/Dto/Concerns/HasLimitParameterWithSmallerMax.php b/app/Dto/Concerns/HasLimitParameterWithSmallerMax.php new file mode 100644 index 00000000..c964a8a0 --- /dev/null +++ b/app/Dto/Concerns/HasLimitParameterWithSmallerMax.php @@ -0,0 +1,25 @@ + Date: Sun, 10 Nov 2024 01:43:11 +0500 Subject: [PATCH 05/11] adds `limit` to random endpoints --- app/Dto/Concerns/PreparesData.php | 18 ++++----- app/Dto/QueryRandomAnimeCommand.php | 6 +-- app/Dto/QueryRandomCharacterCommand.php | 2 + app/Dto/QueryRandomMangaCommand.php | 8 +++- app/Dto/QueryRandomPersonCommand.php | 5 ++- app/Dto/QueryRandomUserCommand.php | 2 + app/Features/QueryRandomAnimeHandler.php | 11 ++--- app/Features/QueryRandomCharacterHandler.php | 32 +++++++++------ app/Features/QueryRandomItemHandler.php | 34 ---------------- app/Features/QueryRandomMangaHandler.php | 19 +++++---- app/Features/QueryRandomPersonHandler.php | 32 +++++++++------ app/Features/QueryRandomUserHandler.php | 31 ++++++++------ app/Http/Resources/V4/AnimeCollection.php | 2 +- app/Http/Resources/V4/CharacterCollection.php | 40 ++++++++++++------- app/Http/Resources/V4/MangaCollection.php | 40 ++++++++++++------- app/Http/Resources/V4/PersonCollection.php | 38 ++++++++++++------ app/Http/Resources/V4/UserCollection.php | 2 +- 17 files changed, 174 insertions(+), 148 deletions(-) delete mode 100644 app/Features/QueryRandomItemHandler.php diff --git a/app/Dto/Concerns/PreparesData.php b/app/Dto/Concerns/PreparesData.php index e2f32f96..7aaad1b9 100644 --- a/app/Dto/Concerns/PreparesData.php +++ b/app/Dto/Concerns/PreparesData.php @@ -16,17 +16,13 @@ trait PreparesData public static function prepareForPipeline(Collection $properties): Collection { // let's always set the limit parameter to the globally configured default value - // // commented out to prevent override for backwards compatibility in /random - //if (property_exists(static::class, "limit") && !$properties->has("limit")) { - // $properties->put("limit", max_results_per_page( - // property_exists(static::class, "defaultLimit") ? static::$defaultLimit : null)); - //} - - // apply defaultLimit for endpoints that have `limit` supplied - if (property_exists(static::class, "limit") && $properties->has("limit")) { - $properties->put("limit", max_results_per_page( - property_exists(static::class, "defaultLimit") ? static::$defaultLimit : null)); - } + // // BUG: this causes override and always sets the default limit to config value + // even if the property `limit` does not exist + // max_results_per_page never accepts $defaultLimit and will always return config value + // if (property_exists(static::class, "limit") && !$properties->has("limit")) { + // $properties->put("limit", max_results_per_page( + // property_exists(static::class, "defaultLimit") ? static::$defaultLimit : null)); + // } // we want to cast "true" and "false" string values to boolean before validation, so let's take all properties // of the class which are bool or bool|Optional type, and using their name read the values from the incoming diff --git a/app/Dto/QueryRandomAnimeCommand.php b/app/Dto/QueryRandomAnimeCommand.php index 82ca1382..bfc3e63e 100644 --- a/app/Dto/QueryRandomAnimeCommand.php +++ b/app/Dto/QueryRandomAnimeCommand.php @@ -3,7 +3,7 @@ namespace App\Dto; use App\Contracts\DataRequest; -use App\Dto\Concerns\HasLimitParameter; +use App\Dto\Concerns\HasLimitParameterWithSmallerMax; use App\Dto\Concerns\HasSfwParameter; use App\Dto\Concerns\HasUnapprovedParameter; use App\Http\Resources\V4\AnimeCollection; @@ -17,7 +17,5 @@ final class QueryRandomAnimeCommand extends Data implements DataRequest { use HasSfwParameter, HasUnapprovedParameter, - HasLimitParameter; - - protected static int $defaultLimit = 5; + HasLimitParameterWithSmallerMax; } diff --git a/app/Dto/QueryRandomCharacterCommand.php b/app/Dto/QueryRandomCharacterCommand.php index ff71e1d4..b956aec3 100644 --- a/app/Dto/QueryRandomCharacterCommand.php +++ b/app/Dto/QueryRandomCharacterCommand.php @@ -3,6 +3,7 @@ namespace App\Dto; use App\Contracts\DataRequest; +use App\Dto\Concerns\HasLimitParameterWithSmallerMax; use App\Http\Resources\V4\CharacterResource; use Spatie\LaravelData\Data; @@ -11,4 +12,5 @@ */ final class QueryRandomCharacterCommand extends Data implements DataRequest { + use HasLimitParameterWithSmallerMax; } diff --git a/app/Dto/QueryRandomMangaCommand.php b/app/Dto/QueryRandomMangaCommand.php index e24b158c..8e5035de 100644 --- a/app/Dto/QueryRandomMangaCommand.php +++ b/app/Dto/QueryRandomMangaCommand.php @@ -3,15 +3,19 @@ namespace App\Dto; use App\Contracts\DataRequest; +use App\Dto\Concerns\HasLimitParameterWithSmallerMax; use App\Dto\Concerns\HasSfwParameter; use App\Dto\Concerns\HasUnapprovedParameter; +use App\Http\Resources\V4\MangaCollection; use App\Http\Resources\V4\MangaResource; use Spatie\LaravelData\Data; /** - * @implements DataRequest + * @implements DataRequest */ final class QueryRandomMangaCommand extends Data implements DataRequest { - use HasSfwParameter, HasUnapprovedParameter; + use HasSfwParameter, + HasUnapprovedParameter, + HasLimitParameterWithSmallerMax; } diff --git a/app/Dto/QueryRandomPersonCommand.php b/app/Dto/QueryRandomPersonCommand.php index 10e07fd3..1b1a6f79 100644 --- a/app/Dto/QueryRandomPersonCommand.php +++ b/app/Dto/QueryRandomPersonCommand.php @@ -3,12 +3,15 @@ namespace App\Dto; use App\Contracts\DataRequest; +use App\Dto\Concerns\HasLimitParameterWithSmallerMax; +use App\Http\Resources\V4\PersonCollection; use App\Http\Resources\V4\PersonResource; use Spatie\LaravelData\Data; /** - * @implements DataRequest + * @implements DataRequest */ final class QueryRandomPersonCommand extends Data implements DataRequest { + use HasLimitParameterWithSmallerMax; } diff --git a/app/Dto/QueryRandomUserCommand.php b/app/Dto/QueryRandomUserCommand.php index 7a39bcfc..9f6cc08d 100644 --- a/app/Dto/QueryRandomUserCommand.php +++ b/app/Dto/QueryRandomUserCommand.php @@ -3,6 +3,7 @@ namespace App\Dto; use App\Contracts\DataRequest; +use App\Dto\Concerns\HasLimitParameterWithSmallerMax; use App\Http\Resources\V4\ProfileResource; use Spatie\LaravelData\Data; @@ -11,4 +12,5 @@ */ final class QueryRandomUserCommand extends Data implements DataRequest { + use HasLimitParameterWithSmallerMax; } diff --git a/app/Features/QueryRandomAnimeHandler.php b/app/Features/QueryRandomAnimeHandler.php index 8a9073d0..a72b0b95 100644 --- a/app/Features/QueryRandomAnimeHandler.php +++ b/app/Features/QueryRandomAnimeHandler.php @@ -3,15 +3,11 @@ namespace App\Features; use App\Anime; -use App\Contracts\AnimeRepository; use App\Contracts\RequestHandler; use App\Dto\QueryRandomAnimeCommand; use App\Http\Resources\V4\AnimeCollection; use App\Http\Resources\V4\AnimeResource; -use App\Services\QueryBuilderPaginatorService; -use MongoDB\Collection; use Spatie\LaravelData\Optional; -use function Amp\Iterator\merge; /** * @implements RequestHandler @@ -25,10 +21,9 @@ public function handle($request): AnimeResource|AnimeCollection { $queryable = Anime::query(); - $o = Optional::create(); - $sfwParam = $request->sfw === $o ? false : $request->sfw; - $unapprovedParam = $request->unapproved === $o ? false : $request->unapproved; - $limit = $request->limit instanceof $o ? 1 : $request->limit; + $sfwParam = $request->sfw instanceof Optional ? false : $request->sfw; + $unapprovedParam = $request->unapproved instanceof Optional ? false : $request->unapproved; + $limit = $request->limit instanceof Optional ? 1 : $request->limit; $results = $queryable->random($limit, $sfwParam, $unapprovedParam); diff --git a/app/Features/QueryRandomCharacterHandler.php b/app/Features/QueryRandomCharacterHandler.php index c66a2cdf..c9e597fb 100644 --- a/app/Features/QueryRandomCharacterHandler.php +++ b/app/Features/QueryRandomCharacterHandler.php @@ -2,20 +2,33 @@ namespace App\Features; -use App\Contracts\CharacterRepository; +use App\Character; +use App\Contracts\RequestHandler; use App\Dto\QueryRandomCharacterCommand; +use App\Http\Resources\V4\CharacterCollection; use App\Http\Resources\V4\CharacterResource; -use Illuminate\Http\Resources\Json\JsonResource; -use Illuminate\Support\Collection; +use Spatie\LaravelData\Optional; /** - * @extends QueryRandomItemHandler + * @extends QueryRandomCharacterHandler */ -final class QueryRandomCharacterHandler extends QueryRandomItemHandler +final class QueryRandomCharacterHandler implements RequestHandler { - public function __construct(CharacterRepository $repository) + + /** + * @inheritDoc + */ + public function handle($request): CharacterResource|CharacterCollection { - parent::__construct($repository); + $queryable = Character::query(); + + $limit = $request->limit instanceof Optional ? 1 : $request->limit; + + $results = $queryable->random($limit); + + return $results->count() === 1 + ? new CharacterResource($results->first()) + : new CharacterCollection($results, false); } /** @@ -25,9 +38,4 @@ public function requestClass(): string { return QueryRandomCharacterCommand::class; } - - protected function resource(Collection $results): JsonResource - { - return new CharacterResource($results->first()); - } } diff --git a/app/Features/QueryRandomItemHandler.php b/app/Features/QueryRandomItemHandler.php deleted file mode 100644 index 6db9e1f2..00000000 --- a/app/Features/QueryRandomItemHandler.php +++ /dev/null @@ -1,34 +0,0 @@ - - */ -abstract class QueryRandomItemHandler implements RequestHandler -{ - protected function __construct(protected readonly Repository $repository) - { - } - - /** - * @inheritDoc - */ - public function handle($request) - { - $results = $this->repository->random(); - return $this->resource($results); - } - - protected abstract function resource(Collection $results): JsonResource; -} diff --git a/app/Features/QueryRandomMangaHandler.php b/app/Features/QueryRandomMangaHandler.php index f4864575..bf013cc8 100644 --- a/app/Features/QueryRandomMangaHandler.php +++ b/app/Features/QueryRandomMangaHandler.php @@ -4,29 +4,32 @@ use App\Contracts\RequestHandler; use App\Dto\QueryRandomMangaCommand; +use App\Http\Resources\V4\MangaCollection; use App\Http\Resources\V4\MangaResource; use App\Manga; use Spatie\LaravelData\Optional; /** - * @implements RequestHandler + * @implements RequestHandler */ final class QueryRandomMangaHandler implements RequestHandler { /** * @inheritDoc */ - public function handle($request) + public function handle($request): MangaResource|MangaCollection { $queryable = Manga::query(); - $o = Optional::create(); - $sfwParam = $request->sfw === $o ? false : $request->sfw; - $unapprovedParam = $request->unapproved === $o ? false : $request->unapproved; + $sfwParam = $request->sfw instanceof Optional ? false : $request->sfw; + $unapprovedParam = $request->unapproved instanceof Optional ? false : $request->unapproved; + $limit = $request->limit instanceof Optional ? 1 : $request->limit; - return new MangaResource( - $queryable->random(1, $sfwParam, $unapprovedParam)->first() - ); + $results = $queryable->random($limit, $sfwParam, $unapprovedParam); + + return $results->count() === 1 + ? new MangaResource($results->first()) + : new MangaCollection($results, false); } /** diff --git a/app/Features/QueryRandomPersonHandler.php b/app/Features/QueryRandomPersonHandler.php index 80a4daf0..045652a6 100644 --- a/app/Features/QueryRandomPersonHandler.php +++ b/app/Features/QueryRandomPersonHandler.php @@ -2,20 +2,33 @@ namespace App\Features; -use App\Contracts\PeopleRepository; +use App\Contracts\RequestHandler; use App\Dto\QueryRandomPersonCommand; +use App\Http\Resources\V4\PersonCollection; use App\Http\Resources\V4\PersonResource; -use Illuminate\Http\Resources\Json\JsonResource; -use Illuminate\Support\Collection; +use App\Person; +use Spatie\LaravelData\Optional; /** - * @extends QueryRandomItemHandler + * @extends RequestHandler */ -final class QueryRandomPersonHandler extends QueryRandomItemHandler +final class QueryRandomPersonHandler implements RequestHandler { - public function __construct(PeopleRepository $repository) + + /** + * @inheritDoc + */ + public function handle($request): PersonResource|PersonCollection { - parent::__construct($repository); + $queryable = Person::query(); + + $limit = $request->limit instanceof Optional ? 1 : $request->limit; + + $results = $queryable->random($limit); + + return $results->count() === 1 + ? new PersonResource($results->first()) + : new PersonCollection($results, false); } /** @@ -25,9 +38,4 @@ public function requestClass(): string { return QueryRandomPersonCommand::class; } - - protected function resource(Collection $results): JsonResource - { - return new PersonResource($results->first()); - } } diff --git a/app/Features/QueryRandomUserHandler.php b/app/Features/QueryRandomUserHandler.php index 2e29c95b..b1035622 100644 --- a/app/Features/QueryRandomUserHandler.php +++ b/app/Features/QueryRandomUserHandler.php @@ -2,27 +2,32 @@ namespace App\Features; -use App\Contracts\UserRepository; +use App\Contracts\RequestHandler; use App\Dto\QueryRandomUserCommand; use App\Http\Resources\V4\ProfileResource; -use Illuminate\Http\Resources\Json\JsonResource; -use Illuminate\Support\Collection; +use App\Http\Resources\V4\UserCollection; +use App\Profile; +use Spatie\LaravelData\Optional; /** - * @extends QueryRandomItemHandler + * @extends RequestHandler */ -final class QueryRandomUserHandler extends QueryRandomItemHandler +final class QueryRandomUserHandler implements RequestHandler { - public function __construct(UserRepository $repository) + /** + * @inheritDoc + */ + public function handle($request): ProfileResource|UserCollection { - parent::__construct($repository); - } + $queryable = Profile::query(); - protected function resource(Collection $results): JsonResource - { - return new ProfileResource( - $results->first() - ); + $limit = $request->limit instanceof Optional ? 1 : $request->limit; + + $results = $queryable->random($limit); + + return $results->count() === 1 + ? new ProfileResource($results->first()) + : new UserCollection($results, false); } /** diff --git a/app/Http/Resources/V4/AnimeCollection.php b/app/Http/Resources/V4/AnimeCollection.php index d1109fe5..334d8cda 100644 --- a/app/Http/Resources/V4/AnimeCollection.php +++ b/app/Http/Resources/V4/AnimeCollection.php @@ -35,7 +35,7 @@ class AnimeCollection extends ResourceCollection */ public $collects = 'App\Http\Resources\V4\AnimeResource'; - private $pagination; + private ?array $pagination = null; public function __construct($resource, bool $paginated = true) { diff --git a/app/Http/Resources/V4/CharacterCollection.php b/app/Http/Resources/V4/CharacterCollection.php index d7539ad3..0ff8cc6a 100644 --- a/app/Http/Resources/V4/CharacterCollection.php +++ b/app/Http/Resources/V4/CharacterCollection.php @@ -35,22 +35,28 @@ class CharacterCollection extends ResourceCollection */ public $collects = 'App\Http\Resources\V4\CharacterResource'; - private $pagination; + private ?array $pagination = null; - public function __construct(LengthAwarePaginator $resource) + public function __construct($resource, bool $paginated = true) { - $this->pagination = [ - 'last_visible_page' => $resource->lastPage(), - 'has_next_page' => $resource->hasMorePages(), - 'current_page' => $resource->currentPage(), - 'items' => [ - 'count' => $resource->count(), - 'total' => $resource->total(), - 'per_page' => $resource->perPage(), - ], - ]; + if ($paginated) { + $this->pagination = [ + 'last_visible_page' => $resource->lastPage(), + 'has_next_page' => $resource->hasMorePages(), + 'current_page' => $resource->currentPage(), + 'items' => [ + 'count' => $resource->count(), + 'total' => $resource->total(), + 'per_page' => $resource->perPage(), + ], + ]; + + $this->collection = $resource->getCollection(); + } - $this->collection = $resource->getCollection(); + if (!$paginated) { + $this->collection = $resource; + } parent::__construct($resource); } @@ -63,6 +69,12 @@ public function __construct(LengthAwarePaginator $resource) */ public function toArray($request) { + if ($this->pagination === null) { + return [ + 'data' => $this->collection + ]; + } + return [ 'pagination' => $this->pagination, 'data' => $this->collection @@ -75,4 +87,4 @@ public function withResponse($request, $response) unset($jsonResponse['links'],$jsonResponse['meta']); $response->setContent(json_encode($jsonResponse)); } -} \ No newline at end of file +} diff --git a/app/Http/Resources/V4/MangaCollection.php b/app/Http/Resources/V4/MangaCollection.php index 99c6cadb..82109f7c 100644 --- a/app/Http/Resources/V4/MangaCollection.php +++ b/app/Http/Resources/V4/MangaCollection.php @@ -36,22 +36,28 @@ class MangaCollection extends ResourceCollection */ public $collects = 'App\Http\Resources\V4\MangaResource'; - private $pagination; + private ?array $pagination = null; - public function __construct($resource) + public function __construct($resource, bool $paginated = true) { - $this->pagination = [ - 'last_visible_page' => $resource->lastPage(), - 'has_next_page' => $resource->hasMorePages(), - 'current_page' => $resource->currentPage(), - 'items' => [ - 'count' => $resource->count(), - 'total' => $resource->total(), - 'per_page' => $resource->perPage(), - ], - ]; + if ($paginated) { + $this->pagination = [ + 'last_visible_page' => $resource->lastPage(), + 'has_next_page' => $resource->hasMorePages(), + 'current_page' => $resource->currentPage(), + 'items' => [ + 'count' => $resource->count(), + 'total' => $resource->total(), + 'per_page' => $resource->perPage(), + ], + ]; + + $this->collection = $resource->getCollection(); + } - $this->collection = $resource->getCollection(); + if (!$paginated) { + $this->collection = $resource; + } parent::__construct($resource); } @@ -64,6 +70,12 @@ public function __construct($resource) */ public function toArray($request) { + if ($this->pagination === null) { + return [ + 'data' => $this->collection + ]; + } + return [ 'pagination' => $this->pagination, 'data' => $this->collection @@ -76,4 +88,4 @@ public function withResponse($request, $response) unset($jsonResponse['links'], $jsonResponse['meta']); $response->setContent(json_encode($jsonResponse)); } -} \ No newline at end of file +} diff --git a/app/Http/Resources/V4/PersonCollection.php b/app/Http/Resources/V4/PersonCollection.php index b23451f8..2e642999 100644 --- a/app/Http/Resources/V4/PersonCollection.php +++ b/app/Http/Resources/V4/PersonCollection.php @@ -37,22 +37,28 @@ class PersonCollection extends ResourceCollection */ public $collects = 'App\Http\Resources\V4\PersonResource'; - private $pagination; + private ?array $pagination = null; - public function __construct(LengthAwarePaginator $resource) + public function __construct($resource, bool $paginated = true) { - $this->pagination = [ - 'last_visible_page' => $resource->lastPage(), - 'has_next_page' => $resource->hasMorePages(), - 'current_page' => $resource->currentPage(), - 'items' => [ - 'count' => $resource->count(), - 'total' => $resource->total(), - 'per_page' => $resource->perPage(), - ], - ]; + if ($paginated) { + $this->pagination = [ + 'last_visible_page' => $resource->lastPage(), + 'has_next_page' => $resource->hasMorePages(), + 'current_page' => $resource->currentPage(), + 'items' => [ + 'count' => $resource->count(), + 'total' => $resource->total(), + 'per_page' => $resource->perPage(), + ], + ]; + + $this->collection = $resource->getCollection(); + } - $this->collection = $resource->getCollection(); + if (!$paginated) { + $this->collection = $resource; + } parent::__construct($resource); } @@ -65,6 +71,12 @@ public function __construct(LengthAwarePaginator $resource) */ public function toArray($request) { + if ($this->pagination === null) { + return [ + 'data' => $this->collection + ]; + } + return [ 'pagination' => $this->pagination, 'data' => $this->collection diff --git a/app/Http/Resources/V4/UserCollection.php b/app/Http/Resources/V4/UserCollection.php index b7f90a87..cf05b5e5 100644 --- a/app/Http/Resources/V4/UserCollection.php +++ b/app/Http/Resources/V4/UserCollection.php @@ -22,4 +22,4 @@ public function toArray($request) 'data' => $this->collection ]; } -} \ No newline at end of file +} From 6b18a36f15152a498013e012d4b5aa3cfbb07c75 Mon Sep 17 00:00:00 2001 From: Irfan Date: Sun, 10 Nov 2024 01:43:26 +0500 Subject: [PATCH 06/11] removes $defaultLimit usage in DTO --- app/Dto/QuerySpecificAnimeSeasonCommand.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/Dto/QuerySpecificAnimeSeasonCommand.php b/app/Dto/QuerySpecificAnimeSeasonCommand.php index 7a94899d..40761cb3 100644 --- a/app/Dto/QuerySpecificAnimeSeasonCommand.php +++ b/app/Dto/QuerySpecificAnimeSeasonCommand.php @@ -24,8 +24,6 @@ final class QuerySpecificAnimeSeasonCommand extends QueryAnimeSeasonCommand #[WithCast(EnumCast::class, AnimeSeasonEnum::class), EnumValidation(AnimeSeasonEnum::class)] public AnimeSeasonEnum $season; - protected static int $defaultLimit = 30; - public static function messages(...$args): array { return [ From 3b59908e4d43a74800c50485c6e7178e636ab3bb Mon Sep 17 00:00:00 2001 From: Irfan Date: Sun, 10 Nov 2024 02:26:53 +0500 Subject: [PATCH 07/11] random: update docs --- app/Dto/Concerns/HasLimitParameter.php | 2 + .../HasLimitParameterWithSmallerMax.php | 8 ---- .../Controllers/V4DB/RandomController.php | 20 +++++++--- storage/api-docs/api-docs.json | 37 ++++++++++++++++--- 4 files changed, 49 insertions(+), 18 deletions(-) diff --git a/app/Dto/Concerns/HasLimitParameter.php b/app/Dto/Concerns/HasLimitParameter.php index eb0b119e..2a642bca 100644 --- a/app/Dto/Concerns/HasLimitParameter.php +++ b/app/Dto/Concerns/HasLimitParameter.php @@ -13,6 +13,8 @@ * @OA\Parameter( * name="limit", * in="query", + * required=false, + * description="Maximum limit (and the default number of entries returned) is 25 for all endpoints except for Random endpoints where the maximum limit is 5 and the default number of entries returned is 1.", * @OA\Schema(type="integer") * ), */ diff --git a/app/Dto/Concerns/HasLimitParameterWithSmallerMax.php b/app/Dto/Concerns/HasLimitParameterWithSmallerMax.php index c964a8a0..fe95f165 100644 --- a/app/Dto/Concerns/HasLimitParameterWithSmallerMax.php +++ b/app/Dto/Concerns/HasLimitParameterWithSmallerMax.php @@ -8,14 +8,6 @@ use Spatie\LaravelData\Attributes\Validation\Min; use Spatie\LaravelData\Optional; - -/** - * @OA\Parameter( - * name="limit", - * in="query", - * @OA\Schema(type="integer") - * ), - */ trait HasLimitParameterWithSmallerMax { use PreparesData; diff --git a/app/Http/Controllers/V4DB/RandomController.php b/app/Http/Controllers/V4DB/RandomController.php index 826de4bd..5daf3606 100644 --- a/app/Http/Controllers/V4DB/RandomController.php +++ b/app/Http/Controllers/V4DB/RandomController.php @@ -40,9 +40,11 @@ class RandomController extends Controller * operationId="getRandomAnime", * tags={"random"}, * + * @OA\Parameter(ref="#/components/parameters/limit"), + * * @OA\Response( * response="200", - * description="Returns a random anime resource", + * description="Returns a single random anime resource or multiple resources in an array when `limit` is supplied", * @OA\JsonContent( * @OA\Property( * property="data", @@ -67,9 +69,11 @@ public function anime(QueryRandomAnimeCommand $command) * operationId="getRandomManga", * tags={"random"}, * + * @OA\Parameter(ref="#/components/parameters/limit"), + * * @OA\Response( * response="200", - * description="Returns a random manga resource", + * description="Returns a single random manga resource or multiple resources in an array when `limit` is supplied", * @OA\JsonContent( * @OA\Property( * property="data", @@ -94,9 +98,11 @@ public function manga(QueryRandomMangaCommand $command) * operationId="getRandomCharacters", * tags={"random"}, * + * @OA\Parameter(ref="#/components/parameters/limit"), + * * @OA\Response( * response="200", - * description="Returns a random character resource", + * description="Returns a single random character resource or multiple resources in an array when `limit` is supplied", * @OA\JsonContent( * @OA\Property( * property="data", @@ -121,9 +127,11 @@ public function characters(QueryRandomCharacterCommand $command) * operationId="getRandomPeople", * tags={"random"}, * + * @OA\Parameter(ref="#/components/parameters/limit"), + * * @OA\Response( * response="200", - * description="Returns a random person resource", + * description="Returns a single random person resource or multiple resources in an array when `limit` is supplied", * @OA\JsonContent( * @OA\Property( * property="data", @@ -148,9 +156,11 @@ public function people(QueryRandomPersonCommand $command) * operationId="getRandomUsers", * tags={"random"}, * + * @OA\Parameter(ref="#/components/parameters/limit"), + * * @OA\Response( * response="200", - * description="Returns a random user profile resource", + * description="Returns a single random user profile resource or multiple resources in an array when `limit` is supplied", * @OA\JsonContent( * @OA\Property( * property="data", diff --git a/storage/api-docs/api-docs.json b/storage/api-docs/api-docs.json index a8bc1b78..cc73d4cf 100644 --- a/storage/api-docs/api-docs.json +++ b/storage/api-docs/api-docs.json @@ -2022,9 +2022,14 @@ "random" ], "operationId": "getRandomAnime", + "parameters": [ + { + "$ref": "#/components/parameters/limit" + } + ], "responses": { "200": { - "description": "Returns a random anime resource", + "description": "Returns a single random anime resource or multiple resources in an array when `limit` is supplied", "content": { "application/json": { "schema": { @@ -2050,9 +2055,14 @@ "random" ], "operationId": "getRandomManga", + "parameters": [ + { + "$ref": "#/components/parameters/limit" + } + ], "responses": { "200": { - "description": "Returns a random manga resource", + "description": "Returns a single random manga resource or multiple resources in an array when `limit` is supplied", "content": { "application/json": { "schema": { @@ -2078,9 +2088,14 @@ "random" ], "operationId": "getRandomCharacters", + "parameters": [ + { + "$ref": "#/components/parameters/limit" + } + ], "responses": { "200": { - "description": "Returns a random character resource", + "description": "Returns a single random character resource or multiple resources in an array when `limit` is supplied", "content": { "application/json": { "schema": { @@ -2106,9 +2121,14 @@ "random" ], "operationId": "getRandomPeople", + "parameters": [ + { + "$ref": "#/components/parameters/limit" + } + ], "responses": { "200": { - "description": "Returns a random person resource", + "description": "Returns a single random person resource or multiple resources in an array when `limit` is supplied", "content": { "application/json": { "schema": { @@ -2134,9 +2154,14 @@ "random" ], "operationId": "getRandomUsers", + "parameters": [ + { + "$ref": "#/components/parameters/limit" + } + ], "responses": { "200": { - "description": "Returns a random user profile resource", + "description": "Returns a single random user profile resource or multiple resources in an array when `limit` is supplied", "content": { "application/json": { "schema": { @@ -9059,6 +9084,8 @@ "limit": { "name": "limit", "in": "query", + "description": "Maximum limit (and the default number of entries returned) is 25 for all endpoints except for Random endpoints where the maximum limit is 5 and the default number of entries returned is 1.", + "required": false, "schema": { "type": "integer" } From f20e40c138d979bb9fdb1042f24bd4a4c098005a Mon Sep 17 00:00:00 2001 From: Irfan Date: Wed, 13 Nov 2024 21:30:10 +0500 Subject: [PATCH 08/11] add new /random/list/* endpoints --- app/Dto/QueryRandomAnimeCommand.php | 7 +- app/Dto/QueryRandomAnimeListCommand.php | 20 ++ app/Dto/QueryRandomCharacterCommand.php | 1 - app/Dto/QueryRandomCharacterListCommand.php | 16 ++ app/Dto/QueryRandomMangaCommand.php | 7 +- app/Dto/QueryRandomMangaListCommand.php | 20 ++ app/Dto/QueryRandomPersonCommand.php | 5 +- app/Dto/QueryRandomPersonListCommand.php | 16 ++ app/Dto/QueryRandomUserCommand.php | 1 - app/Dto/QueryRandomUserListCommand.php | 16 ++ app/Features/QueryRandomAnimeHandler.php | 12 +- app/Features/QueryRandomAnimeListHandler.php | 40 ++++ app/Features/QueryRandomCharacterHandler.php | 20 +- .../QueryRandomCharacterListHandler.php | 35 ++++ app/Features/QueryRandomMangaHandler.php | 13 +- app/Features/QueryRandomMangaListHandler.php | 40 ++++ app/Features/QueryRandomPersonHandler.php | 20 +- app/Features/QueryRandomPersonListHandler.php | 37 ++++ app/Features/QueryRandomUserHandler.php | 15 +- app/Features/QueryRandomUserListHandler.php | 37 ++++ .../Controllers/V4DB/RandomController.php | 171 +++++++++++++++++- app/Http/Middleware/MicroCaching.php | 7 +- app/Providers/AppServiceProvider.php | 5 + routes/web.v4.php | 27 +++ 24 files changed, 515 insertions(+), 73 deletions(-) create mode 100644 app/Dto/QueryRandomAnimeListCommand.php create mode 100644 app/Dto/QueryRandomCharacterListCommand.php create mode 100644 app/Dto/QueryRandomMangaListCommand.php create mode 100644 app/Dto/QueryRandomPersonListCommand.php create mode 100644 app/Dto/QueryRandomUserListCommand.php create mode 100644 app/Features/QueryRandomAnimeListHandler.php create mode 100644 app/Features/QueryRandomCharacterListHandler.php create mode 100644 app/Features/QueryRandomMangaListHandler.php create mode 100644 app/Features/QueryRandomPersonListHandler.php create mode 100644 app/Features/QueryRandomUserListHandler.php diff --git a/app/Dto/QueryRandomAnimeCommand.php b/app/Dto/QueryRandomAnimeCommand.php index bfc3e63e..004908e8 100644 --- a/app/Dto/QueryRandomAnimeCommand.php +++ b/app/Dto/QueryRandomAnimeCommand.php @@ -3,19 +3,16 @@ namespace App\Dto; use App\Contracts\DataRequest; -use App\Dto\Concerns\HasLimitParameterWithSmallerMax; use App\Dto\Concerns\HasSfwParameter; use App\Dto\Concerns\HasUnapprovedParameter; -use App\Http\Resources\V4\AnimeCollection; use App\Http\Resources\V4\AnimeResource; use Spatie\LaravelData\Data; /** - * @implements DataRequest + * @implements DataRequest */ final class QueryRandomAnimeCommand extends Data implements DataRequest { use HasSfwParameter, - HasUnapprovedParameter, - HasLimitParameterWithSmallerMax; + HasUnapprovedParameter; } diff --git a/app/Dto/QueryRandomAnimeListCommand.php b/app/Dto/QueryRandomAnimeListCommand.php new file mode 100644 index 00000000..89249ec1 --- /dev/null +++ b/app/Dto/QueryRandomAnimeListCommand.php @@ -0,0 +1,20 @@ + + */ +final class QueryRandomAnimeListCommand extends Data implements DataRequest +{ + use HasSfwParameter, + HasUnapprovedParameter, + HasLimitParameterWithSmallerMax; +} diff --git a/app/Dto/QueryRandomCharacterCommand.php b/app/Dto/QueryRandomCharacterCommand.php index b956aec3..b2a5e747 100644 --- a/app/Dto/QueryRandomCharacterCommand.php +++ b/app/Dto/QueryRandomCharacterCommand.php @@ -12,5 +12,4 @@ */ final class QueryRandomCharacterCommand extends Data implements DataRequest { - use HasLimitParameterWithSmallerMax; } diff --git a/app/Dto/QueryRandomCharacterListCommand.php b/app/Dto/QueryRandomCharacterListCommand.php new file mode 100644 index 00000000..8bf0e9c0 --- /dev/null +++ b/app/Dto/QueryRandomCharacterListCommand.php @@ -0,0 +1,16 @@ + + */ +final class QueryRandomCharacterListCommand extends Data implements DataRequest +{ + use HasLimitParameterWithSmallerMax; +} diff --git a/app/Dto/QueryRandomMangaCommand.php b/app/Dto/QueryRandomMangaCommand.php index 8e5035de..ae1ce69b 100644 --- a/app/Dto/QueryRandomMangaCommand.php +++ b/app/Dto/QueryRandomMangaCommand.php @@ -3,19 +3,16 @@ namespace App\Dto; use App\Contracts\DataRequest; -use App\Dto\Concerns\HasLimitParameterWithSmallerMax; use App\Dto\Concerns\HasSfwParameter; use App\Dto\Concerns\HasUnapprovedParameter; -use App\Http\Resources\V4\MangaCollection; use App\Http\Resources\V4\MangaResource; use Spatie\LaravelData\Data; /** - * @implements DataRequest + * @implements DataRequest */ final class QueryRandomMangaCommand extends Data implements DataRequest { use HasSfwParameter, - HasUnapprovedParameter, - HasLimitParameterWithSmallerMax; + HasUnapprovedParameter; } diff --git a/app/Dto/QueryRandomMangaListCommand.php b/app/Dto/QueryRandomMangaListCommand.php new file mode 100644 index 00000000..e8c6e777 --- /dev/null +++ b/app/Dto/QueryRandomMangaListCommand.php @@ -0,0 +1,20 @@ + + */ +final class QueryRandomMangaListCommand extends Data implements DataRequest +{ + use HasSfwParameter, + HasUnapprovedParameter, + HasLimitParameterWithSmallerMax; +} diff --git a/app/Dto/QueryRandomPersonCommand.php b/app/Dto/QueryRandomPersonCommand.php index 1b1a6f79..10e07fd3 100644 --- a/app/Dto/QueryRandomPersonCommand.php +++ b/app/Dto/QueryRandomPersonCommand.php @@ -3,15 +3,12 @@ namespace App\Dto; use App\Contracts\DataRequest; -use App\Dto\Concerns\HasLimitParameterWithSmallerMax; -use App\Http\Resources\V4\PersonCollection; use App\Http\Resources\V4\PersonResource; use Spatie\LaravelData\Data; /** - * @implements DataRequest + * @implements DataRequest */ final class QueryRandomPersonCommand extends Data implements DataRequest { - use HasLimitParameterWithSmallerMax; } diff --git a/app/Dto/QueryRandomPersonListCommand.php b/app/Dto/QueryRandomPersonListCommand.php new file mode 100644 index 00000000..e5075088 --- /dev/null +++ b/app/Dto/QueryRandomPersonListCommand.php @@ -0,0 +1,16 @@ + + */ +final class QueryRandomPersonListCommand extends Data implements DataRequest +{ + use HasLimitParameterWithSmallerMax; +} diff --git a/app/Dto/QueryRandomUserCommand.php b/app/Dto/QueryRandomUserCommand.php index 9f6cc08d..18bd3051 100644 --- a/app/Dto/QueryRandomUserCommand.php +++ b/app/Dto/QueryRandomUserCommand.php @@ -12,5 +12,4 @@ */ final class QueryRandomUserCommand extends Data implements DataRequest { - use HasLimitParameterWithSmallerMax; } diff --git a/app/Dto/QueryRandomUserListCommand.php b/app/Dto/QueryRandomUserListCommand.php new file mode 100644 index 00000000..d6c3046a --- /dev/null +++ b/app/Dto/QueryRandomUserListCommand.php @@ -0,0 +1,16 @@ + + */ +final class QueryRandomUserListCommand extends Data implements DataRequest +{ + use HasLimitParameterWithSmallerMax; +} diff --git a/app/Features/QueryRandomAnimeHandler.php b/app/Features/QueryRandomAnimeHandler.php index a72b0b95..372fdfa4 100644 --- a/app/Features/QueryRandomAnimeHandler.php +++ b/app/Features/QueryRandomAnimeHandler.php @@ -5,31 +5,27 @@ use App\Anime; use App\Contracts\RequestHandler; use App\Dto\QueryRandomAnimeCommand; -use App\Http\Resources\V4\AnimeCollection; use App\Http\Resources\V4\AnimeResource; use Spatie\LaravelData\Optional; /** - * @implements RequestHandler + * @implements RequestHandler */ final class QueryRandomAnimeHandler implements RequestHandler { /** * @inheritDoc */ - public function handle($request): AnimeResource|AnimeCollection + public function handle($request): AnimeResource { $queryable = Anime::query(); $sfwParam = $request->sfw instanceof Optional ? false : $request->sfw; $unapprovedParam = $request->unapproved instanceof Optional ? false : $request->unapproved; - $limit = $request->limit instanceof Optional ? 1 : $request->limit; - $results = $queryable->random($limit, $sfwParam, $unapprovedParam); + $results = $queryable->random(1, $sfwParam, $unapprovedParam); - return $results->count() === 1 - ? new AnimeResource($results->first()) - : new AnimeCollection($results, false); + return new AnimeResource($results->first()); } /** diff --git a/app/Features/QueryRandomAnimeListHandler.php b/app/Features/QueryRandomAnimeListHandler.php new file mode 100644 index 00000000..31f51316 --- /dev/null +++ b/app/Features/QueryRandomAnimeListHandler.php @@ -0,0 +1,40 @@ + + */ +final class QueryRandomAnimeListHandler implements RequestHandler +{ + /** + * @inheritDoc + */ + public function handle($request): AnimeCollection + { + $queryable = Anime::query(); + + $sfwParam = $request->sfw instanceof Optional ? false : $request->sfw; + $unapprovedParam = $request->unapproved instanceof Optional ? false : $request->unapproved; + $limit = $request->limit instanceof Optional ? 1 : $request->limit; + + $results = $queryable->random($limit, $sfwParam, $unapprovedParam); + + return new AnimeCollection($results, false); + } + + /** + * @inheritDoc + */ + public function requestClass(): string + { + return QueryRandomAnimeListCommand::class; + } +} diff --git a/app/Features/QueryRandomCharacterHandler.php b/app/Features/QueryRandomCharacterHandler.php index c9e597fb..00cd5ff0 100644 --- a/app/Features/QueryRandomCharacterHandler.php +++ b/app/Features/QueryRandomCharacterHandler.php @@ -5,12 +5,10 @@ use App\Character; use App\Contracts\RequestHandler; use App\Dto\QueryRandomCharacterCommand; -use App\Http\Resources\V4\CharacterCollection; use App\Http\Resources\V4\CharacterResource; -use Spatie\LaravelData\Optional; /** - * @extends QueryRandomCharacterHandler + * @extends QueryRandomCharacterHandler */ final class QueryRandomCharacterHandler implements RequestHandler { @@ -18,17 +16,13 @@ final class QueryRandomCharacterHandler implements RequestHandler /** * @inheritDoc */ - public function handle($request): CharacterResource|CharacterCollection + public function handle($request): CharacterResource { - $queryable = Character::query(); - - $limit = $request->limit instanceof Optional ? 1 : $request->limit; - - $results = $queryable->random($limit); - - return $results->count() === 1 - ? new CharacterResource($results->first()) - : new CharacterCollection($results, false); + return new CharacterResource( + Character::query() + ->random(1) + ->first() + ); } /** diff --git a/app/Features/QueryRandomCharacterListHandler.php b/app/Features/QueryRandomCharacterListHandler.php new file mode 100644 index 00000000..e7a1e930 --- /dev/null +++ b/app/Features/QueryRandomCharacterListHandler.php @@ -0,0 +1,35 @@ + + */ +final class QueryRandomCharacterListHandler implements RequestHandler +{ + /** + * @inheritDoc + */ + public function handle($request): CharacterCollection + { + $queryable = Character::query(); + $limit = $request->limit instanceof Optional ? 1 : $request->limit; + $results = $queryable->random($limit); + + return new CharacterCollection($results, false); + } + + /** + * @inheritDoc + */ + public function requestClass(): string + { + return QueryRandomCharacterListCommand::class; + } +} diff --git a/app/Features/QueryRandomMangaHandler.php b/app/Features/QueryRandomMangaHandler.php index bf013cc8..452dbed3 100644 --- a/app/Features/QueryRandomMangaHandler.php +++ b/app/Features/QueryRandomMangaHandler.php @@ -4,32 +4,27 @@ use App\Contracts\RequestHandler; use App\Dto\QueryRandomMangaCommand; -use App\Http\Resources\V4\MangaCollection; use App\Http\Resources\V4\MangaResource; use App\Manga; use Spatie\LaravelData\Optional; /** - * @implements RequestHandler + * @implements RequestHandler */ final class QueryRandomMangaHandler implements RequestHandler { /** * @inheritDoc */ - public function handle($request): MangaResource|MangaCollection + public function handle($request): MangaResource { $queryable = Manga::query(); $sfwParam = $request->sfw instanceof Optional ? false : $request->sfw; $unapprovedParam = $request->unapproved instanceof Optional ? false : $request->unapproved; - $limit = $request->limit instanceof Optional ? 1 : $request->limit; + $results = $queryable->random(1, $sfwParam, $unapprovedParam); - $results = $queryable->random($limit, $sfwParam, $unapprovedParam); - - return $results->count() === 1 - ? new MangaResource($results->first()) - : new MangaCollection($results, false); + return new MangaResource($results->first()); } /** diff --git a/app/Features/QueryRandomMangaListHandler.php b/app/Features/QueryRandomMangaListHandler.php new file mode 100644 index 00000000..d51befe9 --- /dev/null +++ b/app/Features/QueryRandomMangaListHandler.php @@ -0,0 +1,40 @@ + + */ +final class QueryRandomMangaListHandler implements RequestHandler +{ + /** + * @inheritDoc + */ + public function handle($request): MangaCollection + { + $queryable = Manga::query(); + + $sfwParam = $request->sfw instanceof Optional ? false : $request->sfw; + $unapprovedParam = $request->unapproved instanceof Optional ? false : $request->unapproved; + $limit = $request->limit instanceof Optional ? 1 : $request->limit; + + $results = $queryable->random($limit, $sfwParam, $unapprovedParam); + + return new MangaCollection($results, false); + } + + /** + * @inheritDoc + */ + public function requestClass(): string + { + return QueryRandomMangaListCommand::class; + } +} diff --git a/app/Features/QueryRandomPersonHandler.php b/app/Features/QueryRandomPersonHandler.php index 045652a6..8027acf0 100644 --- a/app/Features/QueryRandomPersonHandler.php +++ b/app/Features/QueryRandomPersonHandler.php @@ -4,13 +4,11 @@ use App\Contracts\RequestHandler; use App\Dto\QueryRandomPersonCommand; -use App\Http\Resources\V4\PersonCollection; use App\Http\Resources\V4\PersonResource; use App\Person; -use Spatie\LaravelData\Optional; /** - * @extends RequestHandler + * @extends RequestHandler */ final class QueryRandomPersonHandler implements RequestHandler { @@ -18,17 +16,13 @@ final class QueryRandomPersonHandler implements RequestHandler /** * @inheritDoc */ - public function handle($request): PersonResource|PersonCollection + public function handle($request): PersonResource { - $queryable = Person::query(); - - $limit = $request->limit instanceof Optional ? 1 : $request->limit; - - $results = $queryable->random($limit); - - return $results->count() === 1 - ? new PersonResource($results->first()) - : new PersonCollection($results, false); + return new PersonResource( + Person::query() + ->random() + ->first() + ); } /** diff --git a/app/Features/QueryRandomPersonListHandler.php b/app/Features/QueryRandomPersonListHandler.php new file mode 100644 index 00000000..af7181be --- /dev/null +++ b/app/Features/QueryRandomPersonListHandler.php @@ -0,0 +1,37 @@ + + */ +final class QueryRandomPersonListHandler implements RequestHandler +{ + + /** + * @inheritDoc + */ + public function handle($request): PersonCollection + { + $queryable = Person::query(); + $limit = $request->limit instanceof Optional ? 1 : $request->limit; + $results = $queryable->random($limit); + + return new PersonCollection($results, false); + } + + /** + * @inheritDoc + */ + public function requestClass(): string + { + return QueryRandomPersonListCommand::class; + } +} diff --git a/app/Features/QueryRandomUserHandler.php b/app/Features/QueryRandomUserHandler.php index b1035622..db89a37a 100644 --- a/app/Features/QueryRandomUserHandler.php +++ b/app/Features/QueryRandomUserHandler.php @@ -5,29 +5,22 @@ use App\Contracts\RequestHandler; use App\Dto\QueryRandomUserCommand; use App\Http\Resources\V4\ProfileResource; -use App\Http\Resources\V4\UserCollection; use App\Profile; -use Spatie\LaravelData\Optional; /** - * @extends RequestHandler + * @extends RequestHandler */ final class QueryRandomUserHandler implements RequestHandler { /** * @inheritDoc */ - public function handle($request): ProfileResource|UserCollection + public function handle($request): ProfileResource { $queryable = Profile::query(); + $results = $queryable->random(1); - $limit = $request->limit instanceof Optional ? 1 : $request->limit; - - $results = $queryable->random($limit); - - return $results->count() === 1 - ? new ProfileResource($results->first()) - : new UserCollection($results, false); + return new ProfileResource($results->first()); } /** diff --git a/app/Features/QueryRandomUserListHandler.php b/app/Features/QueryRandomUserListHandler.php new file mode 100644 index 00000000..73ef794b --- /dev/null +++ b/app/Features/QueryRandomUserListHandler.php @@ -0,0 +1,37 @@ + + */ +final class QueryRandomUserListHandler implements RequestHandler +{ + /** + * @inheritDoc + */ + public function handle($request): UserCollection + { + $queryable = Profile::query(); + $limit = $request->limit instanceof Optional ? 1 : $request->limit; + $results = $queryable->random($limit); + + return new UserCollection($results, false); + } + + /** + * @inheritDoc + */ + public function requestClass(): string + { + return QueryRandomUserListCommand::class; + } +} diff --git a/app/Http/Controllers/V4DB/RandomController.php b/app/Http/Controllers/V4DB/RandomController.php index 5daf3606..ba53c04a 100644 --- a/app/Http/Controllers/V4DB/RandomController.php +++ b/app/Http/Controllers/V4DB/RandomController.php @@ -3,10 +3,14 @@ namespace App\Http\Controllers\V4DB; use App\Dto\QueryRandomAnimeCommand; +use App\Dto\QueryRandomAnimeListCommand; use App\Dto\QueryRandomCharacterCommand; +use App\Dto\QueryRandomCharacterListCommand; use App\Dto\QueryRandomMangaCommand; use App\Dto\QueryRandomPersonCommand; +use App\Dto\QueryRandomPersonListCommand; use App\Dto\QueryRandomUserCommand; +use App\Dto\QueryRandomUserListCommand; class RandomController extends Controller @@ -44,7 +48,7 @@ class RandomController extends Controller * * @OA\Response( * response="200", - * description="Returns a single random anime resource or multiple resources in an array when `limit` is supplied", + * description="Returns a single random anime resource", * @OA\JsonContent( * @OA\Property( * property="data", @@ -63,6 +67,39 @@ public function anime(QueryRandomAnimeCommand $command) return $this->mediator->send($command); } + /** + * @OA\Get( + * path="/random/list/anime", + * operationId="getRandomAnimeList", + * tags={"random"}, + * + * @OA\Parameter(ref="#/components/parameters/limit"), + * + * @OA\Response( + * response="200", + * description="Returns multiple anime resources. You can use `limit` to control the number of items returned. By default it returns 1 and maximum 5", + * @OA\JsonContent( + * @OA\Property( + * property="data", + * type="array", + * @OA\Items( + * type="object", + * ref="#/components/schemas/anime" + * ), + * ), + * ) + * ), + * @OA\Response( + * response="400", + * description="Error: Bad request. When required parameters were not supplied.", + * ), + * ), + */ + public function animeList(QueryRandomAnimeListCommand $command) + { + return $this->mediator->send($command); + } + /** * @OA\Get( * path="/random/manga", @@ -92,6 +129,39 @@ public function manga(QueryRandomMangaCommand $command) return $this->mediator->send($command); } + /** + * @OA\Get( + * path="/random/list/manga", + * operationId="getRandomMangaList", + * tags={"random"}, + * + * @OA\Parameter(ref="#/components/parameters/limit"), + * + * @OA\Response( + * response="200", + * description="Returns multiple manga resources. You can use `limit` to control the number of items returned. By default it returns 1 and maximum 5", + * @OA\JsonContent( + * @OA\Property( + * property="data", + * type="array", + * @OA\Items( + * type="object", + * ref="#/components/schemas/manga" + * ), + * ), + * ) + * ), + * @OA\Response( + * response="400", + * description="Error: Bad request. When required parameters were not supplied.", + * ), + * ), + */ + public function mangaList(QueryRandomAnimeListCommand $command) + { + return $this->mediator->send($command); + } + /** * @OA\Get( * path="/random/characters", @@ -121,6 +191,39 @@ public function characters(QueryRandomCharacterCommand $command) return $this->mediator->send($command); } + /** + * @OA\Get( + * path="/random/list/characters", + * operationId="getRandomCharactersList", + * tags={"random"}, + * + * @OA\Parameter(ref="#/components/parameters/limit"), + * + * @OA\Response( + * response="200", + * description="Returns multiple character resources. You can use `limit` to control the number of items returned. By default it returns 1 and maximum 5", + * @OA\JsonContent( + * @OA\Property( + * property="data", + * type="array", + * @OA\Items( + * type="object", + * ref="#/components/schemas/character" + * ), + * ), + * ) + * ), + * @OA\Response( + * response="400", + * description="Error: Bad request. When required parameters were not supplied.", + * ), + * ), + */ + public function charactersList(QueryRandomCharacterListCommand $command) + { + return $this->mediator->send($command); + } + /** * @OA\Get( * path="/random/people", @@ -150,6 +253,39 @@ public function people(QueryRandomPersonCommand $command) return $this->mediator->send($command); } + /** + * @OA\Get( + * path="/random/list/people", + * operationId="getRandomPeopleList", + * tags={"random"}, + * + * @OA\Parameter(ref="#/components/parameters/limit"), + * + * @OA\Response( + * response="200", + * description="Returns multiple people resources. You can use `limit` to control the number of items returned. By default it returns 1 and maximum 5", + * @OA\JsonContent( + * @OA\Property( + * property="data", + * type="array", + * @OA\Items( + * type="object", + * ref="#/components/schemas/person" + * ), + * ), + * ) + * ), + * @OA\Response( + * response="400", + * description="Error: Bad request. When required parameters were not supplied.", + * ), + * ), + */ + public function peopleList(QueryRandomPersonListCommand $command) + { + return $this->mediator->send($command); + } + /** * @OA\Get( * path="/random/users", @@ -178,4 +314,37 @@ public function users(QueryRandomUserCommand $command) { return $this->mediator->send($command); } + + /** + * @OA\Get( + * path="/random/list/users", + * operationId="getRandomUsersList", + * tags={"random"}, + * + * @OA\Parameter(ref="#/components/parameters/limit"), + * + * @OA\Response( + * response="200", + * description="Returns multiple user resources. You can use `limit` to control the number of items returned. By default it returns 1 and maximum 5", + * @OA\JsonContent( + * @OA\Property( + * property="data", + * type="array", + * @OA\Items( + * type="object", + * ref="#/components/schemas/user_profile" + * ), + * ), + * ) + * ), + * @OA\Response( + * response="400", + * description="Error: Bad request. When required parameters were not supplied.", + * ), + * ), + */ + public function usersList(QueryRandomUserListCommand $command) + { + return $this->mediator->send($command); + } } diff --git a/app/Http/Middleware/MicroCaching.php b/app/Http/Middleware/MicroCaching.php index 115b1167..4d600cca 100644 --- a/app/Http/Middleware/MicroCaching.php +++ b/app/Http/Middleware/MicroCaching.php @@ -5,18 +5,21 @@ use App\Http\HttpHelper; use App\Support\JikanConfig; use Closure; -use Illuminate\Support\Env; use Illuminate\Support\Facades\Cache; -use Jikan\Exception\BadResponseException; class MicroCaching { private const NO_CACHING = [ 'RandomController@anime', + 'RandomController@animeList', 'RandomController@manga', + 'RandomController@mangaList', 'RandomController@characters', + 'RandomController@charactersList', 'RandomController@people', + 'RandomController@peopleList', 'RandomController@users', + 'RandomController@usersList', 'InsightsController@main' ]; diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 4b8fb545..8eda934e 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -307,10 +307,15 @@ private function registerRequestHandlers() // automatically resolvable dependencies or no dependencies at all $requestHandlersWithNoDependencies = [ Features\QueryRandomAnimeHandler::class, + Features\QueryRandomAnimeListHandler::class, Features\QueryRandomMangaHandler::class, + Features\QueryRandomMangaListHandler::class, Features\QueryRandomCharacterHandler::class, + Features\QueryRandomCharacterListHandler::class, Features\QueryRandomPersonHandler::class, + Features\QueryRandomPersonListHandler::class, Features\QueryRandomUserHandler::class, + Features\QueryRandomUserListHandler::class, Features\QueryAnimeSchedulesHandler::class, Features\QueryCurrentAnimeSeasonHandler::class, Features\QuerySpecificAnimeSeasonHandler::class, diff --git a/routes/web.v4.php b/routes/web.v4.php index 0d064746..87b1f3b2 100644 --- a/routes/web.v4.php +++ b/routes/web.v4.php @@ -556,6 +556,33 @@ function() use ($router) { $router->get('/users', [ 'uses' => 'RandomController@users', ]); + + $router->group( + [ + 'prefix' => '/list' + ], + function() use ($router) { + $router->get('/anime', [ + 'uses' => 'RandomController@animeList', + ]); + + $router->get('/manga', [ + 'uses' => 'RandomController@mangaList', + ]); + + $router->get('/characters', [ + 'uses' => 'RandomController@charactersList', + ]); + + $router->get('/people', [ + 'uses' => 'RandomController@peopleList', + ]); + + $router->get('/users', [ + 'uses' => 'RandomController@usersList', + ]); + } + ); } ); From 389b58aa9939182eb2c56438fa401b70518e42ca Mon Sep 17 00:00:00 2001 From: Irfan Date: Wed, 13 Nov 2024 21:30:38 +0500 Subject: [PATCH 09/11] update docs: adds /random/lists/* --- storage/api-docs/api-docs.json | 182 ++++++++++++++++++++++++++++++++- 1 file changed, 181 insertions(+), 1 deletion(-) diff --git a/storage/api-docs/api-docs.json b/storage/api-docs/api-docs.json index cc73d4cf..4e1ff48d 100644 --- a/storage/api-docs/api-docs.json +++ b/storage/api-docs/api-docs.json @@ -2029,7 +2029,7 @@ ], "responses": { "200": { - "description": "Returns a single random anime resource or multiple resources in an array when `limit` is supplied", + "description": "Returns a single random anime resource", "content": { "application/json": { "schema": { @@ -2049,6 +2049,42 @@ } } }, + "/random/list/anime": { + "get": { + "tags": [ + "random" + ], + "operationId": "getRandomAnimeList", + "parameters": [ + { + "$ref": "#/components/parameters/limit" + } + ], + "responses": { + "200": { + "description": "Returns multiple anime resources. You can use `limit` to control the number of items returned. By default it returns 1 and maximum 5", + "content": { + "application/json": { + "schema": { + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/components/schemas/anime" + } + } + }, + "type": "object" + } + } + } + }, + "400": { + "description": "Error: Bad request. When required parameters were not supplied." + } + } + } + }, "/random/manga": { "get": { "tags": [ @@ -2082,6 +2118,42 @@ } } }, + "/random/list/manga": { + "get": { + "tags": [ + "random" + ], + "operationId": "getRandomMangaList", + "parameters": [ + { + "$ref": "#/components/parameters/limit" + } + ], + "responses": { + "200": { + "description": "Returns multiple manga resources. You can use `limit` to control the number of items returned. By default it returns 1 and maximum 5", + "content": { + "application/json": { + "schema": { + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/components/schemas/manga" + } + } + }, + "type": "object" + } + } + } + }, + "400": { + "description": "Error: Bad request. When required parameters were not supplied." + } + } + } + }, "/random/characters": { "get": { "tags": [ @@ -2115,6 +2187,42 @@ } } }, + "/random/list/characters": { + "get": { + "tags": [ + "random" + ], + "operationId": "getRandomCharactersList", + "parameters": [ + { + "$ref": "#/components/parameters/limit" + } + ], + "responses": { + "200": { + "description": "Returns multiple character resources. You can use `limit` to control the number of items returned. By default it returns 1 and maximum 5", + "content": { + "application/json": { + "schema": { + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/components/schemas/character" + } + } + }, + "type": "object" + } + } + } + }, + "400": { + "description": "Error: Bad request. When required parameters were not supplied." + } + } + } + }, "/random/people": { "get": { "tags": [ @@ -2148,6 +2256,42 @@ } } }, + "/random/list/people": { + "get": { + "tags": [ + "random" + ], + "operationId": "getRandomPeopleList", + "parameters": [ + { + "$ref": "#/components/parameters/limit" + } + ], + "responses": { + "200": { + "description": "Returns multiple people resources. You can use `limit` to control the number of items returned. By default it returns 1 and maximum 5", + "content": { + "application/json": { + "schema": { + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/components/schemas/person" + } + } + }, + "type": "object" + } + } + } + }, + "400": { + "description": "Error: Bad request. When required parameters were not supplied." + } + } + } + }, "/random/users": { "get": { "tags": [ @@ -2181,6 +2325,42 @@ } } }, + "/random/list/users": { + "get": { + "tags": [ + "random" + ], + "operationId": "getRandomUsersList", + "parameters": [ + { + "$ref": "#/components/parameters/limit" + } + ], + "responses": { + "200": { + "description": "Returns multiple user resources. You can use `limit` to control the number of items returned. By default it returns 1 and maximum 5", + "content": { + "application/json": { + "schema": { + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/components/schemas/user_profile" + } + } + }, + "type": "object" + } + } + } + }, + "400": { + "description": "Error: Bad request. When required parameters were not supplied." + } + } + } + }, "/recommendations/anime": { "get": { "tags": [ From 0442af7631f879756fb16f3160f5b343f2ed88d5 Mon Sep 17 00:00:00 2001 From: Irfan Date: Wed, 13 Nov 2024 21:34:07 +0500 Subject: [PATCH 10/11] update docs [#490]: Random API: adds sfw and unapproved --- .../Controllers/V4DB/RandomController.php | 25 +++++++-- storage/api-docs/api-docs.json | 55 +++++++++++++++++-- 2 files changed, 70 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/V4DB/RandomController.php b/app/Http/Controllers/V4DB/RandomController.php index ba53c04a..a778bb6c 100644 --- a/app/Http/Controllers/V4DB/RandomController.php +++ b/app/Http/Controllers/V4DB/RandomController.php @@ -44,7 +44,8 @@ class RandomController extends Controller * operationId="getRandomAnime", * tags={"random"}, * - * @OA\Parameter(ref="#/components/parameters/limit"), + * @OA\Parameter(ref="#/components/parameters/sfw"), + * @OA\Parameter(ref="#/components/parameters/unapproved"), * * @OA\Response( * response="200", @@ -73,6 +74,8 @@ public function anime(QueryRandomAnimeCommand $command) * operationId="getRandomAnimeList", * tags={"random"}, * + * @OA\Parameter(ref="#/components/parameters/sfw"), + * @OA\Parameter(ref="#/components/parameters/unapproved"), * @OA\Parameter(ref="#/components/parameters/limit"), * * @OA\Response( @@ -106,7 +109,8 @@ public function animeList(QueryRandomAnimeListCommand $command) * operationId="getRandomManga", * tags={"random"}, * - * @OA\Parameter(ref="#/components/parameters/limit"), + * @OA\Parameter(ref="#/components/parameters/sfw"), + * @OA\Parameter(ref="#/components/parameters/unapproved"), * * @OA\Response( * response="200", @@ -135,6 +139,8 @@ public function manga(QueryRandomMangaCommand $command) * operationId="getRandomMangaList", * tags={"random"}, * + * @OA\Parameter(ref="#/components/parameters/sfw"), + * @OA\Parameter(ref="#/components/parameters/unapproved"), * @OA\Parameter(ref="#/components/parameters/limit"), * * @OA\Response( @@ -168,7 +174,8 @@ public function mangaList(QueryRandomAnimeListCommand $command) * operationId="getRandomCharacters", * tags={"random"}, * - * @OA\Parameter(ref="#/components/parameters/limit"), + * @OA\Parameter(ref="#/components/parameters/sfw"), + * @OA\Parameter(ref="#/components/parameters/unapproved"), * * @OA\Response( * response="200", @@ -197,6 +204,8 @@ public function characters(QueryRandomCharacterCommand $command) * operationId="getRandomCharactersList", * tags={"random"}, * + * @OA\Parameter(ref="#/components/parameters/sfw"), + * @OA\Parameter(ref="#/components/parameters/unapproved"), * @OA\Parameter(ref="#/components/parameters/limit"), * * @OA\Response( @@ -230,7 +239,8 @@ public function charactersList(QueryRandomCharacterListCommand $command) * operationId="getRandomPeople", * tags={"random"}, * - * @OA\Parameter(ref="#/components/parameters/limit"), + * @OA\Parameter(ref="#/components/parameters/sfw"), + * @OA\Parameter(ref="#/components/parameters/unapproved"), * * @OA\Response( * response="200", @@ -259,6 +269,8 @@ public function people(QueryRandomPersonCommand $command) * operationId="getRandomPeopleList", * tags={"random"}, * + * @OA\Parameter(ref="#/components/parameters/sfw"), + * @OA\Parameter(ref="#/components/parameters/unapproved"), * @OA\Parameter(ref="#/components/parameters/limit"), * * @OA\Response( @@ -292,7 +304,8 @@ public function peopleList(QueryRandomPersonListCommand $command) * operationId="getRandomUsers", * tags={"random"}, * - * @OA\Parameter(ref="#/components/parameters/limit"), + * @OA\Parameter(ref="#/components/parameters/sfw"), + * @OA\Parameter(ref="#/components/parameters/unapproved"), * * @OA\Response( * response="200", @@ -321,6 +334,8 @@ public function users(QueryRandomUserCommand $command) * operationId="getRandomUsersList", * tags={"random"}, * + * @OA\Parameter(ref="#/components/parameters/sfw"), + * @OA\Parameter(ref="#/components/parameters/unapproved"), * @OA\Parameter(ref="#/components/parameters/limit"), * * @OA\Response( diff --git a/storage/api-docs/api-docs.json b/storage/api-docs/api-docs.json index 4e1ff48d..688647db 100644 --- a/storage/api-docs/api-docs.json +++ b/storage/api-docs/api-docs.json @@ -2024,7 +2024,10 @@ "operationId": "getRandomAnime", "parameters": [ { - "$ref": "#/components/parameters/limit" + "$ref": "#/components/parameters/sfw" + }, + { + "$ref": "#/components/parameters/unapproved" } ], "responses": { @@ -2056,6 +2059,12 @@ ], "operationId": "getRandomAnimeList", "parameters": [ + { + "$ref": "#/components/parameters/sfw" + }, + { + "$ref": "#/components/parameters/unapproved" + }, { "$ref": "#/components/parameters/limit" } @@ -2093,7 +2102,10 @@ "operationId": "getRandomManga", "parameters": [ { - "$ref": "#/components/parameters/limit" + "$ref": "#/components/parameters/sfw" + }, + { + "$ref": "#/components/parameters/unapproved" } ], "responses": { @@ -2125,6 +2137,12 @@ ], "operationId": "getRandomMangaList", "parameters": [ + { + "$ref": "#/components/parameters/sfw" + }, + { + "$ref": "#/components/parameters/unapproved" + }, { "$ref": "#/components/parameters/limit" } @@ -2162,7 +2180,10 @@ "operationId": "getRandomCharacters", "parameters": [ { - "$ref": "#/components/parameters/limit" + "$ref": "#/components/parameters/sfw" + }, + { + "$ref": "#/components/parameters/unapproved" } ], "responses": { @@ -2194,6 +2215,12 @@ ], "operationId": "getRandomCharactersList", "parameters": [ + { + "$ref": "#/components/parameters/sfw" + }, + { + "$ref": "#/components/parameters/unapproved" + }, { "$ref": "#/components/parameters/limit" } @@ -2231,7 +2258,10 @@ "operationId": "getRandomPeople", "parameters": [ { - "$ref": "#/components/parameters/limit" + "$ref": "#/components/parameters/sfw" + }, + { + "$ref": "#/components/parameters/unapproved" } ], "responses": { @@ -2263,6 +2293,12 @@ ], "operationId": "getRandomPeopleList", "parameters": [ + { + "$ref": "#/components/parameters/sfw" + }, + { + "$ref": "#/components/parameters/unapproved" + }, { "$ref": "#/components/parameters/limit" } @@ -2300,7 +2336,10 @@ "operationId": "getRandomUsers", "parameters": [ { - "$ref": "#/components/parameters/limit" + "$ref": "#/components/parameters/sfw" + }, + { + "$ref": "#/components/parameters/unapproved" } ], "responses": { @@ -2332,6 +2371,12 @@ ], "operationId": "getRandomUsersList", "parameters": [ + { + "$ref": "#/components/parameters/sfw" + }, + { + "$ref": "#/components/parameters/unapproved" + }, { "$ref": "#/components/parameters/limit" } From c71b34380d74c4ce788b4b7ccc5f1f2e0086d1dd Mon Sep 17 00:00:00 2001 From: Irfan Date: Wed, 13 Nov 2024 23:10:53 +0500 Subject: [PATCH 11/11] fixes tests --- app/Features/QueryAnimeSchedulesHandler.php | 3 ++- app/Features/QueryAnimeSeasonHandlerBase.php | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/Features/QueryAnimeSchedulesHandler.php b/app/Features/QueryAnimeSchedulesHandler.php index 573d94c8..1f408d55 100644 --- a/app/Features/QueryAnimeSchedulesHandler.php +++ b/app/Features/QueryAnimeSchedulesHandler.php @@ -8,6 +8,7 @@ use App\Http\Resources\V4\AnimeCollection; use App\Support\CachedData; use Illuminate\Support\Env; +use Spatie\LaravelData\Optional; /** * @implements RequestHandler @@ -24,7 +25,7 @@ public function __construct(private readonly AnimeRepository $repository) public function handle($request) { $requestParams = collect($request->all()); - $limit = $requestParams->get("limit"); + $limit = $request->limit instanceof Optional ? max_results_per_page() : $request->limit; $results = $this->repository->getCurrentlyAiring($request->filter); // apply sfw, kids and unapproved filters /** @noinspection PhpUndefinedMethodInspection */ diff --git a/app/Features/QueryAnimeSeasonHandlerBase.php b/app/Features/QueryAnimeSeasonHandlerBase.php index 67a8cedf..0498a84d 100644 --- a/app/Features/QueryAnimeSeasonHandlerBase.php +++ b/app/Features/QueryAnimeSeasonHandlerBase.php @@ -12,6 +12,7 @@ use Illuminate\Contracts\Database\Query\Builder; use Illuminate\Http\JsonResponse; use Illuminate\Support\Carbon; +use Spatie\LaravelData\Optional; use Symfony\Component\HttpFoundation\Exception\BadRequestException; /** @@ -32,11 +33,12 @@ public function handle($request): JsonResponse { $requestParams = collect($request->all()); $type = $requestParams->has("filter") ? $request->filter : null; + $limit = $request->limit instanceof Optional ? max_results_per_page() : $request->limit; $results = $this->getSeasonItems($request, $type); // apply sfw, kids and unapproved filters /** @noinspection PhpUndefinedMethodInspection */ $results = $results->filter($requestParams); - $results = $results->paginate($request->limit, ["*"], null, $request->page); + $results = $results->paginate($limit, ["*"], null, $request->page); $animeCollection = new AnimeCollection($results); $response = $animeCollection->response();