From a71392d63229ce8eda638e1035b43caba3b1f777 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Thu, 14 Jan 2021 11:04:19 -0700 Subject: [PATCH] api(application): add endpoints for (de)attaching eggs and nodes on mounts --- .../Application/Mounts/MountController.php | 90 +++++++++++++++++++ .../Mounts/MountAddEggsRequest.php | 31 +++++++ .../Mounts/MountAddNodesRequest.php | 31 +++++++ routes/api-application.php | 5 ++ 4 files changed, 157 insertions(+) create mode 100644 app/Http/Requests/Api/Application/Mounts/MountAddEggsRequest.php create mode 100644 app/Http/Requests/Api/Application/Mounts/MountAddNodesRequest.php diff --git a/app/Http/Controllers/Api/Application/Mounts/MountController.php b/app/Http/Controllers/Api/Application/Mounts/MountController.php index fc5ecefaf..478cf6c77 100644 --- a/app/Http/Controllers/Api/Application/Mounts/MountController.php +++ b/app/Http/Controllers/Api/Application/Mounts/MountController.php @@ -12,6 +12,8 @@ use Pterodactyl\Http\Requests\Api\Application\Mounts\GetMountsRequest; use Pterodactyl\Http\Requests\Api\Application\Mounts\StoreMountRequest; use Pterodactyl\Http\Requests\Api\Application\Mounts\UpdateMountRequest; use Pterodactyl\Http\Requests\Api\Application\Mounts\DeleteMountRequest; +use Pterodactyl\Http\Requests\Api\Application\Mounts\MountAddEggsRequest; +use Pterodactyl\Http\Requests\Api\Application\Mounts\MountAddNodesRequest; use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController; class MountController extends ApplicationApiController @@ -117,4 +119,92 @@ class MountController extends ApplicationApiController return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT); } + + /** + * ? + * + * @param \Pterodactyl\Http\Requests\Api\Application\Mounts\MountAddEggsRequest $request + * @param \Pterodactyl\Models\Mount $mount + * + * @return array + */ + public function addEggs(MountAddEggsRequest $request, Mount $mount): array + { + $data = $request->validated(); + + $eggs = $data['eggs'] ?? []; + if (count($eggs) > 0) { + $mount->eggs()->syncWithoutDetaching($eggs); + } + + return $this->fractal->item($mount) + ->transformWith($this->getTransformer(MountTransformer::class)) + ->toArray(); + } + + /** + * ? + * + * @param \Pterodactyl\Http\Requests\Api\Application\Mounts\MountAddNodesRequest $request + * @param \Pterodactyl\Models\Mount $mount + * + * @return array + */ + public function addNodes(MountAddNodesRequest $request, Mount $mount): array + { + $data = $request->validated(); + + $nodes = $data['nodes'] ?? []; + if (count($nodes) > 0) { + $mount->nodes()->syncWithoutDetaching($nodes); + } + + return $this->fractal->item($mount) + ->transformWith($this->getTransformer(MountTransformer::class)) + ->toArray(); + } + + /** + * ? + * + * @param \Pterodactyl\Http\Requests\Api\Application\Mounts\MountAddEggsRequest $request + * @param \Pterodactyl\Models\Mount $mount + * + * @return array + */ + public function deleteEggs(MountAddEggsRequest $request, Mount $mount): array + { + $data = $request->validated(); + + $eggs = $data['eggs'] ?? []; + if (count($eggs) > 0) { + $mount->eggs()->detach($eggs); + } + + return $this->fractal->item($mount) + ->transformWith($this->getTransformer(MountTransformer::class)) + ->toArray(); + } + + /** + * ? + * + * @param \Pterodactyl\Http\Requests\Api\Application\Mounts\MountAddNodesRequest $request + * @param \Pterodactyl\Models\Mount $mount + * + * @return array + */ + public function deleteNodes(MountAddNodesRequest $request, Mount $mount): array + { + $data = $request->validated(); + + $nodes = $data['nodes'] ?? []; + if (count($nodes) > 0) { + $mount->nodes()->detach($nodes); + } + + return $this->fractal->item($mount) + ->transformWith($this->getTransformer(MountTransformer::class)) + ->toArray(); + } } diff --git a/app/Http/Requests/Api/Application/Mounts/MountAddEggsRequest.php b/app/Http/Requests/Api/Application/Mounts/MountAddEggsRequest.php new file mode 100644 index 000000000..26d257aa0 --- /dev/null +++ b/app/Http/Requests/Api/Application/Mounts/MountAddEggsRequest.php @@ -0,0 +1,31 @@ + 'required|exists:eggs,id']; + } +} diff --git a/app/Http/Requests/Api/Application/Mounts/MountAddNodesRequest.php b/app/Http/Requests/Api/Application/Mounts/MountAddNodesRequest.php new file mode 100644 index 000000000..94a90d89f --- /dev/null +++ b/app/Http/Requests/Api/Application/Mounts/MountAddNodesRequest.php @@ -0,0 +1,31 @@ + 'required|exists:nodes,id']; + } +} diff --git a/routes/api-application.php b/routes/api-application.php index 5702ccbb8..fd7253a02 100644 --- a/routes/api-application.php +++ b/routes/api-application.php @@ -74,9 +74,14 @@ Route::group(['prefix' => '/mounts'], function () { Route::post('/', 'Mounts\MountController@store'); + Route::put('/{mount}/eggs', 'Mounts\MountController@addEggs'); + Route::put('/{mount}/nodes', 'Mounts\MountController@addNodes'); + Route::patch('/{mount}', 'Mounts\MountController@update'); Route::delete('/{mount}', 'Mounts\MountController@delete'); + Route::delete('/{mount}/eggs', 'Mounts\MountController@deleteEggs'); + Route::delete('/{mount}/nodes', 'Mounts\MountController@deleteNodes'); }); /*