From 2848d182efe60686f41d1e1b0f952555bc198136 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 16 Nov 2019 13:08:46 -0800 Subject: [PATCH] Break server creation logic into own controller --- .../Admin/Servers/CreateServerController.php | 132 ++++++++++++++++++ .../Controllers/Admin/ServersController.php | 83 ----------- routes/admin.php | 4 +- 3 files changed, 134 insertions(+), 85 deletions(-) create mode 100644 app/Http/Controllers/Admin/Servers/CreateServerController.php diff --git a/app/Http/Controllers/Admin/Servers/CreateServerController.php b/app/Http/Controllers/Admin/Servers/CreateServerController.php new file mode 100644 index 000000000..eb8f74da6 --- /dev/null +++ b/app/Http/Controllers/Admin/Servers/CreateServerController.php @@ -0,0 +1,132 @@ +repository = $repository; + $this->nodeRepository = $nodeRepository; + $this->alert = $alert; + $this->nestRepository = $nestRepository; + $this->locationRepository = $locationRepository; + $this->creationService = $creationService; + } + + /** + * Displays the create server page. + * + * @return \Illuminate\Contracts\View\Factory + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException + */ + public function index() + { + $nodes = $this->nodeRepository->all(); + if (count($nodes) < 1) { + $this->alert->warning(trans('admin/server.alerts.node_required'))->flash(); + + return redirect()->route('admin.nodes'); + } + + $nests = $this->nestRepository->getWithEggs(); + + Javascript::put([ + 'nodeData' => $this->nodeRepository->getNodesForServerCreation(), + 'nests' => $nests->map(function ($item) { + return array_merge($item->toArray(), [ + 'eggs' => $item->eggs->keyBy('id')->toArray(), + ]); + })->keyBy('id'), + ]); + + return view('admin.servers.new', [ + 'locations' => $this->locationRepository->all(), + 'nests' => $nests, + ]); + } + + /** + * Create a new server on the remote system. + * + * @param \Pterodactyl\Http\Requests\Admin\ServerFormRequest $request + * @return \Illuminate\Http\RedirectResponse + * + * @throws \Illuminate\Validation\ValidationException + * @throws \Pterodactyl\Exceptions\DisplayException + * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException + * @throws \Pterodactyl\Exceptions\Model\DataValidationException + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException + * @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException + * @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException + */ + public function store(ServerFormRequest $request) + { + $server = $this->creationService->handle( + $request->validated() + ); + + $this->alert->success( + trans('admin/server.alerts.server_created') + )->flash(); + + return RedirectResponse::create('/admin/servers/view/' . $server->id); + } +} diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index 3e5e1542c..c89d9a621 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -17,8 +17,6 @@ use Prologue\Alerts\AlertsMessageBag; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Services\Servers\SuspensionService; -use Pterodactyl\Http\Requests\Admin\ServerFormRequest; -use Pterodactyl\Services\Servers\ServerCreationService; use Pterodactyl\Services\Servers\ServerDeletionService; use Pterodactyl\Services\Servers\ReinstallServerService; use Pterodactyl\Services\Servers\ContainerRebuildService; @@ -27,13 +25,11 @@ use Pterodactyl\Services\Databases\DatabasePasswordService; use Pterodactyl\Services\Servers\DetailsModificationService; use Pterodactyl\Services\Servers\StartupModificationService; use Pterodactyl\Contracts\Repository\NestRepositoryInterface; -use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository; use Pterodactyl\Services\Databases\DatabaseManagementService; use Illuminate\Contracts\Config\Repository as ConfigRepository; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; -use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Http\Requests\Admin\Servers\Databases\StoreServerDatabaseRequest; @@ -94,21 +90,11 @@ class ServersController extends Controller */ protected $detailsModificationService; - /** - * @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface - */ - protected $locationRepository; - /** * @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface */ protected $nestRepository; - /** - * @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface - */ - protected $nodeRepository; - /** * @var \Pterodactyl\Services\Servers\ReinstallServerService */ @@ -119,11 +105,6 @@ class ServersController extends Controller */ protected $repository; - /** - * @var \Pterodactyl\Services\Servers\ServerCreationService - */ - protected $service; - /** * @var \Pterodactyl\Services\Servers\StartupModificationService */ @@ -142,15 +123,12 @@ class ServersController extends Controller * @param \Pterodactyl\Services\Servers\BuildModificationService $buildModificationService * @param \Illuminate\Contracts\Config\Repository $config * @param \Pterodactyl\Services\Servers\ContainerRebuildService $containerRebuildService - * @param \Pterodactyl\Services\Servers\ServerCreationService $service * @param \Pterodactyl\Services\Databases\DatabaseManagementService $databaseManagementService * @param \Pterodactyl\Services\Databases\DatabasePasswordService $databasePasswordService * @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository * @param \Pterodactyl\Repositories\Eloquent\DatabaseHostRepository $databaseHostRepository * @param \Pterodactyl\Services\Servers\ServerDeletionService $deletionService * @param \Pterodactyl\Services\Servers\DetailsModificationService $detailsModificationService - * @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $locationRepository - * @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository * @param \Pterodactyl\Services\Servers\ReinstallServerService $reinstallService * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository * @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $nestRepository @@ -163,15 +141,12 @@ class ServersController extends Controller BuildModificationService $buildModificationService, ConfigRepository $config, ContainerRebuildService $containerRebuildService, - ServerCreationService $service, DatabaseManagementService $databaseManagementService, DatabasePasswordService $databasePasswordService, DatabaseRepositoryInterface $databaseRepository, DatabaseHostRepository $databaseHostRepository, ServerDeletionService $deletionService, DetailsModificationService $detailsModificationService, - LocationRepositoryInterface $locationRepository, - NodeRepositoryInterface $nodeRepository, ReinstallServerService $reinstallService, ServerRepositoryInterface $repository, NestRepositoryInterface $nestRepository, @@ -189,12 +164,9 @@ class ServersController extends Controller $this->databaseRepository = $databaseRepository; $this->detailsModificationService = $detailsModificationService; $this->deletionService = $deletionService; - $this->locationRepository = $locationRepository; $this->nestRepository = $nestRepository; - $this->nodeRepository = $nodeRepository; $this->reinstallService = $reinstallService; $this->repository = $repository; - $this->service = $service; $this->startupModificationService = $startupModificationService; $this->suspensionService = $suspensionService; } @@ -214,61 +186,6 @@ class ServersController extends Controller ]); } - /** - * Display create new server page. - * - * @return \Illuminate\View\View - * - * @throws \Exception - */ - public function create() - { - $nodes = $this->nodeRepository->all(); - if (count($nodes) < 1) { - $this->alert->warning(trans('admin/server.alerts.node_required'))->flash(); - - return redirect()->route('admin.nodes'); - } - - $nests = $this->nestRepository->getWithEggs(); - - Javascript::put([ - 'nodeData' => $this->nodeRepository->getNodesForServerCreation(), - 'nests' => $nests->map(function ($item) { - return array_merge($item->toArray(), [ - 'eggs' => $item->eggs->keyBy('id')->toArray(), - ]); - })->keyBy('id'), - ]); - - return view('admin.servers.new', [ - 'locations' => $this->locationRepository->all(), - 'nests' => $nests, - ]); - } - - /** - * Handle POST of server creation form. - * - * @param \Pterodactyl\Http\Requests\Admin\ServerFormRequest $request - * @return \Illuminate\Http\RedirectResponse - * - * @throws \Illuminate\Validation\ValidationException - * @throws \Pterodactyl\Exceptions\DisplayException - * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException - * @throws \Pterodactyl\Exceptions\Model\DataValidationException - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException - * @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException - * @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException - */ - public function store(ServerFormRequest $request) - { - $server = $this->service->handle($request->except('_token')); - $this->alert->success(trans('admin/server.alerts.server_created'))->flash(); - - return redirect()->route('admin.servers.view', $server->id); - } - /** * Display the index when viewing a specific server. * diff --git a/routes/admin.php b/routes/admin.php index e9d78cdaa..2d099760c 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -102,7 +102,7 @@ Route::group(['prefix' => 'users'], function () { */ Route::group(['prefix' => 'servers'], function () { Route::get('/', 'ServersController@index')->name('admin.servers'); - Route::get('/new', 'ServersController@create')->name('admin.servers.new'); + Route::get('/new', 'Servers\CreateServerController@index')->name('admin.servers.new'); Route::get('/view/{server}', 'ServersController@viewIndex')->name('admin.servers.view'); Route::get('/view/{server}/details', 'ServersController@viewDetails')->name('admin.servers.view.details'); Route::get('/view/{server}/build', 'ServersController@viewBuild')->name('admin.servers.view.build'); @@ -111,7 +111,7 @@ Route::group(['prefix' => 'servers'], function () { Route::get('/view/{server}/manage', 'ServersController@viewManage')->name('admin.servers.view.manage'); Route::get('/view/{server}/delete', 'ServersController@viewDelete')->name('admin.servers.view.delete'); - Route::post('/new', 'ServersController@store'); + Route::post('/new', 'Servers\CreateServerController@store'); Route::post('/view/{server}/build', 'ServersController@updateBuild'); Route::post('/view/{server}/startup', 'ServersController@saveStartup'); Route::post('/view/{server}/database', 'ServersController@newDatabase');