DatabaseInitializer
This commit is contained in:
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user