Update LocalArticleManager.php
This commit is contained in:
@@ -62,7 +62,8 @@ class LocalArticleManager implements ArticleManagerDAO {
|
|||||||
"author" => $author,
|
"author" => $author,
|
||||||
"category" => $category,
|
"category" => $category,
|
||||||
"tags" => $tags,
|
"tags" => $tags,
|
||||||
"creationDate" => date("Y-m-d H:i:s")
|
"creationDate" => date("Y-m-d H:i:s"),
|
||||||
|
"likes" => []
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->saveArticle($articles);
|
$this->saveArticle($articles);
|
||||||
@@ -92,7 +93,8 @@ class LocalArticleManager implements ArticleManagerDAO {
|
|||||||
"author" => $author,
|
"author" => $author,
|
||||||
"category" => $article->getCategory(),
|
"category" => $article->getCategory(),
|
||||||
"tags" => $article->getTags(),
|
"tags" => $article->getTags(),
|
||||||
"creationDate" => $article->getCreationDate()
|
"creationDate" => $article->getCreationDate(),
|
||||||
|
"likes" => $storedArticle['likes'] ?? []
|
||||||
];
|
];
|
||||||
$updated = true;
|
$updated = true;
|
||||||
break;
|
break;
|
||||||
@@ -142,7 +144,17 @@ class LocalArticleManager implements ArticleManagerDAO {
|
|||||||
|
|
||||||
foreach ($articles as $article) {
|
foreach ($articles as $article) {
|
||||||
if (isset($article['id']) && $article['id'] == $id) {
|
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) {
|
foreach ($articles as $article) {
|
||||||
if (isset($article['author']) && $article['author'] == $author) {
|
if (isset($article['author']) && $article['author'] == $author) {
|
||||||
|
$likes = isset($article['likes']) && is_array($article['likes']) ? $article['likes'] : [];
|
||||||
$filteredArticles[] = new Article(
|
$filteredArticles[] = new Article(
|
||||||
intval($article['id']),
|
intval($article['id']),
|
||||||
$article['title'],
|
$article['title'],
|
||||||
@@ -175,7 +188,8 @@ class LocalArticleManager implements ArticleManagerDAO {
|
|||||||
$article['author'],
|
$article['author'],
|
||||||
$article['category'],
|
$article['category'],
|
||||||
$article['tags'],
|
$article['tags'],
|
||||||
$article['creationDate']
|
$article['creationDate'],
|
||||||
|
$likes
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -201,6 +215,7 @@ class LocalArticleManager implements ArticleManagerDAO {
|
|||||||
if (($cleanKeyword !== '' && strpos($title, $cleanKeyword) !== false) ||
|
if (($cleanKeyword !== '' && strpos($title, $cleanKeyword) !== false) ||
|
||||||
($cleanKeyword !== '' && strpos($content, $cleanKeyword) !== false)) {
|
($cleanKeyword !== '' && strpos($content, $cleanKeyword) !== false)) {
|
||||||
|
|
||||||
|
$likes = isset($article['likes']) && is_array($article['likes']) ? $article['likes'] : [];
|
||||||
$filteredArticles[] = new Article(
|
$filteredArticles[] = new Article(
|
||||||
intval($article['id'] ?? 0),
|
intval($article['id'] ?? 0),
|
||||||
$article['title'] ?? '',
|
$article['title'] ?? '',
|
||||||
@@ -208,7 +223,8 @@ class LocalArticleManager implements ArticleManagerDAO {
|
|||||||
$article['author'] ?? '',
|
$article['author'] ?? '',
|
||||||
$article['category'] ?? '',
|
$article['category'] ?? '',
|
||||||
$article['tags'] ?? '',
|
$article['tags'] ?? '',
|
||||||
$article['creationDate'] ?? ''
|
$article['creationDate'] ?? '',
|
||||||
|
$likes
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -223,6 +239,7 @@ class LocalArticleManager implements ArticleManagerDAO {
|
|||||||
|
|
||||||
foreach ($articles as $article) {
|
foreach ($articles as $article) {
|
||||||
if (isset($article['category']) && $article['category'] == $category) {
|
if (isset($article['category']) && $article['category'] == $category) {
|
||||||
|
$likes = isset($article['likes']) && is_array($article['likes']) ? $article['likes'] : [];
|
||||||
$filteredArticles[] = new Article(
|
$filteredArticles[] = new Article(
|
||||||
intval($article['id']),
|
intval($article['id']),
|
||||||
$article['title'],
|
$article['title'],
|
||||||
@@ -230,12 +247,53 @@ class LocalArticleManager implements ArticleManagerDAO {
|
|||||||
$article['author'],
|
$article['author'],
|
||||||
$article['category'],
|
$article['category'],
|
||||||
$article['tags'],
|
$article['tags'],
|
||||||
$article['creationDate']
|
$article['creationDate'],
|
||||||
|
$likes
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $filteredArticles;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
Reference in New Issue
Block a user