Add more application api routes for Nests

This commit is contained in:
Matthew Penner 2021-01-03 16:45:07 -07:00
parent b1d30c1bde
commit 0511f75747
7 changed files with 205 additions and 5 deletions

View file

@ -3,9 +3,17 @@
namespace Pterodactyl\Http\Controllers\Api\Application\Nests;
use Pterodactyl\Models\Nest;
use Illuminate\Http\JsonResponse;
use Pterodactyl\Services\Nests\NestUpdateService;
use Pterodactyl\Services\Nests\NestCreationService;
use Pterodactyl\Services\Nests\NestDeletionService;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
use Pterodactyl\Transformers\Api\Application\NestTransformer;
use Pterodactyl\Http\Requests\Api\Application\Nests\GetNestRequest;
use Pterodactyl\Http\Requests\Api\Application\Nests\GetNestsRequest;
use Pterodactyl\Http\Requests\Api\Application\Nests\StoreNestRequest;
use Pterodactyl\Http\Requests\Api\Application\Nests\UpdateNestRequest;
use Pterodactyl\Http\Requests\Api\Application\Nests\DeleteNestRequest;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
class NestController extends ApplicationApiController
@ -15,16 +23,42 @@ class NestController extends ApplicationApiController
*/
private $repository;
/**
* @var \Pterodactyl\Services\Nests\NestCreationService
*/
protected $nestCreationService;
/**
* @var \Pterodactyl\Services\Nests\NestDeletionService
*/
protected $nestDeletionService;
/**
* @var \Pterodactyl\Services\Nests\NestUpdateService
*/
protected $nestUpdateService;
/**
* NestController constructor.
*
* @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $repository
* @param \Pterodactyl\Services\Nests\NestCreationService $nestCreationService
* @param \Pterodactyl\Services\Nests\NestDeletionService $nestDeletionService
* @param \Pterodactyl\Services\Nests\NestUpdateService $nestUpdateService
*/
public function __construct(NestRepositoryInterface $repository)
{
public function __construct(
NestRepositoryInterface $repository,
NestCreationService $nestCreationService,
NestDeletionService $nestDeletionService,
NestUpdateService $nestUpdateService
) {
parent::__construct();
$this->repository = $repository;
$this->nestCreationService = $nestCreationService;
$this->nestDeletionService = $nestDeletionService;
$this->nestUpdateService = $nestUpdateService;
}
/**
@ -47,16 +81,70 @@ class NestController extends ApplicationApiController
/**
* Return information about a single Nest model.
*
* @param \Pterodactyl\Http\Requests\Api\Application\Nests\GetNestsRequest $request
* @param \Pterodactyl\Http\Requests\Api\Application\Nests\GetNestRequest $request
* @param \Pterodactyl\Models\Nest $nest
*
* @return array
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function view(GetNestsRequest $request, Nest $nest): array
public function view(GetNestRequest $request, Nest $nest): array
{
return $this->fractal->item($nest)
->transformWith($this->getTransformer(NestTransformer::class))
->toArray();
}
/**
* Creates a new nest.
*
* @param \Pterodactyl\Http\Requests\Api\Application\Nests\StoreNestRequest $request
*
* @return array
* @throws \Illuminate\Contracts\Container\BindingResolutionException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function store(StoreNestRequest $request): array
{
$nest = $this->nestCreationService->handle($request->validated());
return $this->fractal->item($nest)
->transformWith($this->getTransformer(NestTransformer::class))
->toArray();
}
/**
* Updates an existing nest.
*
* @param \Pterodactyl\Http\Requests\Api\Application\Nests\UpdateNestRequest $request
* @param \Pterodactyl\Models\Nest $nest
*
* @return array
* @throws \Illuminate\Contracts\Container\BindingResolutionException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function update(UpdateNestRequest $request, Nest $nest): array
{
$this->nestUpdateService->handle($nest->id, $request->validated());
return $this->fractal->item($nest)
->transformWith($this->getTransformer(NestTransformer::class))
->toArray();
}
/**
* Deletes an existing nest.
*
* @param \Pterodactyl\Http\Requests\Api\Application\Nests\DeleteNestRequest $request
* @param \Pterodactyl\Models\Nest $nest
*
* @return \Illuminate\Http\JsonResponse
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException
*/
public function delete(DeleteNestRequest $request, Nest $nest): JsonResponse
{
$this->nestDeletionService->handle($nest->id);
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace Pterodactyl\Http\Requests\Api\Application\Nests;
use Pterodactyl\Models\Nest;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
class DeleteNestRequest extends ApplicationApiRequest
{
/**
* @var string
*/
protected $resource = AdminAcl::RESOURCE_NESTS;
/**
* @var int
*/
protected $permission = AdminAcl::WRITE;
/**
* Determine if the requested role exists on the Panel.
*
* @return bool
*/
public function resourceExists(): bool
{
$nest = $this->route()->parameter('nest');
return $nest instanceof Nest && $nest->exists;
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace Pterodactyl\Http\Requests\Api\Application\Nests;
use Pterodactyl\Models\Nest;
class GetNestRequest extends GetNestsRequest
{
/**
* Determine if the requested role exists on the Panel.
*
* @return bool
*/
public function resourceExists(): bool
{
$nest = $this->route()->parameter('nest');
return $nest instanceof Nest && $nest->exists;
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace Pterodactyl\Http\Requests\Api\Application\Nests;
use Pterodactyl\Models\Nest;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
class StoreNestRequest extends ApplicationApiRequest
{
/**
* @var string
*/
protected $resource = AdminAcl::RESOURCE_NESTS;
/**
* @var int
*/
protected $permission = AdminAcl::WRITE;
/**
* ?
*
* @param array|null $rules
*
* @return array
*/
public function rules(array $rules = null): array
{
return $rules ?? Nest::getRules();
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace Pterodactyl\Http\Requests\Api\Application\Nests;
use Pterodactyl\Models\Nest;
class UpdateNestRequest extends StoreNestRequest
{
/**
* ?
*
* @param array|null $rules
*
* @return array
*/
public function rules(array $rules = null): array
{
return $rules ?? Nest::getRulesForUpdate($this->route()->parameter('nest')->id);
}
}

View file

@ -43,7 +43,7 @@ class Nest extends Model
* @var array
*/
public static $validationRules = [
'author' => 'required|string|email',
'author' => 'sometimes|string|email',
'name' => 'required|string|max:191',
'description' => 'nullable|string',
];

View file

@ -114,6 +114,12 @@ Route::group(['prefix' => '/nests'], function () {
Route::get('/', 'Nests\NestController@index')->name('api.application.nests');
Route::get('/{nest}', 'Nests\NestController@view')->name('api.application.nests.view');
Route::post('/', 'Nests\NestController@store');
Route::patch('/{nest}', 'Nests\NestController@update');
Route::delete('/{nest}', 'Nests\NestController@delete');
// Egg Management Endpoint
Route::group(['prefix' => '/{nest}/eggs'], function () {
Route::get('/', 'Nests\EggController@index')->name('api.application.nests.eggs');
@ -136,5 +142,7 @@ Route::group(['prefix' => '/roles'], function () {
Route::post('/', 'Roles\RoleController@store');
Route::patch('/{role}', 'Roles\RoleController@update');
Route::delete('/{role}', 'Roles\RoleController@delete');
});