diff --git a/src/LinkRepository.php b/src/LinkRepository.php new file mode 100644 index 0000000..79a010c --- /dev/null +++ b/src/LinkRepository.php @@ -0,0 +1,73 @@ +pdo = $pdo; + } + + public function all(): array + { + return $this->pdo->query('SELECT * FROM links ORDER BY id DESC')->fetchAll(); + } + + public function find(int $id): ?array + { + $stmt = $this->pdo->prepare('SELECT * FROM links WHERE id = :id'); + $stmt->execute(['id' => $id]); + $row = $stmt->fetch(); + + return $row ?: null; + } + + public function create(string $title, string $description, string $linkUrl, ?string $thumbnail): int + { + $stmt = $this->pdo->prepare( + 'INSERT INTO links (title, description, link_url, thumbnail) + VALUES (:title, :description, :link_url, :thumbnail)' + ); + $stmt->execute([ + 'title' => $title, + 'description' => $description, + 'link_url' => $linkUrl, + 'thumbnail' => $thumbnail, + ]); + + return (int) $this->pdo->lastInsertId(); + } + + public function update(int $id, string $title, string $description, string $linkUrl, ?string $thumbnail): bool + { + $stmt = $this->pdo->prepare( + 'UPDATE links + SET title = :title, + description = :description, + link_url = :link_url, + thumbnail = :thumbnail, + updated_at = datetime(\'now\') + WHERE id = :id' + ); + $stmt->execute([ + 'id' => $id, + 'title' => $title, + 'description' => $description, + 'link_url' => $linkUrl, + 'thumbnail' => $thumbnail, + ]); + + return $stmt->rowCount() > 0; + } + + public function delete(int $id): bool + { + $stmt = $this->pdo->prepare('DELETE FROM links WHERE id = :id'); + $stmt->execute(['id' => $id]); + + return $stmt->rowCount() > 0; + } +}