Skip to content

Commit

Permalink
adds limit to random endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
irfan-dahir committed Nov 9, 2024
1 parent ff2c1c9 commit 72274fe
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 148 deletions.
18 changes: 7 additions & 11 deletions app/Dto/Concerns/PreparesData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions app/Dto/QueryRandomAnimeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,7 +17,5 @@ final class QueryRandomAnimeCommand extends Data implements DataRequest
{
use HasSfwParameter,
HasUnapprovedParameter,
HasLimitParameter;

protected static int $defaultLimit = 5;
HasLimitParameterWithSmallerMax;
}
2 changes: 2 additions & 0 deletions app/Dto/QueryRandomCharacterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -11,4 +12,5 @@
*/
final class QueryRandomCharacterCommand extends Data implements DataRequest
{
use HasLimitParameterWithSmallerMax;
}
8 changes: 6 additions & 2 deletions app/Dto/QueryRandomMangaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<MangaResource>
* @implements DataRequest<MangaResource|MangaCollection>
*/
final class QueryRandomMangaCommand extends Data implements DataRequest
{
use HasSfwParameter, HasUnapprovedParameter;
use HasSfwParameter,
HasUnapprovedParameter,
HasLimitParameterWithSmallerMax;
}
5 changes: 4 additions & 1 deletion app/Dto/QueryRandomPersonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<PersonResource>
* @implements DataRequest<PersonResource|PersonCollection>
*/
final class QueryRandomPersonCommand extends Data implements DataRequest
{
use HasLimitParameterWithSmallerMax;
}
2 changes: 2 additions & 0 deletions app/Dto/QueryRandomUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -11,4 +12,5 @@
*/
final class QueryRandomUserCommand extends Data implements DataRequest
{
use HasLimitParameterWithSmallerMax;
}
11 changes: 3 additions & 8 deletions app/Features/QueryRandomAnimeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<QueryRandomAnimeCommand, AnimeResource|AnimeCollection>
Expand All @@ -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);

Expand Down
32 changes: 20 additions & 12 deletions app/Features/QueryRandomCharacterHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<QueryRandomCharacterCommand, CharacterResource>
* @extends QueryRandomCharacterHandler<QueryRandomCharacterCommand, CharacterResource|CharacterCollection>
*/
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);
}

/**
Expand All @@ -25,9 +38,4 @@ public function requestClass(): string
{
return QueryRandomCharacterCommand::class;
}

protected function resource(Collection $results): JsonResource
{
return new CharacterResource($results->first());
}
}
34 changes: 0 additions & 34 deletions app/Features/QueryRandomItemHandler.php

This file was deleted.

19 changes: 11 additions & 8 deletions app/Features/QueryRandomMangaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<QueryRandomMangaCommand, MangaResource>
* @implements RequestHandler<QueryRandomMangaCommand, MangaResource|MangaCollection>
*/
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);
}

/**
Expand Down
32 changes: 20 additions & 12 deletions app/Features/QueryRandomPersonHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<QueryRandomPersonCommand, PersonResource>
* @extends RequestHandler<QueryRandomPersonCommand, PersonResource|PersonCollection>
*/
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);
}

/**
Expand All @@ -25,9 +38,4 @@ public function requestClass(): string
{
return QueryRandomPersonCommand::class;
}

protected function resource(Collection $results): JsonResource
{
return new PersonResource($results->first());
}
}
31 changes: 18 additions & 13 deletions app/Features/QueryRandomUserHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<QueryRandomUserCommand, ProfileResource>
* @extends RequestHandler<QueryRandomUserCommand, ProfileResource|UserCollection>
*/
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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/V4/AnimeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Loading

0 comments on commit 72274fe

Please sign in to comment.