setup.php error handeling
This commit is contained in:
parent
bcb6bf0bf4
commit
1365c00c78
|
@ -17,13 +17,15 @@ function myDB(string $servername, string $username, string $password, string $DB
|
||||||
$conn = new mysqli($servername, $username, $password);
|
$conn = new mysqli($servername, $username, $password);
|
||||||
|
|
||||||
if ($conn->connect_error) {
|
if ($conn->connect_error) {
|
||||||
die("Connection failed: " . $conn->connect_error);
|
http_response_code(400);
|
||||||
|
echo json_encode(["message" => $conn->connect_error]);
|
||||||
|
exit();
|
||||||
}
|
}
|
||||||
$sql = "CREATE DATABASE ".$DBName;
|
$sql = "CREATE DATABASE ".$DBName;
|
||||||
if ($conn->query($sql) === TRUE) {
|
if ($conn->query($sql) === FALSE) {
|
||||||
echo "Database created successfully";
|
http_response_code(400);
|
||||||
} else {
|
echo json_encode(["message" => $conn->error]);
|
||||||
echo "Error creating database: " . $conn->error;
|
exit();
|
||||||
}
|
}
|
||||||
$conn->close();
|
$conn->close();
|
||||||
return array(
|
return array(
|
||||||
|
@ -41,24 +43,33 @@ function myDB(string $servername, string $username, string $password, string $DB
|
||||||
*/
|
*/
|
||||||
function liteDB(string $DBName): array
|
function liteDB(string $DBName): array
|
||||||
{
|
{
|
||||||
New SQLite3("Database/".$DBName.".sqlite");
|
$DBName = $DBName.".sqlite";
|
||||||
$test = array(
|
if(file_exists($DBName)){
|
||||||
|
http_response_code(400);
|
||||||
|
echo json_encode(["message" => "database already exits"]);
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
New SQLite3($DBName);
|
||||||
|
return array(
|
||||||
'driver' => 'sqlite',
|
'driver' => 'sqlite',
|
||||||
'database' => realpath(dirname(__FILE__)."/Database/".$DBName.".sqlite")
|
'database' => realpath(dirname(__FILE__)."/".$DBName)
|
||||||
//'Backend/Database/'.$DBName.'.sqlite'
|
|
||||||
);
|
);
|
||||||
var_dump($test);
|
}
|
||||||
return $test;
|
|
||||||
|
$conFilePath = '../config/database.json';
|
||||||
|
if(!file_exists($conFilePath)){
|
||||||
|
$confile = fopen($conFilePath, "w");
|
||||||
|
fclose($confile);
|
||||||
|
}
|
||||||
|
if (0 !== filesize($conFilePath)){
|
||||||
|
http_response_code(400);
|
||||||
|
echo json_encode(["message" => "already have database connection"]);
|
||||||
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$servername = "localhost";
|
|
||||||
$username = "root";
|
|
||||||
$password = "V#_xWL6_";
|
|
||||||
$DBName = "TestDB";
|
|
||||||
if(isset($_POST["dbSetup"])){
|
if(isset($_POST["dbSetup"])){
|
||||||
|
|
||||||
|
|
||||||
switch($_POST["dbType"]){
|
switch($_POST["dbType"]){
|
||||||
case "mysql":
|
case "mysql":
|
||||||
$dbCon = myDB($_POST["dbServername"],$_POST["dbUsername"],$_POST["dbPassword"], $_POST["dbName"]);
|
$dbCon = myDB($_POST["dbServername"],$_POST["dbUsername"],$_POST["dbPassword"], $_POST["dbName"]);
|
||||||
|
@ -67,28 +78,30 @@ if(isset($_POST["dbSetup"])){
|
||||||
$dbCon = liteDB($_POST["dbName"]);
|
$dbCon = liteDB($_POST["dbName"]);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
die("wrong dbType");
|
http_response_code(400);
|
||||||
|
echo json_encode(["message" => "database type is wrong"]);
|
||||||
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
file_put_contents("../config/database.json", json_encode($dbCon));
|
file_put_contents($conFilePath, json_encode($dbCon));
|
||||||
|
|
||||||
require_once('../bootstrap.php');
|
require_once('../bootstrap.php');
|
||||||
try{
|
try{
|
||||||
$capsule->Connection()->getPdo();
|
$capsule->Connection()->getPdo();
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
//file_put_contents("../config/database.json", NULL);
|
http_response_code(400);
|
||||||
die("Could not connect to the database. Please check your configuration. error:" . $e );
|
file_put_contents($conFilePath, NULL);
|
||||||
|
echo json_encode(["message" => "Could not connect to the database. Please check your configuration. error:" . $e]);
|
||||||
|
exit();
|
||||||
}
|
}
|
||||||
require_once ('Database/databaseMigration.php');
|
require_once ('Database/databaseMigration.php');
|
||||||
AdminUser::firstOrCreate([
|
AdminUser::firstOrCreate([
|
||||||
'user_name' => $_POST["AdminUsername"], 'password' => password_hash($_POST["AdminPassword"],PASSWORD_DEFAULT)
|
'user_name' => $_POST["AdminUsername"], 'password' => password_hash($_POST["AdminPassword"],PASSWORD_DEFAULT)
|
||||||
]);
|
]);
|
||||||
|
http_response_code(201);
|
||||||
|
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
echo "not set";
|
http_response_code(400);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue