Merge branch 'Datenbanken_zusammenfuehren' into dev
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
require_once 'DatabaseInitializer.php';
|
||||
require_once 'ArticleManagerDAO.php';
|
||||
require_once 'Article.php';
|
||||
/**
|
||||
@@ -9,60 +10,35 @@ 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/articles.db')) {
|
||||
try {
|
||||
|
||||
$db = $this->getConnection();
|
||||
|
||||
// Tabelle für Beiträge
|
||||
$db->exec("
|
||||
CREATE TABLE articles (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT,
|
||||
content TEXT,
|
||||
author TEXT,
|
||||
category TEXT,
|
||||
tags TEXT,
|
||||
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);");
|
||||
|
||||
// Tabelle für Likes
|
||||
$db->exec("
|
||||
CREATE TABLE likes (
|
||||
article_id INTEGER,
|
||||
user_id TEXT,
|
||||
PRIMARY KEY (article_id, user_id),
|
||||
FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE
|
||||
);");
|
||||
unset($db);
|
||||
} catch (PDOException $e) {
|
||||
throw new InternalServerErrorException($e->getMessage());
|
||||
}
|
||||
}
|
||||
$this->dbPath = __DIR__ . '/../../db/eduforgeDB.db';
|
||||
DatabaseInitializer::initialize($this->dbPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Baut die Verbindung zur Datenbank auf.
|
||||
* @throws InternalServerErrorException
|
||||
*
|
||||
* @return PDO Datenbankverbindung
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
private function getConnection()
|
||||
{
|
||||
try {
|
||||
$user = 'root';
|
||||
$pw = null;
|
||||
$dsn = 'sqlite:' . __DIR__ . '/../../db/articles.db';
|
||||
return new PDO($dsn, $user, $pw);
|
||||
$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 InternalServerErrorException($e->getMessage());
|
||||
throw new RuntimeException("internal_error");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user