mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-02 14:36:17 +00:00
init
This commit is contained in:
58
app/Actions/Database/CreateDatabase.php
Executable file
58
app/Actions/Database/CreateDatabase.php
Executable file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Database;
|
||||
|
||||
use App\Models\Database;
|
||||
use App\Models\Server;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class CreateDatabase
|
||||
{
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function create(Server $server, array $input): Database
|
||||
{
|
||||
$this->validate($server, $input);
|
||||
|
||||
$database = new Database([
|
||||
'server_id' => $server->id,
|
||||
'name' => $input['name'],
|
||||
]);
|
||||
$database->save();
|
||||
$database->createOnServer();
|
||||
|
||||
return $database;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
private function validate(Server $server, array $input): void
|
||||
{
|
||||
$rules = [
|
||||
'name' => [
|
||||
'required',
|
||||
'alpha_dash',
|
||||
Rule::unique('databases', 'name')->where('server_id', $server->id),
|
||||
],
|
||||
];
|
||||
if (isset($input['user']) && $input['user']) {
|
||||
$rules['username'] = [
|
||||
'required',
|
||||
'alpha_dash',
|
||||
Rule::unique('database_users', 'username')->where('server_id', $server->id),
|
||||
];
|
||||
$rules['password'] = [
|
||||
'required',
|
||||
'min:6',
|
||||
];
|
||||
}
|
||||
if (isset($input['remote']) && $input['remote']) {
|
||||
$rules['host'] = 'required';
|
||||
}
|
||||
Validator::make($input, $rules)->validate();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user