Code cleanup

This commit is contained in:
Dane Everitt 2020-07-06 20:05:42 -07:00
parent a2201aaa38
commit f0e18ba6f7
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 19 additions and 87 deletions

View file

@ -3,36 +3,9 @@
namespace Pterodactyl\Contracts\Repository; namespace Pterodactyl\Contracts\Repository;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
interface AllocationRepositoryInterface extends RepositoryInterface 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. * Return all of the unique IPs that exist for a given node.
* *

View file

@ -3,11 +3,10 @@
namespace Pterodactyl\Http\Controllers\Api\Application\Nodes; namespace Pterodactyl\Http\Controllers\Api\Application\Nodes;
use Pterodactyl\Models\Node; use Pterodactyl\Models\Node;
use Illuminate\Http\Response; use Illuminate\Http\JsonResponse;
use Pterodactyl\Models\Allocation; use Pterodactyl\Models\Allocation;
use Pterodactyl\Services\Allocations\AssignmentService; use Pterodactyl\Services\Allocations\AssignmentService;
use Pterodactyl\Services\Allocations\AllocationDeletionService; use Pterodactyl\Services\Allocations\AllocationDeletionService;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Transformers\Api\Application\AllocationTransformer; use Pterodactyl\Transformers\Api\Application\AllocationTransformer;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController; use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
use Pterodactyl\Http\Requests\Api\Application\Allocations\GetAllocationsRequest; use Pterodactyl\Http\Requests\Api\Application\Allocations\GetAllocationsRequest;
@ -26,41 +25,32 @@ class AllocationController extends ApplicationApiController
*/ */
private $deletionService; private $deletionService;
/**
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
*/
private $repository;
/** /**
* AllocationController constructor. * AllocationController constructor.
* *
* @param \Pterodactyl\Services\Allocations\AssignmentService $assignmentService * @param \Pterodactyl\Services\Allocations\AssignmentService $assignmentService
* @param \Pterodactyl\Services\Allocations\AllocationDeletionService $deletionService * @param \Pterodactyl\Services\Allocations\AllocationDeletionService $deletionService
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $repository
*/ */
public function __construct( public function __construct(
AssignmentService $assignmentService, AssignmentService $assignmentService,
AllocationDeletionService $deletionService, AllocationDeletionService $deletionService
AllocationRepositoryInterface $repository
) { ) {
parent::__construct(); parent::__construct();
$this->assignmentService = $assignmentService; $this->assignmentService = $assignmentService;
$this->deletionService = $deletionService; $this->deletionService = $deletionService;
$this->repository = $repository;
} }
/** /**
* Return all of the allocations that exist for a given node. * Return all of the allocations that exist for a given node.
* *
* @param \Pterodactyl\Http\Requests\Api\Application\Allocations\GetAllocationsRequest $request * @param \Pterodactyl\Http\Requests\Api\Application\Allocations\GetAllocationsRequest $request
* @param \Pterodactyl\Models\Node $node
* @return array * @return array
*/ */
public function index(GetAllocationsRequest $request): array public function index(GetAllocationsRequest $request, Node $node): array
{ {
$allocations = $this->repository->getPaginatedAllocationsForNode( $allocations = $node->allocations()->paginate(50);
$request->getModel(Node::class)->id, 50
);
return $this->fractal->collection($allocations) return $this->fractal->collection($allocations)
->transformWith($this->getTransformer(AllocationTransformer::class)) ->transformWith($this->getTransformer(AllocationTransformer::class))
@ -71,32 +61,35 @@ class AllocationController extends ApplicationApiController
* Store new allocations for a given node. * Store new allocations for a given node.
* *
* @param \Pterodactyl\Http\Requests\Api\Application\Allocations\StoreAllocationRequest $request * @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\CidrOutOfRangeException
* @throws \Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException * @throws \Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException
* @throws \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException * @throws \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException
* @throws \Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException * @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. * Delete a specific allocation from the Panel.
* *
* @param \Pterodactyl\Http\Requests\Api\Application\Allocations\DeleteAllocationRequest $request * @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 * @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);
} }
} }

View file

@ -5,7 +5,6 @@ namespace Pterodactyl\Repositories\Eloquent;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Pterodactyl\Models\Allocation; use Pterodactyl\Models\Allocation;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
class AllocationRepository extends EloquentRepository implements AllocationRepositoryInterface class AllocationRepository extends EloquentRepository implements AllocationRepositoryInterface
@ -20,41 +19,6 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
return Allocation::class; 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. * Return all of the unique IPs that exist for a given node.
* *

View file

@ -270,7 +270,9 @@ class ServerCreationService
$records = array_merge($records, $data['allocation_additional']); $records = array_merge($records, $data['allocation_additional']);
} }
$this->allocationRepository->assignAllocationsToServer($server->id, $records); $this->allocationRepository->updateWhereIn('id', $records, [
'server_id' => $server->id,
]);
} }
/** /**