Compare commits

...

14 Commits

Author SHA1 Message Date
NOrtmann1 d5a3f4298f Update README.md 2026-06-17 22:39:43 +02:00
NOrtmann1 b4a5c9c020 Update README.md 2026-06-17 22:38:23 +02:00
NOrtmann1 6d6f1f31b0 Update updateArticle-controller.php 2026-06-17 22:28:01 +02:00
NOrtmann1 cafffba921 Update updateArticle-controller.php 2026-06-17 22:21:00 +02:00
NOrtmann1 8fa758d825 Update dataSources.local.xml 2026-06-17 22:20:55 +02:00
niklas.ortmann 01fc8537cf Merge pull request 'Bewertungssystem' (#36) from Bewertungssystem into dev
Reviewed-on: #36
2026-06-17 21:29:24 +02:00
niklas.ortmann 4a6a3cf708 search-results -> Likes-Sortierung 2026-06-17 13:23:39 +02:00
niklas.ortmann cb44c3f5a8 Update search-results-controller.php 2026-06-17 13:11:03 +02:00
niklas.ortmann eb0a34e959 Create like-controller.php 2026-06-17 13:00:12 +02:00
niklas.ortmann 8d683cc1a0 showArticle -> like-Button 2026-06-17 12:53:33 +02:00
niklas.ortmann ee41dd4cfe Update LocalArticleManager.php 2026-06-17 12:35:17 +02:00
niklas.ortmann 3d50e6e3b3 ArticleManager um LIKES erweitert...
Einführung einer dynamischen Like-Funktion für Beiträge
2026-06-17 12:26:18 +02:00
niklas.ortmann 4f8d11881d Merge pull request 'home zeigt nun dynamisch die Kategorien an' (#35) from Startseite into dev
Reviewed-on: #35
2026-06-17 12:08:10 +02:00
niklas.ortmann 4321c4bee5 home zeigt nun dynamisch die Kategorien an 2026-06-17 11:31:23 +02:00
16 changed files with 445 additions and 95 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="dataSourceStorageLocal" created-in="IU-261.25134.95"> <component name="dataSourceStorageLocal" created-in="IU-261.24374.151">
<data-source name="articles" uuid="315cb5c9-2b0f-435b-b602-59823b160908"> <data-source name="articles" uuid="315cb5c9-2b0f-435b-b602-59823b160908">
<database-info product="SQLite" version="3.51.1" jdbc-version="4.2" driver-name="SQLite JDBC" driver-version="3.51.1.0" dbms="SQLITE" exact-version="3.51.1" exact-driver-version="3.51"> <database-info product="SQLite" version="3.51.1" jdbc-version="4.2" driver-name="SQLite JDBC" driver-version="3.51.1.0" dbms="SQLITE" exact-version="3.51.1" exact-driver-version="3.51">
<identifier-quote-string>&quot;</identifier-quote-string> <identifier-quote-string>&quot;</identifier-quote-string>
+12 -9
View File
@@ -16,18 +16,21 @@
## Bekannte Fehler und Mängel ## Bekannte Fehler und Mängel
- Bitte auf die gesetzten TODO's achten. Wenn Inhalte fehlen, sind sie i.d.R. als TODO kommentiert. - Bitte auf die gesetzten TODO's achten. Wenn Inhalte fehlen, sind sie i.d.R. als TODO kommentiert.
- Die Suchseite und Kategorieseite packen momentan alle passenden Beiträge untereinander. Später sollen zunächst 10 - Die Kategorieseite listet momentan alle passenden Beiträge untereinander. Später sollen mit einem Paginator die neusten
Ergebnisse auf einer Seite angezeigt werden. Beiträge nacheinander aufgelistet werden (ähnlich wie bei der Suche, wenn nach Fach gefiltert wird).
- Wenn ein Bild aus einem Beitrag entfernt wird, dann wird noch nicht die Datei im Pfad /uploads gelöscht. - Wenn ein Bild aus einem Beitrag entfernt wird, dann wird noch nicht die Datei im Pfad /uploads gelöscht.
- id in showArticle-controller.php und updateArticle-controller.php wird nicht als gültige numerische ID geprüft. - id in showArticle-controller.php und updateArticle-controller.php wird nicht als gültige numerische ID geprüft.
- sort in search-results-controller.php wird nicht gegen erlaubte Werte validiert. - Bilder im Beitragseditor sollen zukünftig eine Bildunterschrift bekommen und größenverstellbar sein.
- Die Elemente eines Contents im Beitrag werden momentan stumpf untereinander aufgelistet. Soll später
sich responisve auch nebeneinander orientieren usw.
## Besonderheiten des Projektes ## Besonderheiten des Projektes
- Es wurde ein einfacher Beitrags-Editor erstellt. Mit diesem können Beiträge erstellt oder bearbeitet werden. - Es wurde AJAX verwendet, um asynchrone Erstellung von Kommentaren zu implementieren. Es ermöglicht dem Nutzer, einen
Es handelt es sich um eine einfache Version. Später sollen z.B. Bilder und die Positionierung der Elemente folgen. Kommentar abzusenden, ohne dass die gesamte Webseite neu geladen werden muss.
- Es sind drei Dummy-Beiträge r den Nutzer max.mustermann hinterlegt. - Mit JavaScript werden auch clientseitig die Kommentare visuell hinzugefügt und die Kommentarbäume aufgebaut.
- Die Such-Seite umfasst eine Such- und Sortierfunktion. Jedoch fehlt noch eine - JavaScript wird verwendet, um im erweitertem Beitragseditor clientseitig einzelne Content-Boxen erstellen und löschen
Filterfunktion (z.B. nur Mathe anzeigen). zu können.
- JavaScript wird ebenfalls verwendet, um in die Suchergebnisse clientseitig zu sortieren.
## Sonstiges ## Sonstiges
- Das Datenschema befindet sich unter /planung/Datenschema.pdf - Das Datenschema befindet sich unter /planung/Datenschema.pdf
+27 -21
View File
@@ -19,28 +19,34 @@ include_once 'php/controller/home-controller.php';
sea takimata sanctus est Lorem ipsum dolor sit amet. sea takimata sanctus est Lorem ipsum dolor sit amet.
</p> </p>
<!-- Flexbox --> <!-- Flexbox für aktive Kategorien -->
<div class="flexbox"> <div class="flexbox">
<div class="container"> <?php
<a href="index.php?pfad=showCategory&category=informatik" class="category-link">Informatik</a> if (!empty($categoriesWithArticles) && is_array($categoriesWithArticles)):
<div class="article-link"> foreach ($categoriesWithArticles as $category):
<a href="index.php?pfad=showArticle&id=<?php echo $dummy3->getId()?>"><?php if(isset($dummy3)){echo $dummy3->getTitle();}else{echo "Fehler: Beitrag nicht gefunden!";} ?></a> ?>
</div> <div class="container">
</div> <a href="index.php?pfad=showCategory&category=<?php echo htmlspecialchars($category['slug']); ?>" class="category-link">
<?php echo htmlspecialchars($category['name']); ?>
</a>
<div class="container"> <!-- die 5 neuesten Beiträge der Kategorie -->
<a href="index.php?pfad=showCategory&category=mathe" class="category-link">Mathe</a> <div class="article-links-wrapper">
<div class="article-link"> <?php foreach ($category['articles'] as $article): ?>
<a href="index.php?pfad=showArticle&id=<?php echo $dummy1->getId()?>"><?php if(isset($dummy1)){echo $dummy1->getTitle();}else{echo "Fehler: Beitrag nicht gefunden!";} ?></a> <div class="article-link">
</div> <a href="index.php?pfad=showArticle&id=<?php echo htmlspecialchars($article->getId()); ?>">
</div> <?php echo htmlspecialchars($article->getTitle()); ?>
</a>
<div class="container"> </div>
<a href="index.php?pfad=showCategory&category=physik" class="category-link">Physik</a> <?php endforeach; ?>
<div class="article-link"> </div>
<a href="index.php?pfad=showArticle&id=<?php echo $dummy2->getId()?>"><?php if(isset($dummy2)){echo $dummy2->getTitle();}else{echo "Fehler: Beitrag nicht gefunden!";} ?></a> </div>
</div> <?php
</div> endforeach;
else:
?>
<p>Aktuell sind keine Beiträge in den Kategorien vorhanden.</p>
<?php endif; ?>
</div> </div>
</main> </main>
+10 -3
View File
@@ -65,8 +65,8 @@ $resultCount = count($results);
</label> </label>
<!-- Noch disabled, da likes noch nicht implementiert--> <!-- Noch disabled, da likes noch nicht implementiert-->
<label class="s-res-filter-option"> <label class="s-res-filter-option">
<input type="radio" name="sort" value="likes" <?php echo $currentSort === 'likes' ? 'checked' : ''; ?> disabled> <input type="radio" name="sort" value="likes" <?php echo $currentSort === 'likes' ? 'checked' : ''; ?> onchange="this.form.submit()">
<span style="color: #94a3b8;">Beliebtheit (Likes)</span> <span>Beliebtheit (Likes)</span>
</label> </label>
<label class="s-res-filter-option"> <label class="s-res-filter-option">
<input type="radio" name="sort" value="newest" <?php echo $currentSort === 'newest' ? 'checked' : ''; ?> onchange="this.form.submit()"> <input type="radio" name="sort" value="newest" <?php echo $currentSort === 'newest' ? 'checked' : ''; ?> onchange="this.form.submit()">
@@ -103,7 +103,14 @@ $resultCount = count($results);
<?php echo htmlspecialchars($item['title']); ?> <?php echo htmlspecialchars($item['title']); ?>
</a> </a>
</h2> </h2>
<p class="s-res-author">Von: <span class="s-res-author-name"><?php echo htmlspecialchars($item['author']); ?></span></p> <div class="s-res-meta-row">
<p class="s-res-author">Von: <span class="s-res-author-name"><?php echo htmlspecialchars($item['author']); ?></span></p>
<span class="s-res-likes">
❤️ <?php echo isset($item['likes']) && is_array($item['likes']) ? count($item['likes']) : 0; ?>
</span>
</div>
</div> </div>
<div class="s-res-arrow">&rarr;</div> <div class="s-res-arrow">&rarr;</div>
</div> </div>
+24 -23
View File
@@ -1,28 +1,10 @@
<?php <?php
include_once 'php/controller/showArticle-controller.php';
require_once 'php/model/CommentManager.php';
$comments = []; $comments = [];
$mainComments = []; $mainComments = [];
$repliesByParent = []; $repliesByParent = [];
$articleObj = null;
if (isset($_GET["id"])) { include_once 'php/controller/showArticle-controller.php';
try {
$commentManager = CommentManager::getInstance();
$comments = $commentManager->getCommentsByArticle($_GET["id"]);
foreach ($comments as $comment) {
if ($comment->isReply()) {
$parentId = $comment->getParentCommentId();
$repliesByParent[$parentId][] = $comment;
} else {
$mainComments[] = $comment;
}
}
} catch (Exception $e) {
$_SESSION["message"] = "internal_error";
}
}
?> ?>
<!-- <!--
Seite: Anzeige für Beiträge Seite: Anzeige für Beiträge
@@ -35,9 +17,28 @@ if (isset($_GET["id"])) {
<!-- Metadaten & Titel --> <!-- Metadaten & Titel -->
<div class="article-view-top-section"> <div class="article-view-top-section">
<?php if (isset($category) && !empty($category)): ?> <div class="article-view-top-section">
<span class="article-view-category"><?php echo htmlspecialchars($category); ?></span>
<?php endif; ?> <div class="category-and-likes-row">
<?php if (isset($category) && !empty($category)): ?>
<span class="article-view-category"><?php echo htmlspecialchars($category); ?></span>
<?php endif; ?>
<!-- Like-Anzeige und dynamischer Like-Button -->
<?php if (isset($articleObj) && $articleObj !== null): ?>
<div class="article-view-likes">
<span>❤️ <span class="like-count"><?php echo $articleObj->getLikeCount(); ?></span></span>
<?php if (isset($_SESSION["user_email"])): ?>
<a href="php/controller/like-controller.php?id=<?php echo $articleObj->getId(); ?>" class="like-toggle-btn">
<?php echo $articleObj->hasLiked($_SESSION["user_email"]) ? '👎 Gefällt mir nicht mehr' : '👍 Gefällt mir'; ?>
</a>
<?php else: ?>
<span class="login-hint">(Anmelden zum Liken)</span>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<h1 class="article-view-title"> <h1 class="article-view-title">
<?php if (isset($title)) { echo htmlspecialchars($title); } ?> <?php if (isset($title)) { echo htmlspecialchars($title); } ?>
+11
View File
@@ -247,6 +247,17 @@ CSS für die Suchergebnis-Seite
cursor: not-allowed; cursor: not-allowed;
} }
.s-res-meta-row {
display: flex;
gap: 15px;
align-items: center;
}
.s-res-likes {
font-size: 0.9em;
color: #475569;
}
/* Responsive Anpassungen unter 760px (für z.B. Smartphones) */ /* Responsive Anpassungen unter 760px (für z.B. Smartphones) */
@media (max-width: 768px) { @media (max-width: 768px) {
.s-res-layout-grid { .s-res-layout-grid {
+44
View File
@@ -239,4 +239,48 @@
.comment-login-hint p { .comment-login-hint p {
margin-bottom: 1rem; margin-bottom: 1rem;
}
/*
Like-Button etc.
*/
.category-and-likes-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
}
.article-view-likes {
display: flex;
align-items: center;
gap: 10px;
font-size: 0.95em;
}
.article-view-likes .like-count {
font-weight: bold;
}
.article-view-likes .login-hint {
font-size: 0.8em;
color: #777;
}
/* Interaktiver Like/Unlike-Button */
.like-toggle-btn {
text-decoration: none;
padding: 4px 10px;
border: 1px solid #bbb;
border-radius: 4px;
background-color: #f5f5f5;
color: #333;
font-weight: 500;
transition: background-color 0.2s ease, border-color 0.2s ease;
}
.like-toggle-btn:hover {
background-color: #eaeaea;
border-color: #999;
} }
+35 -5
View File
@@ -6,14 +6,44 @@ require_once 'php/model/Article.php';
require_once 'php/model/ArticleManager.php'; require_once 'php/model/ArticleManager.php';
require_once 'php/model/LocalArticleManager.php'; require_once 'php/model/LocalArticleManager.php';
$categoriesWithArticles = [];
$allowedCategories = [
'deutsch', 'englisch', 'franzoesisch', 'latein', 'literatur',
'mathe', 'biologie', 'chemie', 'physik', 'informatik', 'astronomie',
'geschichte', 'erdkunde', 'sozialkunde', 'wirtschaft', 'religion',
'ethik', 'philosophie', 'psychologie', 'kunst', 'musik', 'theater',
'technik', 'werken', 'hauswirtschaft', 'sport'
];
try { try {
$articleManager = ArticleManager::getInstance(); $articleManager = ArticleManager::getInstance();
// Beziehen der Dummy-Beiträge aus dem ArticleManager:
$dummy1 = $articleManager->getArticle(1); foreach ($allowedCategories as $categorySlug) {
$dummy2 = $articleManager->getArticle(2); $allCategoryArticles = $articleManager->getArticlesByCategory($categorySlug);
$dummy3 = $articleManager->getArticle(3);
// nur Kategorien, die Beiträge enthält:
if (!empty($allCategoryArticles) && is_array($allCategoryArticles)) {
// Beiträge nach Erstellungsdatum sortieren:
usort($allCategoryArticles, function($a, $b) {
$dateA = strtotime($a->getCreationDate());
$dateB = strtotime($b->getCreationDate());
return $dateB <=> $dateA; // Absteigende Sortierung
});
// auf die 5 zuletzt hinzugefügten Beiträge begrenzen:
$limitedArticles = array_slice($allCategoryArticles, 0, 5);
$categoriesWithArticles[] = [
'slug' => $categorySlug,
'name' => ucfirst($categorySlug), // Erster Buchstabe groß für die Ansicht
'articles' => $limitedArticles
];
}
}
} catch (Exception $e){ } catch (Exception $e){
$_SESSION["message"] = "internal_error"; $_SESSION["message"] = "internal_error";
echo "Fehler aufgetreten: " . $e->getMessage();
} }
?> ?>
+42
View File
@@ -0,0 +1,42 @@
<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
require_once __DIR__ . '/../model/Article.php';
require_once __DIR__ . '/../model/ArticleManager.php';
// 2. Prüfen, ob eine gültige Artikel-ID übergeben wurde
if (isset($_GET["id"]) && !empty($_GET["id"])) {
$articleId = intval($_GET["id"]);
$userEmail = $_SESSION["user_email"];
if (!isset($_SESSION["user_email"]) || empty($_SESSION["user_email"])) {
$_SESSION["message"] = "unauthorized_access";
header("Location: ../../index.php?pfad=showArticle&id=" . $articleId);
exit();
}
try {
$articleManager = ArticleManager::getInstance();
$articleManager->toggleLike($articleId, $userEmail);
header("Location: ../../index.php?pfad=showArticle&id=" . $articleId);
exit();
} catch (NotFoundException $e) {
$_SESSION["message"] = "missing_id";
header("Location: ../../index.php");
exit();
} catch (Exception $e) {
$_SESSION["message"] = "internal_error";
header("Location: ../../index.php");
exit();
}
} else {
$_SESSION["message"] = "missing_id";
header("Location: ../../index.php");
exit();
}
?>
+15 -10
View File
@@ -25,18 +25,22 @@ if ($_SERVER["REQUEST_METHOD"] === "GET" && isset($_GET["q"])) {
if ($sortStyle === 'alphabet') { if ($sortStyle === 'alphabet') {
// Titel aufsteigend alphabetiisch sortiert // Titel aufsteigend alphabetiisch sortiert
usort($results, function ($a, $b) {
return strcasecmp($a->getTitle(), $b->getTitle());
});
} elseif ($sortStyle === 'likes') {
usort($results, function($a, $b) { usort($results, function($a, $b) {
return strcasecmp($a->title, $b->title); return $b->getLikeCount() <=> $a->getLikeCount();
}); });
} elseif ($sortStyle === 'newest') { } elseif ($sortStyle === 'newest') {
// Datum neu zu alt sortiert // Datum neu zu alt sortiert
usort($results, function($a, $b) { usort($results, function($a, $b) {
return strcmp($b->creationDate, $a->creationDate); return strcmp($b->getCreationDate(), $a->getCreationDate());
}); });
} elseif ($sortStyle === 'oldest') { } elseif ($sortStyle === 'oldest') {
// Datum alt zu neu sortiert // Datum alt zu neu sortiert
usort($results, function($a, $b) { usort($results, function($a, $b) {
return strcmp($a->creationDate, $b->creationDate); return strcmp($a->getCreationDate(), $b->getCreationDate());
}); });
} }
@@ -44,13 +48,14 @@ if ($_SERVER["REQUEST_METHOD"] === "GET" && isset($_GET["q"])) {
$safeArrayResults = []; $safeArrayResults = [];
foreach ($results as $obj) { foreach ($results as $obj) {
$safeArrayResults[] = [ $safeArrayResults[] = [
"id" => $obj->id, "id" => $obj->getId(),
"title" => $obj->title, "title" => $obj->getTitle(),
"content" => $obj->content, "content" => $obj->getContent(),
"author" => $obj->author, "author" => $obj->getAuthor(),
"category" => $obj->category, "category" => $obj->getCategory(),
"tags" => $obj->tags, "tags" => $obj->getTags(),
"creationDate" => $obj->creationDate "creationDate" => $obj->getCreationDate(),
"likes" => $obj->getLikes(),
]; ];
} }
+15
View File
@@ -5,6 +5,7 @@ if (session_status() === PHP_SESSION_NONE) {
require_once 'php/model/Article.php'; require_once 'php/model/Article.php';
require_once 'php/model/ArticleManager.php'; require_once 'php/model/ArticleManager.php';
require_once 'php/model/CommentManager.php';
if (isset($_GET["id"]) && !empty($_GET["id"])){ if (isset($_GET["id"]) && !empty($_GET["id"])){
try { try {
@@ -17,11 +18,25 @@ if (isset($_GET["id"]) && !empty($_GET["id"])){
$category = $article->getCategory(); $category = $article->getCategory();
$author = $article->getAuthor(); $author = $article->getAuthor();
$tags = $article->getTags(); $tags = $article->getTags();
$articleObj = $article; // Objekt für die Like-Abfagen sichern
}else{ }else{
//header("location: index.php?pfad=404"); //header("location: index.php?pfad=404");
include_once "content/404.php"; include_once "content/404.php";
exit(); exit();
} }
$commentManager = CommentManager::getInstance();
$comments = $commentManager->getCommentsByArticle($_GET["id"]);
foreach ($comments as $comment) {
if ($comment->isReply()) {
$parentId = $comment->getParentCommentId();
$repliesByParent[$parentId][] = $comment;
} else {
$mainComments[] = $comment;
}
}
} catch (Exception $e){ } catch (Exception $e){
$_SESSION["message"] = "internal_error"; $_SESSION["message"] = "internal_error";
exit(); exit();
+3 -3
View File
@@ -8,7 +8,7 @@ require_once '../model/ArticleManager.php';
require_once '../model/Article.php'; require_once '../model/Article.php';
require_once '../validator/article-validator.php'; require_once '../validator/article-validator.php';
if (!isset($_SESSION["user"])) { if (!isset($_SESSION["user_email"])) {
header("Location: index.php?pfad=login"); header("Location: index.php?pfad=login");
exit(); exit();
} }
@@ -16,7 +16,7 @@ if (!isset($_SESSION["user"])) {
if ($_SERVER["REQUEST_METHOD"] === "POST") { if ($_SERVER["REQUEST_METHOD"] === "POST") {
$_SESSION["old_title"] = $_POST["title"] ?? ''; $_SESSION["old_title"] = $_POST["title"] ?? '';
$_SESSION["old_content"] = $_POST["content"] ?? ''; $_SESSION["old_content"] = $_POST["content"] ?? '';
$_SESSION["old_category"] = $_POST["category"] ?? ''; // TODO: die Kategorie im Dropdown setzen, wenn der Editor erneut geöffnet wird. $_SESSION["old_category"] = $_POST["category"] ?? '';
$_SESSION["old_tags"] = $_POST["tags"] ?? ''; $_SESSION["old_tags"] = $_POST["tags"] ?? '';
if (isset($_GET["id"]) && !empty($_GET["id"])) { if (isset($_GET["id"]) && !empty($_GET["id"])) {
@@ -30,7 +30,7 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
try { try {
$articleManager = ArticleManager::getInstance(); $articleManager = ArticleManager::getInstance();
$article = $articleManager->getArticle($id); $article = $articleManager->getArticle($id);
if ($article->getAuthor() != $_SESSION["user"]->getUsername()) { if ($article->getAuthor() != $_SESSION["user_email"]) {
$_SESSION["message"] = "unauthorized_access"; $_SESSION["message"] = "unauthorized_access";
header("location: ../../index.php"); header("location: ../../index.php");
exit(); exit();
+39 -10
View File
@@ -7,13 +7,14 @@
*/ */
class Article class Article
{ {
public $id; private $id;
public $title; private $title;
public $content; private $content;
public $author; private $author;
public $creationDate; private $creationDate;
public $category; private $category;
public $tags; private $tags;
private $likes;
/** /**
* Konstruktor * Konstruktor
@@ -26,7 +27,7 @@ class Article
* @param $tags string optionale Schlagworte für eine bessere Suche * @param $tags string optionale Schlagworte für eine bessere Suche
* @param $creationDate string Datum der Beitragserstellung * @param $creationDate string Datum der Beitragserstellung
*/ */
public function __construct(int $id, string $title, string $content, string $author, string $category, string $tags, string $creationDate) public function __construct(int $id, string $title, string $content, string $author, string $category, string $tags, string $creationDate, array $likes = [])
{ {
$this->id = $id; $this->id = $id;
$this->title = $title; $this->title = $title;
@@ -35,6 +36,7 @@ class Article
$this->creationDate = $creationDate; $this->creationDate = $creationDate;
$this->category = $category; $this->category = $category;
$this->tags = $tags; $this->tags = $tags;
$this->likes = $likes;
} }
/** /**
@@ -67,7 +69,7 @@ class Article
/** /**
* Gibt den Content eines Beitrags zurück. * Gibt den Content eines Beitrags zurück.
* TODO: Content muss noch definiert werden. *
* @return string * @return string
*/ */
public function getContent(): string public function getContent(): string
@@ -77,7 +79,7 @@ class Article
/** /**
* Setzt den Content eines Beitrags. * Setzt den Content eines Beitrags.
* TODO: Content muss noch definiert werden. *
* @param $content * @param $content
* @return void * @return void
*/ */
@@ -141,7 +143,34 @@ class Article
$this->tags = $tags; $this->tags = $tags;
} }
/**
* Gibt alle User-IDs zurück, die diesen Beitrag geliked haben.
* @return array
*/
public function getLikes(): array
{
return $this->likes;
}
/**
* Gibt die Gesamtzahl der Likes zurück.
* @return int
*/
public function getLikeCount(): int
{
return count($this->likes);
}
/**
* Prüft, ob ein bestimmter Nutzer den Beitrag bereits geliked hat.
*
* @param string $userId
* @return bool
*/
public function hasLiked(string $userId): bool
{
return in_array($userId, $this->likes);
}
} }
?> ?>
+12
View File
@@ -92,6 +92,18 @@ interface ArticleManagerDAO
*/ */
public function getArticlesByCategory($category); public function getArticlesByCategory($category);
/**
* Registriert oder entfernt ein Like eines Nutzers für einen Beitrag.
* Wenn schon geliked -> Unlike
* Wenn noch nicht geliked -> Like
*
* @param int $articleId Die ID des Beitrags
* @param string $userId Die ID des Nutzers
* @return bool True wenn geliked, False wenn unliked
* @throws InternalServerErrorException
* @throws NotFoundException
*/
public function toggleLike(int $articleId, string $userId): bool;
} }
?> ?>
+91 -4
View File
@@ -23,6 +23,7 @@ class DatabaseArticleManager implements ArticleManagerDAO {
$db = $this->getConnection(); $db = $this->getConnection();
// Tabelle für Beiträge
$db->exec(" $db->exec("
CREATE TABLE articles ( CREATE TABLE articles (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -33,6 +34,15 @@ class DatabaseArticleManager implements ArticleManagerDAO {
tags TEXT, tags TEXT,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP 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); unset($db);
} catch (PDOException $e) { } catch (PDOException $e) {
throw new InternalServerErrorException($e->getMessage()); throw new InternalServerErrorException($e->getMessage());
@@ -189,6 +199,8 @@ class DatabaseArticleManager implements ArticleManagerDAO {
$row = $command->fetch(PDO::FETCH_ASSOC); $row = $command->fetch(PDO::FETCH_ASSOC);
if ($row) { if ($row) {
$likes = $this->getLikesForArticle(intval($row['id']));
return new Article( return new Article(
intval($row['id']), intval($row['id']),
$row['title'], $row['title'],
@@ -196,7 +208,8 @@ class DatabaseArticleManager implements ArticleManagerDAO {
$row['author'], $row['author'],
$row['category'], $row['category'],
$row['tags'], $row['tags'],
$row['created'] $row['created'],
$likes
); );
} }
@@ -254,6 +267,8 @@ class DatabaseArticleManager implements ArticleManagerDAO {
$filteredArticles = []; $filteredArticles = [];
foreach ($rows as $row) { foreach ($rows as $row) {
$likes = $this->getLikesForArticle(intval($row['id']));
$filteredArticles[] = new Article( $filteredArticles[] = new Article(
intval($row['id']), intval($row['id']),
$row['title'], $row['title'],
@@ -261,7 +276,8 @@ class DatabaseArticleManager implements ArticleManagerDAO {
$row['author'], $row['author'],
$row['category'], $row['category'],
$row['tags'], $row['tags'],
$row['created'] $row['created'],
$likes
); );
} }
@@ -287,6 +303,8 @@ class DatabaseArticleManager implements ArticleManagerDAO {
$filteredArticles = []; $filteredArticles = [];
foreach ($rows as $row) { foreach ($rows as $row) {
$likes = $this->getLikesForArticle(intval($row['id']));
$filteredArticles[] = new Article( $filteredArticles[] = new Article(
intval($row['id']), intval($row['id']),
$row['title'], $row['title'],
@@ -294,7 +312,8 @@ class DatabaseArticleManager implements ArticleManagerDAO {
$row['author'], $row['author'],
$row['category'], $row['category'],
$row['tags'], $row['tags'],
$row['created'] $row['created'],
$likes
); );
} }
@@ -341,6 +360,8 @@ class DatabaseArticleManager implements ArticleManagerDAO {
$filteredArticles = []; $filteredArticles = [];
foreach ($rows as $row) { foreach ($rows as $row) {
$likes = $this->getLikesForArticle(intval($row['id']));
$filteredArticles[] = new Article( $filteredArticles[] = new Article(
intval($row['id']), intval($row['id']),
$row['title'] ?? '', $row['title'] ?? '',
@@ -348,7 +369,8 @@ class DatabaseArticleManager implements ArticleManagerDAO {
$row['author'] ?? '', $row['author'] ?? '',
$row['category'] ?? '', $row['category'] ?? '',
$row['tags'] ?? '', $row['tags'] ?? '',
$row['created'] ?? '' // Nutzt 'created' aus deiner DB-Struktur $row['created'] ?? '',
$likes
); );
} }
@@ -359,4 +381,69 @@ class DatabaseArticleManager implements ArticleManagerDAO {
} }
} }
/**
* Holt alle User-IDs, die einen bestimmten Beitrag geliked haben.
*
* @return String[] UserIDs
* @throws InternalServerErrorException
*/
private function getLikesForArticle(int $articleId): array
{
try {
$db = $this->getConnection();
$sql = "SELECT user_id FROM likes WHERE article_id = :article_id;";
$command = $db->prepare($sql);
$command->execute([':article_id' => $articleId]);
return $command->fetchAll(PDO::FETCH_COLUMN) ?: [];
} catch (PDOException $e) {
return [];
}
}
public function toggleLike(int $articleId, string $userId): bool
{
// prüfen, ob der Artikel überhaupt existiert
$article = $this->getArticle($articleId);
if (!$article) {
throw new NotFoundException("missing_id");
}
try {
$db = $this->getConnection();
// prüfen, ob das Like bereits existiert
$checkSql = "SELECT COUNT(*) FROM likes WHERE article_id = :article_id AND user_id = :user_id;";
$checkCommand = $db->prepare($checkSql);
$checkCommand->execute([
':article_id' => $articleId,
':user_id' => $userId
]);
$hasLiked = (int)$checkCommand->fetchColumn() > 0;
if ($hasLiked) {
// wenn bereits geliked -> Unlike
$deleteSql = "DELETE FROM likes WHERE article_id = :article_id AND user_id = :user_id;";
$deleteCommand = $db->prepare($deleteSql);
$deleteCommand->execute([
':article_id' => $articleId,
':user_id' => $userId
]);
return false; // gibt false zurück, da der Beitrag jetzt nicht mehr geliked ist
} else {
// wenn noch nicht geliked -> Like
$insertSql = "INSERT INTO likes (article_id, user_id) VALUES (:article_id, :user_id);";
$insertCommand = $db->prepare($insertSql);
$insertCommand->execute([
':article_id' => $articleId,
':user_id' => $userId
]);
return true; // gibt true zurück, da der Beitrag jetzt geliked ist
}
} catch (PDOException $e) {
throw new InternalServerErrorException("internal_error");
}
}
} }
+64 -6
View File
@@ -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;
}
} }
?> ?>