Initial Post list

This commit is contained in:
2024-07-25 20:55:35 +02:00
parent 9a2673aba2
commit bb1e0eebf5
16 changed files with 327 additions and 96 deletions
+13 -6
View File
@@ -16,16 +16,18 @@ class Post implements JsonSerializable
private int $id;
// User is set if the post was fetched by an authenticated user
private ?User $user;
// Name is set if the post was fetched by a non authenticated user
// Name and image are set if the post was fetched by a non authenticated user
private ?string $name;
private ?string $image;
private string $content;
private DateTime $postedAt;
private function __construct(int $id, ?User $user, ?string $name, string $content, string $postedAt)
private function __construct(int $id, ?User $user, ?string $name, ?string $image, string $content, string $postedAt)
{
$this->id = $id;
$this->user = $user;
$this->name = $name;
$this->image = $image;
$this->content = $content;
$this->postedAt = new DateTime($postedAt);
}
@@ -53,11 +55,13 @@ class Post implements JsonSerializable
if (!$data) throw new Exception("NotFound");
$user = User::getByID($data["benutzer_id"]);
return new Post($data["id"], $user, null, $data["beitrag"], $data["zeitstempel"]);
return new Post($data["id"], $user, null, null, $data["beitrag"], $data["zeitstempel"]);
}
public static function create(User $user, string $content): Post
{
$content = substr(trim($content), 0, 250);
$db = Database::getInstance();
$stmt = $db->prepare(
@@ -98,7 +102,7 @@ class Post implements JsonSerializable
$list = array_map(
function ($item) use ($authed) {
$user = User::getByID($item["benutzer_id"]);
return new Post($item["id"], $authed ? $user : null, !$authed ? $user->getUsername() : null, $item["beitrag"], $item["zeitstempel"]);
return new Post($item["id"], $authed ? $user : null, !$authed ? $user->getUsername() : null, !$authed ? $user->getImage() : null, $item["beitrag"], $item["zeitstempel"]);
},
$data
);
@@ -115,12 +119,14 @@ class Post implements JsonSerializable
$db = Database::getInstance();
if (!empty($content)) {
$content = substr(trim($content), 0, 250);
$stmt = $db->prepare("UPDATE egb_gaestebuch SET beitrag = :CON WHERE id = :ID");
$stmt->bindValue(":CON", $content);
$stmt->bindValue(":CON", nl2br(htmlspecialchars($content)));
$stmt->bindValue(":ID", $this->id);
try {
if (!$stmt->execute()) throw ApiError::failedUpdate(["content"]);
} catch (Exception $e) {
} catch (Exception) {
throw ApiError::failedUpdate(["content"]);
}
}
@@ -169,6 +175,7 @@ class Post implements JsonSerializable
{
$user = $this->user ? $this->user : [
"username" => $this->name,
"image" => $this->image,
];
return [