Files
panel/app/Http/Controllers/Admin/DatabaseController.php

127 lines
4.0 KiB
PHP
Raw Normal View History

<?php
2016-12-07 22:46:38 +00:00
2024-03-12 22:39:16 -04:00
namespace App\Http\Controllers\Admin;
2024-03-14 02:23:30 -04:00
use App\Models\Node;
use Illuminate\View\View;
2024-03-12 22:39:16 -04:00
use App\Models\DatabaseHost;
use Illuminate\Http\RedirectResponse;
use Prologue\Alerts\AlertsMessageBag;
2024-03-12 22:39:16 -04:00
use App\Http\Controllers\Controller;
use App\Services\Databases\Hosts\HostUpdateService;
use App\Http\Requests\Admin\DatabaseHostFormRequest;
use App\Services\Databases\Hosts\HostCreationService;
use App\Services\Databases\Hosts\HostDeletionService;
class DatabaseController extends Controller
{
/**
* DatabaseController constructor.
*/
public function __construct(
private AlertsMessageBag $alert,
private HostCreationService $creationService,
private HostDeletionService $deletionService,
private HostUpdateService $updateService,
) {
}
/**
2017-03-16 19:35:29 -04:00
* Display database host index.
*/
public function index(): View
{
2024-03-16 22:41:36 -04:00
$hosts = DatabaseHost::query()
->withCount('databases')
->with('node')
->get();
2024-03-16 23:23:07 -04:00
return view('admin.databases.index', [
2024-03-14 02:23:30 -04:00
'nodes' => Node::all(),
2024-03-16 22:41:36 -04:00
'hosts' => $hosts,
]);
}
2017-03-16 19:35:29 -04:00
/**
* Display database host to user.
*/
2024-03-19 04:30:26 -04:00
public function view(DatabaseHost $host): View
{
2024-03-16 20:56:37 -04:00
$databases = $host->databases()->with('server')->paginate(25);
2024-03-16 23:23:07 -04:00
return view('admin.databases.view', [
2024-03-16 20:56:37 -04:00
'nodes' => Node::all(),
'host' => $host,
'databases' => $databases,
]);
}
2017-03-16 19:35:29 -04:00
/**
* Handle request to create a new database host.
2017-03-16 19:35:29 -04:00
*
* @throws \Throwable
2017-03-16 19:35:29 -04:00
*/
public function create(DatabaseHostFormRequest $request): RedirectResponse
{
try {
$host = $this->creationService->handle($request->normalize());
2023-02-23 12:30:16 -07:00
} catch (\Exception $exception) {
if ($exception instanceof \PDOException || $exception->getPrevious() instanceof \PDOException) {
$this->alert->danger(
sprintf('There was an error while trying to connect to the host or while executing a query: "%s"', $exception->getMessage())
)->flash();
return redirect()->route('admin.databases')->withInput($request->validated());
} else {
throw $exception;
}
}
2017-03-16 19:35:29 -04:00
$this->alert->success('Successfully created a new database host on the system.')->flash();
return redirect()->route('admin.databases.view', $host->id);
}
2017-03-16 19:35:29 -04:00
/**
* Handle updating database host.
2017-03-16 19:35:29 -04:00
*
* @throws \Throwable
2017-03-16 19:35:29 -04:00
*/
public function update(DatabaseHostFormRequest $request, DatabaseHost $host): RedirectResponse
{
$redirect = redirect()->route('admin.databases.view', $host->id);
try {
$this->updateService->handle($host->id, $request->normalize());
$this->alert->success('Database host was updated successfully.')->flash();
2023-02-23 12:30:16 -07:00
} catch (\Exception $exception) {
// Catch any SQL related exceptions and display them back to the user, otherwise just
// throw the exception like normal and move on with it.
2023-02-23 12:30:16 -07:00
if ($exception instanceof \PDOException || $exception->getPrevious() instanceof \PDOException) {
$this->alert->danger(
sprintf('There was an error while trying to connect to the host or while executing a query: "%s"', $exception->getMessage())
)->flash();
return $redirect->withInput($request->normalize());
} else {
throw $exception;
}
}
2017-03-16 19:35:29 -04:00
return $redirect;
}
/**
* Handle request to delete a database host.
*
2024-03-12 22:39:16 -04:00
* @throws \App\Exceptions\Service\HasActiveServersException
*/
public function delete(int $host): RedirectResponse
{
$this->deletionService->handle($host);
$this->alert->success('The requested database host has been deleted from the system.')->flash();
return redirect()->route('admin.databases');
}
}