Try and connect to the database.

This commit is contained in:
Dave Smith-Hayes 2024-11-05 22:38:26 -05:00
parent 9eb433a8ed
commit 8424a8c6ee
5 changed files with 24 additions and 10 deletions

View File

@ -39,6 +39,7 @@ use Slovocast\Domain\Repository\User\UserRepository;
use Slovocast\Domain\Repository\User\UserRepositoryInterface; use Slovocast\Domain\Repository\User\UserRepositoryInterface;
use Slovocast\Infrastructure\Api\Database\DatabaseConnectionInterface; use Slovocast\Infrastructure\Api\Database\DatabaseConnectionInterface;
use Slovocast\Infrastructure\Database\DatabaseConnection;
use Slovocast\Infrastructure\Api\User\UserAuthorizationInterface; use Slovocast\Infrastructure\Api\User\UserAuthorizationInterface;
use Slovocast\Infrastructure\User\BasicUserAuthorization; use Slovocast\Infrastructure\User\BasicUserAuthorization;
@ -71,7 +72,6 @@ class Bootstrap
$config->addSchema('database', DatabaseConnectionSchema::getSchema()); $config->addSchema('database', DatabaseConnectionSchema::getSchema());
$config->addSchema('session', SessionSchema::getSchema()); $config->addSchema('session', SessionSchema::getSchema());
$config->merge([ $config->merge([
'site' => [ 'site' => [
'name' => "Slovocast", 'name' => "Slovocast",
@ -143,7 +143,14 @@ class Bootstrap
* Database Connections * Database Connections
*/ */
DatabaseConnectionInterface::class => function (ContainerInterface $container) { 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'),
);
}, },
/** /**

View File

@ -15,7 +15,7 @@ class DatabaseConnectionSchema
'port' => Expect::int()->min(1)->max(65535)->default(3306), 'port' => Expect::int()->min(1)->max(65535)->default(3306),
'database' => Expect::string()->required(), 'database' => Expect::string()->required(),
'username' => Expect::string()->required(), 'username' => Expect::string()->required(),
'password' => Expect::string()->nullable() 'password' => Expect::string()->required()
]); ]);
} }
} }

View File

@ -34,10 +34,7 @@ class LoginUserAction extends Controller
} }
// start the session // start the session
$this->session->set('user', [ $this->session->set('user', [ 'id' => $user->getId(), 'authenticated' => true ]);
'id' => $user->getId(), return $this->render('dashboard.twig');
'authenticated' => true
]);
return $this->response;
} }
} }

View File

@ -7,7 +7,18 @@ use PDO;
class DatabaseConnection implements DatabaseConnectionInterface 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 public function getConnection(): PDO
{ {

View File

@ -42,7 +42,6 @@ class Routes
$app->get('/login', LoginUserPage::class) $app->get('/login', LoginUserPage::class)
->setName('user-login-page'); ->setName('user-login-page');
$app->post('/login', LoginUserAction::class) $app->post('/login', LoginUserAction::class)
->add(AuthenticatedMiddleware::class)
->setName('user-login-action'); ->setName('user-login-action');
} }