From 7254025426f32ec2a6906464d3c8dbef01145386 Mon Sep 17 00:00:00 2001 From: RemRem Date: Thu, 9 Nov 2023 16:21:11 +0100 Subject: [PATCH] update status code + db init --- app/database_init.php | 42 ++++++++++++++++++++++++++++++++++++++++++ app/routes.php | 2 +- public/index.php | 7 +++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 app/database_init.php diff --git a/app/database_init.php b/app/database_init.php new file mode 100644 index 0000000..b2437e2 --- /dev/null +++ b/app/database_init.php @@ -0,0 +1,42 @@ +con = (new DatabaseCon)->connect(); + } catch(PDOException $e) { + throw new PDOException($e->getMessage(), $e->getCode(), $e); + $this->createUserTable(); + $this->createFileTable(); + putenv("IS_DB_INIT=true"); + } + } + + private function createUserTable() { + $query = 'CREATE TABLE user ( + `id` UUID PRIMARY KEY, + email VARCHAR(100) UNIQUE, + hash VARCHAR(255), + username VARCHAR(20) DEFAULT \'Change Me!\', + creation_date DATE);'; + + $this->con->executeQuery($query); + } + + private function createFileTable() { + $query = 'CREATE TABLE file ( + id UUID PRIMARY KEY, + `user_id` UUID REFERENCES `user`(`id`) ON DELETE CASCADE, + filename VARCHAR(100) DEFAULT CURDATE(), + import_date DATE);'; + + $this->con->executeQuery($query); + } +} diff --git a/app/routes.php b/app/routes.php index d43f3f0..ffdcc49 100644 --- a/app/routes.php +++ b/app/routes.php @@ -27,7 +27,7 @@ return function (App $app) { return $res->withStatus(400); } $code = (new UserGateway)->createUser($req_body['email'], $req_body['hash'], $req_body['username']); - if($code === -1) return $res->withStatus(409); + if($code === -1) return $res->withStatus(500); $res->getBody()->write(json_encode($code)); return $res; diff --git a/public/index.php b/public/index.php index bc583c8..c789dc8 100644 --- a/public/index.php +++ b/public/index.php @@ -9,6 +9,10 @@ use App\Application\Settings\SettingsInterface; use DI\ContainerBuilder; use Slim\Factory\AppFactory; use Slim\Factory\ServerRequestCreatorFactory; +use Config\DatabaseInit; +require '../app/connection.php'; +require '../app/database_con.php'; +require '../app/database_init.php'; require __DIR__ . '/../vendor/autoload.php'; @@ -76,6 +80,9 @@ $app->addBodyParsingMiddleware(); $errorMiddleware = $app->addErrorMiddleware($displayErrorDetails, $logError, $logErrorDetails); $errorMiddleware->setDefaultErrorHandler($errorHandler); +// Create DB +(new DatabaseInit); + // Run App & Emit Response $response = $app->handle($request); $responseEmitter = new ResponseEmitter();