From 3d65f79d2c1cab0d9c3acccfe641e2783015ce13 Mon Sep 17 00:00:00 2001 From: NOrtmann1 <145041949+NOrtmann1@users.noreply.github.com> Date: Wed, 24 Jun 2026 16:36:11 +0200 Subject: [PATCH] DatabaseInitializer --- php/model/DatabaseArticleManager.php | 43 +++--------------- php/model/DatabaseCommentManager.php | 43 ++---------------- php/model/DatabaseInitializer.php | 66 ++++++++++++++++++++++++++++ php/model/DatabaseUserManager.php | 28 +++--------- 4 files changed, 80 insertions(+), 100 deletions(-) create mode 100644 php/model/DatabaseInitializer.php diff --git a/php/model/DatabaseArticleManager.php b/php/model/DatabaseArticleManager.php index 3d282a4..3560167 100644 --- a/php/model/DatabaseArticleManager.php +++ b/php/model/DatabaseArticleManager.php @@ -9,48 +9,18 @@ require_once 'Article.php'; class DatabaseArticleManager implements ArticleManagerDAO { private static $instance = null; + private $dbPath; /** * Konstruktor * * Erstellt die Datenbankverbindung und die Tabelle articles. - * @throws InternalServerErrorException + * @throws RuntimeException */ public function __construct() { - if (!file_exists(__DIR__ . '/../../db/eduforgeDB.db')) { - try { - - $db = $this->getConnection(); - - // Tabelle für Beiträge - $db->exec(" - CREATE TABLE IF NOT EXISTS articles ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - title TEXT, - content TEXT, - author TEXT, - category TEXT, - tags TEXT, - created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (author) REFERENCES users(email) ON DELETE SET NULL ON UPDATE CASCADE - );"); - - - // Tabelle für Likes - $db->exec(" - CREATE TABLE IF NOT EXISTS likes ( - article_id INTEGER, - user_id TEXT, - PRIMARY KEY (article_id, user_id), - FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE, - FOREIGN KEY (user_id) REFERENCES users(email) ON DELETE CASCADE - );"); - unset($db); - } catch (PDOException $e) { - throw new InternalServerErrorException($e->getMessage()); - } - } + $this->dbPath = __DIR__ . '/../../db/eduforgeDB.db'; + DatabaseInitializer::initialize($this->dbPath); } /** @@ -62,12 +32,9 @@ class DatabaseArticleManager implements ArticleManagerDAO { private function getConnection() { try { - $dsn = 'sqlite:' . __DIR__ . '/../../db/eduforgeDB.db'; - - $db = new PDO($dsn, null, null); + $db = new PDO('sqlite:' . $this->dbPath, null, null); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->exec('PRAGMA foreign_keys = ON;'); - return $db; } catch (PDOException $e) { throw new RuntimeException("internal_error"); diff --git a/php/model/DatabaseCommentManager.php b/php/model/DatabaseCommentManager.php index 1ad537a..e46a9ce 100644 --- a/php/model/DatabaseCommentManager.php +++ b/php/model/DatabaseCommentManager.php @@ -12,46 +12,15 @@ require_once "Comment.php"; class DatabaseCommentManager implements CommentManagerDAO { private static $instance = null; + private $dbPath; /** * Erstellt die Kommentartabelle, falls diese noch nicht existiert. */ public function __construct() { - try { - $db = $this->getConnection(); - - $db->exec(" - CREATE TABLE IF NOT EXISTS comments ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - article_id INTEGER NOT NULL, - parent_comment_id INTEGER NULL, - author TEXT NOT NULL, - content TEXT NOT NULL, - created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE, - FOREIGN KEY (author) REFERENCES users(email) ON DELETE CASCADE, - FOREIGN KEY (parent_comment_id) REFERENCES comments(id) ON DELETE CASCADE - ); - "); - - $columns = $db->query("PRAGMA table_info(comments);")->fetchAll(PDO::FETCH_ASSOC); - $hasParentColumn = false; - - foreach ($columns as $column) { - if ($column["name"] === "parent_comment_id") { - $hasParentColumn = true; - break; - } - } - - if (!$hasParentColumn) { - $db->exec("ALTER TABLE comments ADD COLUMN parent_comment_id INTEGER NULL;"); - } - - } catch (PDOException $e) { - throw new RuntimeException("internal_error"); - } + $this->dbPath = __DIR__ . '/../../db/eduforgeDB.db'; + DatabaseInitializer::initialize($this->dbPath); } /** @@ -62,14 +31,10 @@ class DatabaseCommentManager implements CommentManagerDAO private function getConnection() { try { - $dsn = 'sqlite:' . __DIR__ . '/../../db/eduforgeDB.db'; - - $db = new PDO($dsn, null, null); + $db = new PDO('sqlite:' . $this->dbPath, null, null); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->exec('PRAGMA foreign_keys = ON;'); - return $db; - } catch (PDOException $e) { throw new RuntimeException("internal_error"); } diff --git a/php/model/DatabaseInitializer.php b/php/model/DatabaseInitializer.php new file mode 100644 index 0000000..5e2dc3b --- /dev/null +++ b/php/model/DatabaseInitializer.php @@ -0,0 +1,66 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $db->exec('PRAGMA foreign_keys = ON;'); + + $db->exec(" + CREATE TABLE IF NOT EXISTS users ( + email TEXT PRIMARY KEY, + vorname TEXT NOT NULL, + nachname TEXT NOT NULL, + password TEXT NOT NULL + ); + "); + + $db->exec(" + CREATE TABLE IF NOT EXISTS articles ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title TEXT, + content TEXT, + author TEXT, + category TEXT, + tags TEXT, + created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (author) REFERENCES users(email) ON DELETE SET NULL ON UPDATE CASCADE + ); + "); + + $db->exec(" + CREATE TABLE IF NOT EXISTS likes ( + article_id INTEGER, + user_id TEXT, + PRIMARY KEY (article_id, user_id), + FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE, + FOREIGN KEY (user_id) REFERENCES users(email) ON DELETE CASCADE + ); + "); + + $db->exec(" + CREATE TABLE IF NOT EXISTS comments ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + article_id INTEGER NOT NULL, + parent_comment_id INTEGER NULL, + author TEXT NOT NULL, + content TEXT NOT NULL, + created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE, + FOREIGN KEY (author) REFERENCES users(email) ON DELETE CASCADE, + FOREIGN KEY (parent_comment_id) REFERENCES comments(id) ON DELETE CASCADE + ); + "); + + } catch (PDOException $e) { + throw new RuntimeException("internal-error"); + } + } + } +} diff --git a/php/model/DatabaseUserManager.php b/php/model/DatabaseUserManager.php index 311366d..afbf71a 100644 --- a/php/model/DatabaseUserManager.php +++ b/php/model/DatabaseUserManager.php @@ -11,6 +11,7 @@ require_once "UserManagerDAO.php"; class DatabaseUserManager implements UserManagerDAO { private static $instance = null; + private $dbPath; /** * Konstruktor. @@ -22,23 +23,8 @@ class DatabaseUserManager implements UserManagerDAO { */ public function __construct() { - try { - $db = $this->getConnection(); - - $db->exec(" - CREATE TABLE IF NOT EXISTS users ( - email TEXT PRIMARY KEY, - vorname TEXT NOT NULL, - nachname TEXT NOT NULL, - password TEXT NOT NULL - ); - "); - - unset($db); - - } catch (PDOException $e) { - throw new RuntimeException("Benutzerdatenbank konnte nicht erstellt werden."); - } + $this->dbPath = __DIR__ . '/../../db/eduforgeDB.db'; + DatabaseInitializer::initialize($this->dbPath); } /** @@ -51,16 +37,12 @@ class DatabaseUserManager implements UserManagerDAO { private function getConnection() { try { - $dsn = 'sqlite:' . __DIR__ . '/../../db/eduforgeDB.db'; - - $db = new PDO($dsn, null, null); + $db = new PDO('sqlite:' . $this->dbPath, null, null); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->exec('PRAGMA foreign_keys = ON;'); - return $db; - } catch (PDOException $e) { - throw new RuntimeException("Verbindung zur Benutzerdatenbank fehlgeschlagen."); + throw new RuntimeException("internal_error"); } }