Break server creation logic into own controller

This commit is contained in:
Dane Everitt 2019-11-16 13:08:46 -08:00
parent 634c1cae9a
commit 2848d182ef
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
3 changed files with 134 additions and 85 deletions

View file

@ -0,0 +1,132 @@
<?php
namespace Pterodactyl\Http\Controllers\Admin\Servers;
use JavaScript;
use Illuminate\Http\RedirectResponse;
use Prologue\Alerts\AlertsMessageBag;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Repositories\Eloquent\NestRepository;
use Pterodactyl\Repositories\Eloquent\NodeRepository;
use Pterodactyl\Http\Requests\Admin\ServerFormRequest;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Services\Servers\ServerCreationService;
use Pterodactyl\Repositories\Eloquent\LocationRepository;
class CreateServerController extends Controller
{
/**
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
*/
private $repository;
/**
* @var \Pterodactyl\Repositories\Eloquent\NodeRepository
*/
private $nodeRepository;
/**
* @var \Prologue\Alerts\AlertsMessageBag
*/
private $alert;
/**
* @var \Pterodactyl\Repositories\Eloquent\NestRepository
*/
private $nestRepository;
/**
* @var \Pterodactyl\Repositories\Eloquent\LocationRepository
*/
private $locationRepository;
/**
* @var \Pterodactyl\Services\Servers\ServerCreationService
*/
private $creationService;
/**
* CreateServerController constructor.
*
* @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Repositories\Eloquent\NestRepository $nestRepository
* @param \Pterodactyl\Repositories\Eloquent\LocationRepository $locationRepository
* @param \Pterodactyl\Repositories\Eloquent\NodeRepository $nodeRepository
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
* @param \Pterodactyl\Services\Servers\ServerCreationService $creationService
*/
public function __construct(
AlertsMessageBag $alert,
NestRepository $nestRepository,
LocationRepository $locationRepository,
NodeRepository $nodeRepository,
ServerRepository $repository,
ServerCreationService $creationService
) {
$this->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);
}
}

View file

@ -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.
*

View file

@ -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');