Add location control through API
This commit is contained in:
parent
15289b76a7
commit
f32cee3ae5
4 changed files with 176 additions and 19 deletions
147
app/Http/Controllers/API/Admin/Locations/LocationController.php
Normal file
147
app/Http/Controllers/API/Admin/Locations/LocationController.php
Normal file
|
@ -0,0 +1,147 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Http\Controllers\API\Admin\Locations;
|
||||
|
||||
use Spatie\Fractal\Fractal;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Pterodactyl\Models\Location;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Pterodactyl\Http\Controllers\Controller;
|
||||
use Pterodactyl\Http\Requests\Admin\LocationFormRequest;
|
||||
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
||||
use Pterodactyl\Services\Locations\LocationUpdateService;
|
||||
use Pterodactyl\Services\Locations\LocationCreationService;
|
||||
use Pterodactyl\Services\Locations\LocationDeletionService;
|
||||
use Pterodactyl\Transformers\Api\Admin\LocationTransformer;
|
||||
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
|
||||
|
||||
class LocationController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Locations\LocationCreationService
|
||||
*/
|
||||
private $creationService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Locations\LocationDeletionService
|
||||
*/
|
||||
private $deletionService;
|
||||
|
||||
/**
|
||||
* @var \Spatie\Fractal\Fractal
|
||||
*/
|
||||
private $fractal;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Locations\LocationUpdateService
|
||||
*/
|
||||
private $updateService;
|
||||
|
||||
/**
|
||||
* LocationController constructor.
|
||||
*
|
||||
* @param \Spatie\Fractal\Fractal $fractal
|
||||
* @param \Pterodactyl\Services\Locations\LocationCreationService $creationService
|
||||
* @param \Pterodactyl\Services\Locations\LocationDeletionService $deletionService
|
||||
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $repository
|
||||
* @param \Pterodactyl\Services\Locations\LocationUpdateService $updateService
|
||||
*/
|
||||
public function __construct(
|
||||
Fractal $fractal,
|
||||
LocationCreationService $creationService,
|
||||
LocationDeletionService $deletionService,
|
||||
LocationRepositoryInterface $repository,
|
||||
LocationUpdateService $updateService
|
||||
) {
|
||||
$this->creationService = $creationService;
|
||||
$this->deletionService = $deletionService;
|
||||
$this->fractal = $fractal;
|
||||
$this->repository = $repository;
|
||||
$this->updateService = $updateService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all of the locations currently registered on the Panel.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function index(Request $request): array
|
||||
{
|
||||
$locations = $this->repository->all(50);
|
||||
|
||||
return $this->fractal->collection($locations)
|
||||
->transformWith(new LocationTransformer($request))
|
||||
->withResourceName('location')
|
||||
->paginateWith(new IlluminatePaginatorAdapter($locations))
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a single location.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Pterodactyl\Models\Location $location
|
||||
* @return array
|
||||
*/
|
||||
public function view(Request $request, Location $location): array
|
||||
{
|
||||
return $this->fractal->item($location)
|
||||
->transformWith(new LocationTransformer($request))
|
||||
->withResourceName('location')
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Pterodactyl\Http\Requests\Admin\LocationFormRequest $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
*/
|
||||
public function store(LocationFormRequest $request): JsonResponse
|
||||
{
|
||||
$location = $this->creationService->handle($request->normalize());
|
||||
|
||||
return $this->fractal->item($location)
|
||||
->transformWith(new LocationTransformer($request))
|
||||
->withResourceName('location')
|
||||
->respond(201);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Pterodactyl\Http\Requests\Admin\LocationFormRequest $request
|
||||
* @param \Pterodactyl\Models\Location $location
|
||||
* @return array
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function update(LocationFormRequest $request, Location $location): array
|
||||
{
|
||||
$location = $this->updateService->handle($location, $request->normalize());
|
||||
|
||||
return $this->fractal->item($location)
|
||||
->transformWith(new LocationTransformer($request))
|
||||
->withResourceName('location')
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Pterodactyl\Models\Location $location
|
||||
* @return \Illuminate\Http\Response
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException
|
||||
*/
|
||||
public function delete(Location $location): Response
|
||||
{
|
||||
$this->deletionService->handle($location);
|
||||
|
||||
return response('', 204);
|
||||
}
|
||||
}
|
|
@ -78,16 +78,11 @@ class UserController extends Controller
|
|||
{
|
||||
$users = $this->repository->all(config('pterodactyl.paginate.api.users'));
|
||||
|
||||
$fractal = $this->fractal->collection($users)
|
||||
return $this->fractal->collection($users)
|
||||
->transformWith(new UserTransformer($request))
|
||||
->withResourceName('user')
|
||||
->paginateWith(new IlluminatePaginatorAdapter($users));
|
||||
|
||||
if (config('pterodactyl.api.include_on_list') && $request->filled('include')) {
|
||||
$fractal->parseIncludes(explode(',', $request->input('include')));
|
||||
}
|
||||
|
||||
return $fractal->toArray();
|
||||
->paginateWith(new IlluminatePaginatorAdapter($users))
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -100,15 +95,10 @@ class UserController extends Controller
|
|||
*/
|
||||
public function view(Request $request, User $user): array
|
||||
{
|
||||
$fractal = $this->fractal->item($user)
|
||||
return $this->fractal->item($user)
|
||||
->transformWith(new UserTransformer($request))
|
||||
->withResourceName('user');
|
||||
|
||||
if ($request->filled('include')) {
|
||||
$fractal->parseIncludes(explode(',', $request->input('include')));
|
||||
}
|
||||
|
||||
return $fractal->toArray();
|
||||
->withResourceName('user')
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue