diff --git a/README.md b/README.md index f329b64..852c20b 100644 Binary files a/README.md and b/README.md differ diff --git a/bootstrap.php b/bootstrap.php index 20a719b..2d00e79 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -3,22 +3,35 @@ use Doctrine\DBAL\DriverManager; use Doctrine\ORM\EntityManager; use Doctrine\ORM\ORMSetup; +use App\Adapter\MysqlAdapter; +use App\Adapter\PostgresAdapter; require_once "vendor/autoload.php"; -$config = ORMSetup::createAttributeMetadataConfiguration( - paths: [__DIR__."/src/Entity"], - isDevMode: true, -); - -$connection = DriverManager::getConnection([ - 'driver' => 'pdo_mysql', - 'dbname' => 'db_name', +$isDevMode = true; +$paths = [__DIR__."/src/Entity"]; +$dbParams = [ + 'driver' => 'pdo_mysql', 'user' => 'user', 'password' => 'password', 'host' => 'setup-mysql', -], $config); + 'dbname' => 'db_name', +]; + +// Configuração da conexão MySQL +$mysqlAdapter = new MysqlAdapter(); +$pdo_mysql = $mysqlAdapter->getConnection(); + +// Configuração da conexão PostgreSQL +$postgresAdapter = new PostgresAdapter(); +$pdo_postgres = $postgresAdapter->getConnection(); -$entityManager = new EntityManager($connection, $config); +// Crie a configuração do ORM +$ormConfig = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode); +$entityManager = EntityManager::create($dbParams, $ormConfig); -return $entityManager; \ No newline at end of file +return [ + 'entityManager' => $entityManager, + 'pdo_mysql' => $pdo_mysql, + 'pdo_postgres' => $pdo_postgres +]; diff --git a/config/routes.php b/config/routes.php index 86456c7..9584e6e 100644 --- a/config/routes.php +++ b/config/routes.php @@ -15,6 +15,9 @@ '/translate' => [TranslateController::class, 'translate'], + '/alunos/listar' => [AlunoController::class, 'listar'], + '/alunos/excluir' => [AlunoController::class, 'excluir'], + '/cursos/listar' => [CursoController::class, 'listar'], '/cursos/adicionar' => [CursoController::class, 'add'], '/cursos/editar' => [CursoController::class, 'editar'], diff --git a/src/Adapter/DatabaseAdapterInterface.php b/src/Adapter/DatabaseAdapterInterface.php new file mode 100644 index 0000000..8626669 --- /dev/null +++ b/src/Adapter/DatabaseAdapterInterface.php @@ -0,0 +1,8 @@ + \PDO::ERRMODE_EXCEPTION, + \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, + ]; + + try { + $pdo = new \PDO($dsn, $user, $password, $options); + return $pdo; + } catch (\PDOException $e) { + throw new \PDOException($e->getMessage(), (int)$e->getCode()); + } + } +} diff --git a/src/Adapter/PostgresAdapter.php b/src/Adapter/PostgresAdapter.php new file mode 100644 index 0000000..1b3ed4d --- /dev/null +++ b/src/Adapter/PostgresAdapter.php @@ -0,0 +1,31 @@ + \PDO::ERRMODE_EXCEPTION, + \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, + ]; + + try { + $pdo = new \PDO($dsn, $user, $password, $options); + return $pdo; + } catch (\PDOException $e) { + throw new \PDOException($e->getMessage(), (int)$e->getCode()); + } +} +} diff --git a/src/Controller/AlunoController.php b/src/Controller/AlunoController.php index 4b11f1c..913ecbd 100644 --- a/src/Controller/AlunoController.php +++ b/src/Controller/AlunoController.php @@ -9,15 +9,33 @@ final class AlunoController extends AbstractController { + public mixed $entityManager; + + public function __construct() + { + $this->entityManager = parent::entityManager(); + } public function listar(): void { - $entityManager = parent::entityManager(); - $repository = $entityManager->getRepository(Aluno::class); + $repository = $this->entityManager->getRepository(Aluno::class); parent::render('alunos/listar', [ 'alunos' => $repository->findAll(), ]); } + + public function excluir(): void + { + $id = $_GET['id']; + $aluno = $this->entityManager->find(Aluno::class, $id); + + if($aluno !== null) { + $this->entityManager->remove($aluno); + $this->entityManager->flush(); + } + + header('location: /alunos/listar'); + } } \ No newline at end of file diff --git a/views/alunos/listar.php b/views/alunos/listar.php index 97ddbb1..37e7d88 100644 --- a/views/alunos/listar.php +++ b/views/alunos/listar.php @@ -24,11 +24,12 @@ {$aluno->created_at->format('d/m/Y')} {$buttonEdit} - {$buttonDelete} + {$buttonDelete} "; } ?> +