From f0e18ba6f7493178e5fc7f31a8970908fab5a3a9 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 6 Jul 2020 20:05:42 -0700 Subject: [PATCH] Code cleanup --- .../AllocationRepositoryInterface.php | 27 ------------- .../Nodes/AllocationController.php | 39 ++++++++----------- .../Eloquent/AllocationRepository.php | 36 ----------------- .../Servers/ServerCreationService.php | 4 +- 4 files changed, 19 insertions(+), 87 deletions(-) diff --git a/app/Contracts/Repository/AllocationRepositoryInterface.php b/app/Contracts/Repository/AllocationRepositoryInterface.php index 6f038a853..b02ef66a4 100644 --- a/app/Contracts/Repository/AllocationRepositoryInterface.php +++ b/app/Contracts/Repository/AllocationRepositoryInterface.php @@ -3,36 +3,9 @@ namespace Pterodactyl\Contracts\Repository; use Illuminate\Support\Collection; -use Illuminate\Contracts\Pagination\LengthAwarePaginator; interface AllocationRepositoryInterface extends RepositoryInterface { - /** - * Set an array of allocation IDs to be assigned to a specific server. - * - * @param int|null $server - * @param array $ids - * @return int - */ - public function assignAllocationsToServer(int $server = null, array $ids): int; - - /** - * Return all of the allocations for a specific node. - * - * @param int $node - * @return \Illuminate\Support\Collection - */ - public function getAllocationsForNode(int $node): Collection; - - /** - * Return all of the allocations for a node in a paginated format. - * - * @param int $node - * @param int $perPage - * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator - */ - public function getPaginatedAllocationsForNode(int $node, int $perPage = 100): LengthAwarePaginator; - /** * Return all of the unique IPs that exist for a given node. * diff --git a/app/Http/Controllers/Api/Application/Nodes/AllocationController.php b/app/Http/Controllers/Api/Application/Nodes/AllocationController.php index cc1a32ff8..01ec37fe1 100644 --- a/app/Http/Controllers/Api/Application/Nodes/AllocationController.php +++ b/app/Http/Controllers/Api/Application/Nodes/AllocationController.php @@ -3,11 +3,10 @@ namespace Pterodactyl\Http\Controllers\Api\Application\Nodes; use Pterodactyl\Models\Node; -use Illuminate\Http\Response; +use Illuminate\Http\JsonResponse; use Pterodactyl\Models\Allocation; use Pterodactyl\Services\Allocations\AssignmentService; use Pterodactyl\Services\Allocations\AllocationDeletionService; -use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Transformers\Api\Application\AllocationTransformer; use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController; use Pterodactyl\Http\Requests\Api\Application\Allocations\GetAllocationsRequest; @@ -26,41 +25,32 @@ class AllocationController extends ApplicationApiController */ private $deletionService; - /** - * @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface - */ - private $repository; - /** * AllocationController constructor. * * @param \Pterodactyl\Services\Allocations\AssignmentService $assignmentService * @param \Pterodactyl\Services\Allocations\AllocationDeletionService $deletionService - * @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $repository */ public function __construct( AssignmentService $assignmentService, - AllocationDeletionService $deletionService, - AllocationRepositoryInterface $repository + AllocationDeletionService $deletionService ) { parent::__construct(); $this->assignmentService = $assignmentService; $this->deletionService = $deletionService; - $this->repository = $repository; } /** * Return all of the allocations that exist for a given node. * * @param \Pterodactyl\Http\Requests\Api\Application\Allocations\GetAllocationsRequest $request + * @param \Pterodactyl\Models\Node $node * @return array */ - public function index(GetAllocationsRequest $request): array + public function index(GetAllocationsRequest $request, Node $node): array { - $allocations = $this->repository->getPaginatedAllocationsForNode( - $request->getModel(Node::class)->id, 50 - ); + $allocations = $node->allocations()->paginate(50); return $this->fractal->collection($allocations) ->transformWith($this->getTransformer(AllocationTransformer::class)) @@ -71,32 +61,35 @@ class AllocationController extends ApplicationApiController * Store new allocations for a given node. * * @param \Pterodactyl\Http\Requests\Api\Application\Allocations\StoreAllocationRequest $request - * @return \Illuminate\Http\Response + * @param \Pterodactyl\Models\Node $node + * @return \Illuminate\Http\JsonResponse * * @throws \Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException * @throws \Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException * @throws \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException * @throws \Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException */ - public function store(StoreAllocationRequest $request): Response + public function store(StoreAllocationRequest $request, Node $node): JsonResponse { - $this->assignmentService->handle($request->getModel(Node::class), $request->validated()); + $this->assignmentService->handle($node, $request->validated()); - return response('', 204); + return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT); } /** * Delete a specific allocation from the Panel. * * @param \Pterodactyl\Http\Requests\Api\Application\Allocations\DeleteAllocationRequest $request - * @return \Illuminate\Http\Response + * @param \Pterodactyl\Models\Node $node + * @param \Pterodactyl\Models\Allocation $allocation + * @return \Illuminate\Http\JsonResponse * * @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException */ - public function delete(DeleteAllocationRequest $request): Response + public function delete(DeleteAllocationRequest $request, Node $node, Allocation $allocation): JsonResponse { - $this->deletionService->handle($request->getModel(Allocation::class)); + $this->deletionService->handle($allocation); - return response('', 204); + return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT); } } diff --git a/app/Repositories/Eloquent/AllocationRepository.php b/app/Repositories/Eloquent/AllocationRepository.php index 920b5c135..cc7efb23b 100644 --- a/app/Repositories/Eloquent/AllocationRepository.php +++ b/app/Repositories/Eloquent/AllocationRepository.php @@ -5,7 +5,6 @@ namespace Pterodactyl\Repositories\Eloquent; use Illuminate\Support\Collection; use Pterodactyl\Models\Allocation; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; class AllocationRepository extends EloquentRepository implements AllocationRepositoryInterface @@ -20,41 +19,6 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos return Allocation::class; } - /** - * Set an array of allocation IDs to be assigned to a specific server. - * - * @param int|null $server - * @param array $ids - * @return int - */ - public function assignAllocationsToServer(int $server = null, array $ids): int - { - return $this->getBuilder()->whereIn('id', $ids)->update(['server_id' => $server]); - } - - /** - * Return all of the allocations for a specific node. - * - * @param int $node - * @return \Illuminate\Support\Collection - */ - public function getAllocationsForNode(int $node): Collection - { - return $this->getBuilder()->where('node_id', $node)->get($this->getColumns()); - } - - /** - * Return all of the allocations for a node in a paginated format. - * - * @param int $node - * @param int $perPage - * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator - */ - public function getPaginatedAllocationsForNode(int $node, int $perPage = 100): LengthAwarePaginator - { - return $this->getBuilder()->where('node_id', $node)->paginate($perPage, $this->getColumns()); - } - /** * Return all of the unique IPs that exist for a given node. * diff --git a/app/Services/Servers/ServerCreationService.php b/app/Services/Servers/ServerCreationService.php index b84bed7c5..09638547b 100644 --- a/app/Services/Servers/ServerCreationService.php +++ b/app/Services/Servers/ServerCreationService.php @@ -270,7 +270,9 @@ class ServerCreationService $records = array_merge($records, $data['allocation_additional']); } - $this->allocationRepository->assignAllocationsToServer($server->id, $records); + $this->allocationRepository->updateWhereIn('id', $records, [ + 'server_id' => $server->id, + ]); } /**