setup.php error handeling

This commit is contained in:
RundelhausCode 2021-03-26 12:06:46 +01:00
parent bcb6bf0bf4
commit 1365c00c78
1 changed files with 37 additions and 24 deletions

View File

@ -17,13 +17,15 @@ function myDB(string $servername, string $username, string $password, string $DB
$conn = new mysqli($servername, $username, $password);
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;
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
if ($conn->query($sql) === FALSE) {
http_response_code(400);
echo json_encode(["message" => $conn->error]);
exit();
}
$conn->close();
return array(
@ -41,24 +43,33 @@ function myDB(string $servername, string $username, string $password, string $DB
*/
function liteDB(string $DBName): array
{
New SQLite3("Database/".$DBName.".sqlite");
$test = array(
$DBName = $DBName.".sqlite";
if(file_exists($DBName)){
http_response_code(400);
echo json_encode(["message" => "database already exits"]);
exit();
}
New SQLite3($DBName);
return array(
'driver' => 'sqlite',
'database' => realpath(dirname(__FILE__)."/Database/".$DBName.".sqlite")
//'Backend/Database/'.$DBName.'.sqlite'
'database' => realpath(dirname(__FILE__)."/".$DBName)
);
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"])){
switch($_POST["dbType"]){
case "mysql":
$dbCon = myDB($_POST["dbServername"],$_POST["dbUsername"],$_POST["dbPassword"], $_POST["dbName"]);
@ -67,28 +78,30 @@ if(isset($_POST["dbSetup"])){
$dbCon = liteDB($_POST["dbName"]);
break;
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');
try{
$capsule->Connection()->getPdo();
} catch (\Exception $e) {
//file_put_contents("../config/database.json", NULL);
die("Could not connect to the database. Please check your configuration. error:" . $e );
http_response_code(400);
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');
AdminUser::firstOrCreate([
'user_name' => $_POST["AdminUsername"], 'password' => password_hash($_POST["AdminPassword"],PASSWORD_DEFAULT)
]);
http_response_code(201);
}else{
echo "not set";
http_response_code(400);
}