db = Database::getConnection(); } /** * Gibt alle Blogposts als Array von Post-Objekten zurück. * * @return Post[] Array mit allen Blogposts. * @throws Exception Wenn keine Einträge vorhanden sind. */ public function getPosts(): array { $posts = []; $query = $this->db->query(<<rowCount()) throw new Exception("no entries available"); while($row = $query->fetchObject()) { $posts[] = new Post($row->id, $row->title, $row->content, $row->username, $row->stamp); } return $posts; } /** * Gibt einen einzelnen Blogpost anhand der ID zurück. * * @param int $id Die ID des Blogposts. * @return Post|null Das Post-Objekt oder null, falls nicht gefunden. * @throws Exception Wenn kein Eintrag gefunden wurde. */ public function getPost($id): ?Post { $stmt = $this->db->prepare(<<bindParam(":id", $id, $this->db::PARAM_INT); $stmt->execute(); if(!$stmt->rowCount()) throw new Exception("no entry found"); $row = $stmt->fetchObject(); return new Post($row->id, $row->title, $row->content, $row->username, $row->stamp); } /** * Aktualisiert den Inhalt eines Blogposts. * * @param int $id Die ID des Blogposts. * @param string $content Der neue Inhalt. * @return bool Erfolg der Aktualisierung. */ public function updatePostContent($id, $content) { $stmt = $this->db->prepare(<<bindValue(':content', $content, \PDO::PARAM_STR); $stmt->bindValue(':id', $id, \PDO::PARAM_INT); return $stmt->execute(); } /** * Legt einen neuen Blogpost an. * * @param string $title Der Titel des Blogposts. * @param string $content Der Inhalt des Blogposts. * @param int $authorId Die ID des Autors. * @return bool Erfolg des Einfügens. */ public function createPost($title, $content, $authorId) { $stmt = $this->db->prepare(<<bindValue(':title', $title, \PDO::PARAM_STR); $stmt->bindValue(':content', $content, \PDO::PARAM_STR); $stmt->bindValue(':author_id', $authorId, \PDO::PARAM_INT); $stmt->bindValue(':stamp', time(), \PDO::PARAM_INT); return $stmt->execute(); } /** * Löscht einen Blogpost anhand der ID. * * @param int $id Die ID des Blogposts. * @return bool Erfolg des Löschens. */ public function deletePost($id) { $stmt = $this->db->prepare(<<bindValue(':id', $id, \PDO::PARAM_INT); return $stmt->execute(); } }