diff --git a/php/model/LocalArticleManager.php b/php/model/LocalArticleManager.php index 6df27e9..aab081a 100644 --- a/php/model/LocalArticleManager.php +++ b/php/model/LocalArticleManager.php @@ -62,7 +62,8 @@ class LocalArticleManager implements ArticleManagerDAO { "author" => $author, "category" => $category, "tags" => $tags, - "creationDate" => date("Y-m-d H:i:s") + "creationDate" => date("Y-m-d H:i:s"), + "likes" => [] ]; $this->saveArticle($articles); @@ -92,7 +93,8 @@ class LocalArticleManager implements ArticleManagerDAO { "author" => $author, "category" => $article->getCategory(), "tags" => $article->getTags(), - "creationDate" => $article->getCreationDate() + "creationDate" => $article->getCreationDate(), + "likes" => $storedArticle['likes'] ?? [] ]; $updated = true; break; @@ -142,7 +144,17 @@ class LocalArticleManager implements ArticleManagerDAO { foreach ($articles as $article) { if (isset($article['id']) && $article['id'] == $id) { - return new Article(intval($article['id']), $article['title'], $article['content'], $article['author'], $article['category'], $article['tags'], $article['creationDate']); + $likes = isset($article['likes']) && is_array($article['likes']) ? $article['likes'] : []; + return new Article( + intval($article['id']), + $article['title'], + $article['content'], + $article['author'], + $article['category'], + $article['tags'], + $article['creationDate'], + $likes + ); } } @@ -168,6 +180,7 @@ class LocalArticleManager implements ArticleManagerDAO { foreach ($articles as $article) { if (isset($article['author']) && $article['author'] == $author) { + $likes = isset($article['likes']) && is_array($article['likes']) ? $article['likes'] : []; $filteredArticles[] = new Article( intval($article['id']), $article['title'], @@ -175,7 +188,8 @@ class LocalArticleManager implements ArticleManagerDAO { $article['author'], $article['category'], $article['tags'], - $article['creationDate'] + $article['creationDate'], + $likes ); } } @@ -201,6 +215,7 @@ class LocalArticleManager implements ArticleManagerDAO { if (($cleanKeyword !== '' && strpos($title, $cleanKeyword) !== false) || ($cleanKeyword !== '' && strpos($content, $cleanKeyword) !== false)) { + $likes = isset($article['likes']) && is_array($article['likes']) ? $article['likes'] : []; $filteredArticles[] = new Article( intval($article['id'] ?? 0), $article['title'] ?? '', @@ -208,7 +223,8 @@ class LocalArticleManager implements ArticleManagerDAO { $article['author'] ?? '', $article['category'] ?? '', $article['tags'] ?? '', - $article['creationDate'] ?? '' + $article['creationDate'] ?? '', + $likes ); } } @@ -223,6 +239,7 @@ class LocalArticleManager implements ArticleManagerDAO { foreach ($articles as $article) { if (isset($article['category']) && $article['category'] == $category) { + $likes = isset($article['likes']) && is_array($article['likes']) ? $article['likes'] : []; $filteredArticles[] = new Article( intval($article['id']), $article['title'], @@ -230,12 +247,53 @@ class LocalArticleManager implements ArticleManagerDAO { $article['author'], $article['category'], $article['tags'], - $article['creationDate'] + $article['creationDate'], + $likes ); } } return $filteredArticles; } + public function toggleLike(int $articleId, string $userId): bool + { + $articles = $this->getAllArticles(); + $articleFound = false; + $isLikedNow = false; + + foreach ($articles as $index => $article) { + if (isset($article['id']) && $article['id'] == $articleId) { + $articleFound = true; + + // Likes-Array initialisieren, falls nicht vorhanden + if (!isset($articles[$index]['likes']) || !is_array($articles[$index]['likes'])) { + $articles[$index]['likes'] = []; + } + + $likeIndex = array_search($userId, $articles[$index]['likes']); + + if ($likeIndex !== false) { + // Bereits geliked -> Unlike + unset($articles[$index]['likes'][$likeIndex]); + // Array-Keys neu indizieren, damit JSON sauber bleibt + $articles[$index]['likes'] = array_values($articles[$index]['likes']); + $isLikedNow = false; + } else { + // Noch nicht geliked -> Like (User-ID hinzufügen) + $articles[$index]['likes'][] = $userId; + $isLikedNow = true; + } + break; + } + } + + if (!$articleFound) { + throw new NotFoundException("missing_id"); + } + + $this->saveArticle($articles); + return $isLikedNow; + } + } ?> \ No newline at end of file