Path: blob/1.0-develop/app/Http/Controllers/Admin/DatabaseController.php
10284 views
<?php12namespace Pterodactyl\Http\Controllers\Admin;34use Illuminate\View\View;5use Pterodactyl\Models\DatabaseHost;6use Illuminate\Http\RedirectResponse;7use Prologue\Alerts\AlertsMessageBag;8use Pterodactyl\Http\Controllers\Controller;9use Pterodactyl\Services\Databases\Hosts\HostUpdateService;10use Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest;11use Pterodactyl\Services\Databases\Hosts\HostCreationService;12use Pterodactyl\Services\Databases\Hosts\HostDeletionService;13use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;14use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;15use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;1617class DatabaseController extends Controller18{19/**20* DatabaseController constructor.21*/22public function __construct(23private AlertsMessageBag $alert,24private DatabaseHostRepositoryInterface $repository,25private DatabaseRepositoryInterface $databaseRepository,26private HostCreationService $creationService,27private HostDeletionService $deletionService,28private HostUpdateService $updateService,29private LocationRepositoryInterface $locationRepository,30) {31}3233/**34* Display database host index.35*/36public function index(): View37{38return view('admin.databases.index', [39'locations' => $this->locationRepository->getAllWithNodes(),40'hosts' => $this->repository->getWithViewDetails(),41]);42}4344/**45* Display database host to user.46*47* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException48*/49public function view(int $host): View50{51return view('admin.databases.view', [52'locations' => $this->locationRepository->getAllWithNodes(),53'host' => $this->repository->find($host),54'databases' => $this->databaseRepository->getDatabasesForHost($host),55]);56}5758/**59* Handle request to create a new database host.60*61* @throws \Throwable62*/63public function create(DatabaseHostFormRequest $request): RedirectResponse64{65try {66$host = $this->creationService->handle($request->normalize());67} catch (\Exception $exception) {68if ($exception instanceof \PDOException || $exception->getPrevious() instanceof \PDOException) {69$this->alert->danger(70sprintf('There was an error while trying to connect to the host or while executing a query: "%s"', $exception->getMessage())71)->flash();7273return redirect()->route('admin.databases')->withInput($request->validated());74} else {75throw $exception;76}77}7879$this->alert->success('Successfully created a new database host on the system.')->flash();8081return redirect()->route('admin.databases.view', $host->id);82}8384/**85* Handle updating database host.86*87* @throws \Throwable88*/89public function update(DatabaseHostFormRequest $request, DatabaseHost $host): RedirectResponse90{91$redirect = redirect()->route('admin.databases.view', $host->id);9293try {94$this->updateService->handle($host->id, $request->normalize());95$this->alert->success('Database host was updated successfully.')->flash();96} catch (\Exception $exception) {97// Catch any SQL related exceptions and display them back to the user, otherwise just98// throw the exception like normal and move on with it.99if ($exception instanceof \PDOException || $exception->getPrevious() instanceof \PDOException) {100$this->alert->danger(101sprintf('There was an error while trying to connect to the host or while executing a query: "%s"', $exception->getMessage())102)->flash();103104return $redirect->withInput($request->normalize());105} else {106throw $exception;107}108}109110return $redirect;111}112113/**114* Handle request to delete a database host.115*116* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException117*/118public function delete(int $host): RedirectResponse119{120$this->deletionService->handle($host);121$this->alert->success('The requested database host has been deleted from the system.')->flash();122123return redirect()->route('admin.databases');124}125}126127128