From 8424a8c6eedd2eb2b192fe704efda690bf97af6c Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Tue, 5 Nov 2024 22:38:26 -0500 Subject: [PATCH] Try and connect to the database. --- app/src/Bootstrap.php | 11 +++++++++-- app/src/Configuration/DatabaseConnectionSchema.php | 2 +- app/src/Controller/User/LoginUserAction.php | 7 ++----- .../Infrastructure/Database/DatabaseConnection.php | 13 ++++++++++++- app/src/Routes.php | 1 - 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/app/src/Bootstrap.php b/app/src/Bootstrap.php index 016c480..d010095 100644 --- a/app/src/Bootstrap.php +++ b/app/src/Bootstrap.php @@ -39,6 +39,7 @@ use Slovocast\Domain\Repository\User\UserRepository; use Slovocast\Domain\Repository\User\UserRepositoryInterface; use Slovocast\Infrastructure\Api\Database\DatabaseConnectionInterface; +use Slovocast\Infrastructure\Database\DatabaseConnection; use Slovocast\Infrastructure\Api\User\UserAuthorizationInterface; use Slovocast\Infrastructure\User\BasicUserAuthorization; @@ -71,7 +72,6 @@ class Bootstrap $config->addSchema('database', DatabaseConnectionSchema::getSchema()); $config->addSchema('session', SessionSchema::getSchema()); - $config->merge([ 'site' => [ 'name' => "Slovocast", @@ -143,7 +143,14 @@ class Bootstrap * Database Connections */ DatabaseConnectionInterface::class => function (ContainerInterface $container) { - $config = $container->get('config')->get('database'); + $config = $container->get('config'); + return new DatabaseConnection( + $config->get('database.host'), + $config->get('database.username'), + $config->get('database.password'), + $config->get('database.database'), + $config->get('database.port'), + ); }, /** diff --git a/app/src/Configuration/DatabaseConnectionSchema.php b/app/src/Configuration/DatabaseConnectionSchema.php index 6fedd60..51d1dfe 100644 --- a/app/src/Configuration/DatabaseConnectionSchema.php +++ b/app/src/Configuration/DatabaseConnectionSchema.php @@ -15,7 +15,7 @@ class DatabaseConnectionSchema 'port' => Expect::int()->min(1)->max(65535)->default(3306), 'database' => Expect::string()->required(), 'username' => Expect::string()->required(), - 'password' => Expect::string()->nullable() + 'password' => Expect::string()->required() ]); } } diff --git a/app/src/Controller/User/LoginUserAction.php b/app/src/Controller/User/LoginUserAction.php index 133a640..dc9f226 100644 --- a/app/src/Controller/User/LoginUserAction.php +++ b/app/src/Controller/User/LoginUserAction.php @@ -34,10 +34,7 @@ class LoginUserAction extends Controller } // start the session - $this->session->set('user', [ - 'id' => $user->getId(), - 'authenticated' => true - ]); - return $this->response; + $this->session->set('user', [ 'id' => $user->getId(), 'authenticated' => true ]); + return $this->render('dashboard.twig'); } } diff --git a/app/src/Infrastructure/Database/DatabaseConnection.php b/app/src/Infrastructure/Database/DatabaseConnection.php index a8207d5..9be9081 100644 --- a/app/src/Infrastructure/Database/DatabaseConnection.php +++ b/app/src/Infrastructure/Database/DatabaseConnection.php @@ -7,7 +7,18 @@ use PDO; class DatabaseConnection implements DatabaseConnectionInterface { - public function __construct(private PDO $pdo) {} + private PDO $pdo; + + public function __construct( + string $host, + string $username, + string $password, + string $database, + int $port = 3306 + ) { + $dsn = "mysql:dbname={$database};host={$host};port={$port}"; + $this->pdo = new PDO($dsn. $username, $password); + } public function getConnection(): PDO { diff --git a/app/src/Routes.php b/app/src/Routes.php index 543cd54..fad026a 100644 --- a/app/src/Routes.php +++ b/app/src/Routes.php @@ -42,7 +42,6 @@ class Routes $app->get('/login', LoginUserPage::class) ->setName('user-login-page'); $app->post('/login', LoginUserAction::class) - ->add(AuthenticatedMiddleware::class) ->setName('user-login-action'); }