diff --git a/app/Http/Controllers/Api/Application/Nests/NestController.php b/app/Http/Controllers/Api/Application/Nests/NestController.php index 87f3074c4..7abfc174a 100644 --- a/app/Http/Controllers/Api/Application/Nests/NestController.php +++ b/app/Http/Controllers/Api/Application/Nests/NestController.php @@ -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); + } } diff --git a/app/Http/Requests/Api/Application/Nests/DeleteNestRequest.php b/app/Http/Requests/Api/Application/Nests/DeleteNestRequest.php new file mode 100644 index 000000000..13bc6a5e4 --- /dev/null +++ b/app/Http/Requests/Api/Application/Nests/DeleteNestRequest.php @@ -0,0 +1,32 @@ +route()->parameter('nest'); + + return $nest instanceof Nest && $nest->exists; + } +} diff --git a/app/Http/Requests/Api/Application/Nests/GetNestRequest.php b/app/Http/Requests/Api/Application/Nests/GetNestRequest.php new file mode 100644 index 000000000..958541362 --- /dev/null +++ b/app/Http/Requests/Api/Application/Nests/GetNestRequest.php @@ -0,0 +1,20 @@ +route()->parameter('nest'); + + return $nest instanceof Nest && $nest->exists; + } +} diff --git a/app/Http/Requests/Api/Application/Nests/StoreNestRequest.php b/app/Http/Requests/Api/Application/Nests/StoreNestRequest.php new file mode 100644 index 000000000..5168dcfcd --- /dev/null +++ b/app/Http/Requests/Api/Application/Nests/StoreNestRequest.php @@ -0,0 +1,32 @@ +route()->parameter('nest')->id); + } +} diff --git a/app/Models/Nest.php b/app/Models/Nest.php index 770c5baf7..c3d08d860 100644 --- a/app/Models/Nest.php +++ b/app/Models/Nest.php @@ -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', ]; diff --git a/routes/api-application.php b/routes/api-application.php index 61b794927..66c12a33e 100644 --- a/routes/api-application.php +++ b/routes/api-application.php @@ -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'); });