User posts

This commit is contained in:
2024-07-27 22:53:27 +02:00
parent a3ec9ae318
commit a950f6770a
6 changed files with 155 additions and 4 deletions
+1 -1
View File
@@ -22,7 +22,7 @@ class Post implements JsonSerializable
private string $content;
private DateTime $postedAt;
private function __construct(int $id, ?User $user, ?string $name, ?string $image, string $content, string $postedAt)
public function __construct(int $id, ?User $user, ?string $name, ?string $image, string $content, string $postedAt)
{
$this->id = $id;
$this->user = $user;
+43
View File
@@ -10,6 +10,7 @@ use Khofmann\Config\Config;
use JsonSerializable;
use Khofmann\ApiError\ApiError;
use Khofmann\GUID\GUID;
use Khofmann\Models\Post\Post;
use PDOException;
class User implements JsonSerializable
@@ -426,6 +427,48 @@ class User implements JsonSerializable
return $this;
}
public function posts(int $page, int $limit, string $sort)
{
$db = Database::getInstance();
$stmt = $db->prepare(
"SELECT
COUNT(*)
FROM
egb_gaestebuch
WHERE
benutzer_id = :ID"
);
$stmt->bindValue(":ID", $this->id);
$stmt->execute();
$count = $stmt->fetch(PDO::FETCH_COLUMN, 0);
$stmt = $db->prepare(
"SELECT
*
FROM
egb_gaestebuch
WHERE
benutzer_id = :ID
ORDER BY
id $sort
LIMIT $limit
OFFSET " . ($page * $limit)
);
$stmt->bindValue(":ID", $this->id);
$stmt->execute();
$data = $stmt->fetchAll();
$list = array_map(
function ($item) {
return new Post($item["id"], $this, null, null, $item["beitrag"], $item["zeitstempel"]);
},
$data
);
return ["pages" => intdiv($count, $limit + 1) + 1, "data" => $list];
}
/*
* Getters
*/