From 890187cff27bb7dd3dae573fb954adf3e200aa3c Mon Sep 17 00:00:00 2001 From: NOrtmann1 <145041949+NOrtmann1@users.noreply.github.com> Date: Wed, 27 May 2026 16:03:56 +0200 Subject: [PATCH] statt txt -> json --- php/model/Article.php | 20 ++++++++-- php/model/ArticleManager.php | 68 ++++++++++++++++++--------------- php/model/ArticleManagerDAO.php | 6 ++- php/model/UserDAO.php | 63 ++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 37 deletions(-) create mode 100644 php/model/UserDAO.php diff --git a/php/model/Article.php b/php/model/Article.php index 9afe99b..e2bdec7 100644 --- a/php/model/Article.php +++ b/php/model/Article.php @@ -1,5 +1,5 @@ id = $id; $this->title = $title; $this->content = $content; $this->author = $author; $this->date = $date; + $this->category = $category; + $this->tags = $tags; } /** * Gibt die ID eines Artikels zurück. + * @return int */ public function getId() { @@ -43,6 +47,7 @@ class Article /** * Gibt den Titel eines Artikels zurück. + * @return string */ public function getTitle() { @@ -50,7 +55,9 @@ class Article } /** - * Setzt den Titel eines Artikels. + * Setzt den Titel eines Artikels + * @param $title + * @return void */ public function setTitle($title) { @@ -60,6 +67,7 @@ class Article /** * Gibt den Content eines Artikels zurück. * TODO: Content muss noch definiert werden. + * @return string */ public function getContent() { @@ -69,6 +77,8 @@ class Article /** * Setzt den Content eines Artikels. * TODO: Content muss noch definiert werden. + * @param $content + * @return void */ public function setContent($content) { @@ -77,6 +87,7 @@ class Article /** * Gibt den Autor eines Artikels zurück. + * @return string */ public function getAuthor() { @@ -85,6 +96,7 @@ class Article /** * Gibt das Veröffentlichungsdatum des Artikels zurück. + * @return mixed */ public function getDate() { diff --git a/php/model/ArticleManager.php b/php/model/ArticleManager.php index 3c2fba0..f7bcec3 100644 --- a/php/model/ArticleManager.php +++ b/php/model/ArticleManager.php @@ -1,15 +1,18 @@ file, + json_encode($articles, JSON_PRETTY_PRINT) + ); } - public function newArticle($article) { - $newData = array( - "title" => $article->getTitle(), - "content" => $article->getContent(), - "author" => $article->getAuthor(), - "category" => $article->getCategory(), - //"tags" => $article->getTags(), TODO: Später hinzufügen - "date" => date("d.m.Y, H:i") - ); - $newData = base64_encode(serialize($newData)); - if (!file_exists("articles.txt")) { - $newData = fopen("articles.txt", "xb"); - fclose($newData); - } - $previousData = file_get_contents("articles.txt"); - if (file_put_contents("articles.txt", "$newData\n$previousData")) { - // TODO: Vernünftige Rückmeldung implementieren. - echo "Artikel erfolgreich gespeichert!"; - } else { - // TODO: passende Exceptions werfen. - echo "Fehler!"; - } + public function addArticle($title, $content, $author, $category, array $tags) + { + $articles = $this->getAllArticles(); + + $articles[] = [ + "id" => count($articles)+1, + "title" => $title, + "content" => $content, + "author" => $author, + "category" => $category, + "tags" => $tags + ]; + + $this->saveArticle($articles); } public function updateArticle($id, $title, $content, $author) @@ -71,7 +70,14 @@ class ArticleManager implements ArticleManagerDAO { public function getAllArticles() { - // TODO: Implement getAllArticles() method. + if (!file_exists($this->file)) { + return []; + } + + $json = file_get_contents($this->file); + $articles = json_decode($json, true); + + return is_array($articles) ? $articles : []; } } diff --git a/php/model/ArticleManagerDAO.php b/php/model/ArticleManagerDAO.php index 49136f7..e8e64d7 100644 --- a/php/model/ArticleManagerDAO.php +++ b/php/model/ArticleManagerDAO.php @@ -18,7 +18,7 @@ interface ArticleManagerDAO * Mögliche Exceptions: * TODO Fehlerbeschreibung hinzufügen & tags hinzufügen */ - public function newArticle($title, $content, $author, $category); + public function addArticle($title, $content, $author, $category, array $tags); /* * Ein angemeldeter Nutzer bearbeitet einen Beitrag. @@ -53,9 +53,11 @@ interface ArticleManagerDAO */ public function getArticle($id); - /* + /** * Alle Beiträge aufrufen. * + * @return Article + * * Mögliche Exceptions: * TODO Fehlerbeschreibung hinzufügen */ diff --git a/php/model/UserDAO.php b/php/model/UserDAO.php new file mode 100644 index 0000000..a0d60ce --- /dev/null +++ b/php/model/UserDAO.php @@ -0,0 +1,63 @@ +file)) { + return []; + } + + $json = file_get_contents($this->file); + $users = json_decode($json, true); + + return is_array($users) ? $users : []; + } + + private function saveUsers($users) { + file_put_contents( + $this->file, + json_encode($users, JSON_PRETTY_PRINT) + ); + } + + public function findUser($email) { + $users = $this->loadUsers(); + + foreach ($users as $user) { + if ($user["email"] === $email) { + return $user; + } + } + + return null; + } + + public function addUser($email, $username, $password) { + $users = $this->loadUsers(); + + $users[] = [ + "email" => $email, + "username" => $username, + "password" => $password + ]; + + $this->saveUsers($users); + } + + public function deleteUser($email) { + $users = $this->loadUsers(); + + foreach ($users as $i => $user) { + if ($user["email"] === $email) { + unset($users[$i]); + $users = array_values($users); + $this->saveUsers($users); + return true; + } + } + + return false; + } +} \ No newline at end of file