179 lines
3.7 KiB
PHP
179 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace Khofmann\Models\Post;
|
|
|
|
use Api\User\User as UserUser;
|
|
use DateTime;
|
|
use Exception;
|
|
use Khofmann\Models\User\User;
|
|
use JsonSerializable;
|
|
use Khofmann\Database\Database;
|
|
use PDO;
|
|
|
|
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
|
|
private ?string $name;
|
|
private string $content;
|
|
private DateTime $postedAt;
|
|
|
|
public function __construct(int $id, ?User $user, ?string $name, string $content, string $postedAt)
|
|
{
|
|
$this->id = $id;
|
|
$this->user = $user;
|
|
$this->name = $name;
|
|
$this->content = $content;
|
|
$this->postedAt = new DateTime($postedAt);
|
|
}
|
|
|
|
/*
|
|
* Statics
|
|
*/
|
|
|
|
public static function getByID(int $id): Post
|
|
{
|
|
$db = Database::getInstance();
|
|
|
|
$stmt = $db->prepare(
|
|
"SELECT
|
|
*
|
|
FROM
|
|
egb_gaestebuch
|
|
WHERE
|
|
id = :ID"
|
|
);
|
|
$stmt->bindValue(":ID", $id);
|
|
$stmt->execute();
|
|
$data = $stmt->fetch();
|
|
|
|
if (!$data) throw new Exception("NotFound");
|
|
|
|
$user = User::getByID($data["benutzer_id"]);
|
|
return new Post($data["id"], $user, null, $data["beitrag"], $data["zeitstempel"]);
|
|
}
|
|
|
|
public static function create(User $user, string $content): Post
|
|
{
|
|
$db = Database::getInstance();
|
|
|
|
$stmt = $db->prepare(
|
|
"INSERT INTO
|
|
egb_gaestebuch(benutzer_id, beitrag)
|
|
VALUES(:USR, :CON)"
|
|
);
|
|
$stmt->bindValue(":USR", $user->getID());
|
|
$stmt->bindValue(":CON", $content);
|
|
|
|
$stmt->execute();
|
|
|
|
return Post::getByID($db->lastInsertId());
|
|
}
|
|
|
|
public static function list(int $page, int $limit, bool $authed = false): array
|
|
{
|
|
$db = Database::getInstance();
|
|
$stmt = $db->prepare(
|
|
"SELECT
|
|
COUNT(*)
|
|
FROM
|
|
egb_gaestebuch"
|
|
);
|
|
$stmt->execute();
|
|
$count = $stmt->fetch(PDO::FETCH_COLUMN, 0);
|
|
$stmt = $db->prepare(
|
|
"SELECT
|
|
*
|
|
FROM
|
|
egb_gaestebuch
|
|
LIMIT $limit
|
|
OFFSET " . ($page * $limit)
|
|
);
|
|
$stmt->execute();
|
|
$data = $stmt->fetchAll();
|
|
|
|
$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"]);
|
|
},
|
|
$data
|
|
);
|
|
|
|
return ["pages" => intdiv($count, $limit) + 1, "data" => $list];
|
|
}
|
|
|
|
/*
|
|
* Members
|
|
*/
|
|
|
|
public function update(?string $content): Post
|
|
{
|
|
$db = Database::getInstance();
|
|
|
|
$error = false;
|
|
if (!empty($content)) {
|
|
$stmt = $db->prepare("UPDATE egb_gaestebuch SET beitrag = :CON WHERE id = :ID");
|
|
$stmt->bindValue(":CON", $content);
|
|
$stmt->bindValue(":ID", $this->id);
|
|
$error = !$stmt->execute();
|
|
}
|
|
if ($error) throw new Exception("FailedContent");
|
|
|
|
return Post::getByID($this->id);
|
|
}
|
|
|
|
public function delete(): Post
|
|
{
|
|
$db = Database::getInstance();
|
|
$stmt = $db->prepare("DELETE FROM egb_gaestebuch WHERE id = :ID");
|
|
$stmt->bindValue(":ID", $this->id);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/*
|
|
* Getters
|
|
*/
|
|
|
|
public function getId(): int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function getUser(): User
|
|
{
|
|
return $this->user;
|
|
}
|
|
|
|
public function getContent(): string
|
|
{
|
|
return $this->content;
|
|
}
|
|
|
|
public function getPostedAt(): DateTime
|
|
{
|
|
return $this->postedAt;
|
|
}
|
|
|
|
/*
|
|
* JSON
|
|
*/
|
|
|
|
public function jsonSerialize(): array
|
|
{
|
|
$user = $this->user ? $this->user : [
|
|
"username" => $this->name,
|
|
];
|
|
|
|
return [
|
|
'id' => $this->id,
|
|
'user' => $user,
|
|
'content' => $this->content,
|
|
'postedAt' => $this->postedAt,
|
|
];
|
|
}
|
|
}
|