-
-
Notifications
You must be signed in to change notification settings - Fork 263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨Feature: Allows random endpoints to return multiple items #559
Open
irfan-dahir
wants to merge
13
commits into
master
Choose a base branch
from
feature/multiplerandom
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1597c92
poc
irfan-dahir aae5512
allow max limit override
irfan-dahir 41a3091
remove unused random code
irfan-dahir ff2c1c9
Create HasLimitParameterWithSmallerMax.php
irfan-dahir 72274fe
adds `limit` to random endpoints
irfan-dahir 6b18a36
removes $defaultLimit usage in DTO
irfan-dahir 3b59908
random: update docs
irfan-dahir f20e40c
add new /random/list/* endpoints
irfan-dahir 389b58a
update docs: adds /random/lists/*
irfan-dahir 0442af7
update docs [#490]: Random API: adds sfw and unapproved
irfan-dahir afcf8d7
Merge branch 'master' into feature/multiplerandom
irfan-dahir c71b343
fixes tests
irfan-dahir 39f18c2
Merge branch 'feature/multiplerandom' of https://github.com/jikan-me/…
irfan-dahir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace App\Dto\Concerns; | ||
|
||
use App\Rules\Attributes\MaxLimitWithFallback; | ||
use OpenApi\Annotations as OA; | ||
use Spatie\LaravelData\Attributes\Validation\IntegerType; | ||
use Spatie\LaravelData\Attributes\Validation\Min; | ||
use Spatie\LaravelData\Optional; | ||
|
||
trait HasLimitParameterWithSmallerMax | ||
{ | ||
use PreparesData; | ||
|
||
#[IntegerType, Min(1), MaxLimitWithFallback(5)] | ||
public int|Optional $limit; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
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 Spatie\LaravelData\Data; | ||
|
||
/** | ||
* @implements DataRequest<AnimeCollection> | ||
*/ | ||
final class QueryRandomAnimeListCommand extends Data implements DataRequest | ||
{ | ||
use HasSfwParameter, | ||
HasUnapprovedParameter, | ||
HasLimitParameterWithSmallerMax; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Dto; | ||
|
||
use App\Contracts\DataRequest; | ||
use App\Dto\Concerns\HasLimitParameterWithSmallerMax; | ||
use App\Http\Resources\V4\CharacterCollection; | ||
use Spatie\LaravelData\Data; | ||
|
||
/** | ||
* @implements DataRequest<CharacterCollection> | ||
*/ | ||
final class QueryRandomCharacterListCommand extends Data implements DataRequest | ||
{ | ||
use HasLimitParameterWithSmallerMax; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
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 Spatie\LaravelData\Data; | ||
|
||
/** | ||
* @implements DataRequest<MangaCollection> | ||
*/ | ||
final class QueryRandomMangaListCommand extends Data implements DataRequest | ||
{ | ||
use HasSfwParameter, | ||
HasUnapprovedParameter, | ||
HasLimitParameterWithSmallerMax; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Dto; | ||
|
||
use App\Contracts\DataRequest; | ||
use App\Dto\Concerns\HasLimitParameterWithSmallerMax; | ||
use App\Http\Resources\V4\PersonCollection; | ||
use Spatie\LaravelData\Data; | ||
|
||
/** | ||
* @implements DataRequest<PersonCollection> | ||
*/ | ||
final class QueryRandomPersonListCommand extends Data implements DataRequest | ||
{ | ||
use HasLimitParameterWithSmallerMax; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Dto; | ||
|
||
use App\Contracts\DataRequest; | ||
use App\Dto\Concerns\HasLimitParameterWithSmallerMax; | ||
use App\Http\Resources\V4\UserCollection; | ||
use Spatie\LaravelData\Data; | ||
|
||
/** | ||
* @implements DataRequest<UserCollection> | ||
*/ | ||
final class QueryRandomUserListCommand extends Data implements DataRequest | ||
{ | ||
use HasLimitParameterWithSmallerMax; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace App\Features; | ||
|
||
use App\Anime; | ||
use App\Contracts\RequestHandler; | ||
use App\Dto\QueryRandomAnimeCommand; | ||
use App\Dto\QueryRandomAnimeListCommand; | ||
use App\Http\Resources\V4\AnimeCollection; | ||
use Spatie\LaravelData\Optional; | ||
|
||
/** | ||
* @implements RequestHandler<QueryRandomAnimeCommand, AnimeCollection> | ||
*/ | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace App\Features; | ||
|
||
use App\Character; | ||
use App\Contracts\RequestHandler; | ||
use App\Dto\QueryRandomCharacterListCommand; | ||
use App\Http\Resources\V4\CharacterCollection; | ||
use Spatie\LaravelData\Optional; | ||
|
||
/** | ||
* @implements RequestHandler<QueryRandomCharacterListCommand, CharacterCollection> | ||
*/ | ||
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is repeating code, shouldn't we either change the
PreparesData
to replace Optional with this?