aboutsummaryrefslogtreecommitdiffstats
path: root/src/app/Models/Comment.php
diff options
context:
space:
mode:
authorThomas Vanbesien <tvanbesi@proton.me>2026-03-21 22:55:13 +0100
committerThomas Vanbesien <tvanbesi@proton.me>2026-03-21 22:55:13 +0100
commitf9ad3f4dc05252839457579303a4e0a0f94d8b80 (patch)
treec78b8b5ce41f1a1dc1a8b5e6bbda2643729d7c4e /src/app/Models/Comment.php
parentec77d2f77b96488b1bc170ced2abab12b3c19416 (diff)
downloadcamagru-f9ad3f4dc05252839457579303a4e0a0f94d8b80.tar.gz
camagru-f9ad3f4dc05252839457579303a4e0a0f94d8b80.zip
Add likes, comments, email notifications, and pagination to gallery
Diffstat (limited to 'src/app/Models/Comment.php')
-rw-r--r--src/app/Models/Comment.php39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/app/Models/Comment.php b/src/app/Models/Comment.php
new file mode 100644
index 0000000..464cfe2
--- /dev/null
+++ b/src/app/Models/Comment.php
@@ -0,0 +1,39 @@
+<?php
+
+declare(strict_types=1);
+// Comment model: database operations for the comments table.
+
+namespace App\Models;
+
+use App\Database;
+
+class Comment
+{
+ private \PDO $pdo;
+
+ public function __construct()
+ {
+ $this->pdo = Database::getInstance()->getPdo();
+ }
+
+ public function create(int $userId, int $postId, string $content): int
+ {
+ $stmt = $this->pdo->prepare(
+ 'INSERT INTO comments (user_id, post_id, content) VALUES (:user_id, :post_id, :content)'
+ );
+ $stmt->execute(['user_id' => $userId, 'post_id' => $postId, 'content' => $content]);
+ return (int) $this->pdo->lastInsertId();
+ }
+
+ public function findByPostId(int $postId): array
+ {
+ $stmt = $this->pdo->prepare(
+ 'SELECT comments.*, users.username FROM comments
+ JOIN users ON comments.user_id = users.id
+ WHERE comments.post_id = :post_id
+ ORDER BY comments.created_at ASC'
+ );
+ $stmt->execute(['post_id' => $postId]);
+ return $stmt->fetchAll();
+ }
+}