From 48ad8f538e50548c78f092085affd56d375af69d Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 26 Mar 2021 09:03:51 -0700 Subject: [PATCH] Always allow specifying a page size with the API; closes #3218 --- .../Api/Application/Locations/LocationController.php | 2 +- app/Http/Controllers/Api/Application/Nests/NestController.php | 2 +- .../Api/Application/Nodes/AllocationController.php | 3 ++- app/Http/Controllers/Api/Application/Nodes/NodeController.php | 2 +- .../Api/Application/Nodes/NodeDeploymentController.php | 2 +- .../Controllers/Api/Application/Servers/ServerController.php | 2 +- app/Http/Controllers/Api/Application/Users/UserController.php | 2 +- app/Services/Deployment/FindViableNodesService.php | 4 ++-- 8 files changed, 10 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/Api/Application/Locations/LocationController.php b/app/Http/Controllers/Api/Application/Locations/LocationController.php index cdcf013b3..b7e82396a 100644 --- a/app/Http/Controllers/Api/Application/Locations/LocationController.php +++ b/app/Http/Controllers/Api/Application/Locations/LocationController.php @@ -65,7 +65,7 @@ class LocationController extends ApplicationApiController $locations = QueryBuilder::for(Location::query()) ->allowedFilters(['short', 'long']) ->allowedSorts(['id']) - ->paginate(100); + ->paginate($request->query('per_page') ?? 50); return $this->fractal->collection($locations) ->transformWith($this->getTransformer(LocationTransformer::class)) diff --git a/app/Http/Controllers/Api/Application/Nests/NestController.php b/app/Http/Controllers/Api/Application/Nests/NestController.php index 92de473e8..b66872d23 100644 --- a/app/Http/Controllers/Api/Application/Nests/NestController.php +++ b/app/Http/Controllers/Api/Application/Nests/NestController.php @@ -30,7 +30,7 @@ class NestController extends ApplicationApiController */ public function index(GetNestsRequest $request): array { - $nests = $this->repository->paginated(50); + $nests = $this->repository->paginated($request->query('per_page') ?? 50); return $this->fractal->collection($nests) ->transformWith($this->getTransformer(NestTransformer::class)) diff --git a/app/Http/Controllers/Api/Application/Nodes/AllocationController.php b/app/Http/Controllers/Api/Application/Nodes/AllocationController.php index c66701494..5a66e8175 100644 --- a/app/Http/Controllers/Api/Application/Nodes/AllocationController.php +++ b/app/Http/Controllers/Api/Application/Nodes/AllocationController.php @@ -43,7 +43,7 @@ class AllocationController extends ApplicationApiController */ public function index(GetAllocationsRequest $request, Node $node): array { - $allocations = $node->allocations()->paginate(50); + $allocations = $node->allocations()->paginate($request->query('per_page') ?? 50); return $this->fractal->collection($allocations) ->transformWith($this->getTransformer(AllocationTransformer::class)) @@ -53,6 +53,7 @@ class AllocationController extends ApplicationApiController /** * Store new allocations for a given node. * + * @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException * @throws \Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException * @throws \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException diff --git a/app/Http/Controllers/Api/Application/Nodes/NodeController.php b/app/Http/Controllers/Api/Application/Nodes/NodeController.php index 61f27b822..2daf187c2 100644 --- a/app/Http/Controllers/Api/Application/Nodes/NodeController.php +++ b/app/Http/Controllers/Api/Application/Nodes/NodeController.php @@ -64,7 +64,7 @@ class NodeController extends ApplicationApiController $nodes = QueryBuilder::for(Node::query()) ->allowedFilters(['uuid', 'name', 'fqdn', 'daemon_token_id']) ->allowedSorts(['id', 'uuid', 'memory', 'disk']) - ->paginate(100); + ->paginate($request->query('per_page') ?? 50); return $this->fractal->collection($nodes) ->transformWith($this->getTransformer(NodeTransformer::class)) diff --git a/app/Http/Controllers/Api/Application/Nodes/NodeDeploymentController.php b/app/Http/Controllers/Api/Application/Nodes/NodeDeploymentController.php index 1714bf7ca..9d5c75a05 100644 --- a/app/Http/Controllers/Api/Application/Nodes/NodeDeploymentController.php +++ b/app/Http/Controllers/Api/Application/Nodes/NodeDeploymentController.php @@ -37,7 +37,7 @@ class NodeDeploymentController extends ApplicationApiController $nodes = $this->viableNodesService->setLocations($data['location_ids'] ?? []) ->setMemory($data['memory']) ->setDisk($data['disk']) - ->handle($request->input('page') ?? 0); + ->handle($request->query('per_page'), $request->query('page')); return $this->fractal->collection($nodes) ->transformWith($this->getTransformer(NodeTransformer::class)) diff --git a/app/Http/Controllers/Api/Application/Servers/ServerController.php b/app/Http/Controllers/Api/Application/Servers/ServerController.php index 795999721..e0e679930 100644 --- a/app/Http/Controllers/Api/Application/Servers/ServerController.php +++ b/app/Http/Controllers/Api/Application/Servers/ServerController.php @@ -56,7 +56,7 @@ class ServerController extends ApplicationApiController $servers = QueryBuilder::for(Server::query()) ->allowedFilters(['uuid', 'name', 'image', 'external_id']) ->allowedSorts(['id', 'uuid']) - ->paginate(100); + ->paginate($request->query('per_page') ?? 50); return $this->fractal->collection($servers) ->transformWith($this->getTransformer(ServerTransformer::class)) diff --git a/app/Http/Controllers/Api/Application/Users/UserController.php b/app/Http/Controllers/Api/Application/Users/UserController.php index 41a13f66c..0a5675e2d 100644 --- a/app/Http/Controllers/Api/Application/Users/UserController.php +++ b/app/Http/Controllers/Api/Application/Users/UserController.php @@ -66,7 +66,7 @@ class UserController extends ApplicationApiController $users = QueryBuilder::for(User::query()) ->allowedFilters(['email', 'uuid', 'username', 'external_id']) ->allowedSorts(['id', 'uuid']) - ->paginate(100); + ->paginate($request->query('per_page') ?? 50); return $this->fractal->collection($users) ->transformWith($this->getTransformer(UserTransformer::class)) diff --git a/app/Services/Deployment/FindViableNodesService.php b/app/Services/Deployment/FindViableNodesService.php index 76cb89abf..5c5a5138e 100644 --- a/app/Services/Deployment/FindViableNodesService.php +++ b/app/Services/Deployment/FindViableNodesService.php @@ -83,7 +83,7 @@ class FindViableNodesService * * @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException */ - public function handle(int $page = null) + public function handle(int $perPage = null, int $page = null) { Assert::integer($this->disk, 'Disk space must be an int, got %s'); Assert::integer($this->memory, 'Memory usage must be an int, got %s'); @@ -103,7 +103,7 @@ class FindViableNodesService ->havingRaw('(IFNULL(SUM(servers.disk), 0) + ?) <= (nodes.disk * (1 + (nodes.disk_overallocate / 100)))', [$this->disk]); if (!is_null($page)) { - $results = $results->paginate(50, ['*'], 'page', $page); + $results = $results->paginate($perPage ?? 50, ['*'], 'page', $page); } else { $results = $results->get()->toBase(); }