You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.9 KiB
58 lines
1.9 KiB
<?php |
|
|
|
namespace App\Http\Controllers\Api\V1; |
|
|
|
use App\Http\Controllers\Api\Concerns\RespondsWithJson; |
|
use App\Http\Controllers\Controller; |
|
use App\Services\DatabaseConnectionManager; |
|
use Illuminate\Http\JsonResponse; |
|
use Illuminate\Http\Request; |
|
use InvalidArgumentException; |
|
use Throwable; |
|
|
|
class DatabaseController extends Controller |
|
{ |
|
use RespondsWithJson; |
|
|
|
public function __construct( |
|
private readonly DatabaseConnectionManager $connections |
|
) { |
|
} |
|
|
|
public function index(Request $request): JsonResponse |
|
{ |
|
$sort = $request->string('sort', 'created')->toString(); |
|
$dir = $request->string('dir', 'desc')->toString() === 'asc' ? 'asc' : 'desc'; |
|
$search = $request->string('q')->trim()->toString(); |
|
$connectionId = $request->integer('connection') ?: null; |
|
|
|
$allowedSort = ['db_name', 'created', 'active_days', 'state', 'last_index_activity', 'last_backup']; |
|
if (! in_array($sort, $allowedSort, true)) { |
|
$sort = 'created'; |
|
} |
|
|
|
$result = $this->connections->listerFor($connectionId)->listDatabases($sort, $dir, $search); |
|
|
|
if ($result['error'] ?? null) { |
|
return $this->fail($result['error'], 'service_error', 503); |
|
} |
|
|
|
return $this->ok($result['rows'], $result['meta']); |
|
} |
|
|
|
public function offline(Request $request, string $database): JsonResponse |
|
{ |
|
$connectionId = $request->integer('connection') ?: null; |
|
|
|
try { |
|
$lister = $this->connections->listerFor($connectionId); |
|
$lister->makeOffline($database); |
|
|
|
return $this->ok(['database' => $database, 'state' => 'RESTRICTED']); |
|
} catch (InvalidArgumentException $e) { |
|
return $this->fail($e->getMessage(), 'validation_error', 422); |
|
} catch (Throwable) { |
|
return $this->fail('Failed to restrict database.', 'server_error', 500); |
|
} |
|
} |
|
}
|
|
|