Post Count

This commit is contained in:
Kilian Hofmann 2024-07-22 01:14:36 +02:00
parent 7b897071f5
commit d94c342bd6
2 changed files with 50 additions and 16 deletions

View File

@ -20,7 +20,7 @@ class User implements JsonSerializable
private DateTime $memberSince;
private int $postCount;
protected function __construct(int $id, string $username, int $status, string $email, string $timestamp, ?string $image, bool $isAdmin)
protected function __construct(int $id, string $username, int $status, string $email, string $timestamp, ?string $image, bool $isAdmin, int $postCount)
{
$this->id = $id;
$this->username = $username;
@ -29,6 +29,7 @@ class User implements JsonSerializable
$this->image = $image;
$this->isAdmin = $isAdmin;
$this->memberSince = new DateTime($timestamp);
$this->postCount = $postCount;
}
/*
@ -39,7 +40,13 @@ class User implements JsonSerializable
{
$db = Database::getInstance();
$stmt = $db->prepare(
"SELECT benutzer, status, email, image, isadmin, zeitstempel FROM egb_benutzer WHERE id = :ID"
"SELECT
b.benutzer, b.status, b.email, b.image, b.isadmin, b.zeitstempel,
(SELECT COUNT(*) FROM egb_gaestebuch WHERE benutzer_id = b.id) as postCount
FROM
egb_benutzer AS b
WHERE
b.id = :ID"
);
$stmt->bindValue(":ID", $id);
$stmt->execute();
@ -47,14 +54,20 @@ class User implements JsonSerializable
if (!$data) throw new Exception("NotFound");
return new User($id, $data["benutzer"], $data["status"], $data["email"], $data["zeitstempel"], $data["image"], $data["isadmin"] === 1);
return new User($id, $data["benutzer"], $data["status"], $data["email"], $data["zeitstempel"], $data["image"], $data["isadmin"] === 1, $data["postCount"]);
}
public static function getByEmail(string $email): User
{
$db = Database::getInstance();
$stmt = $db->prepare(
"SELECT id, benutzer, status, image, isadmin, zeitstempel FROM egb_benutzer WHERE email = :EMAIL"
"SELECT
b.id, b.benutzer, b.status, b.image, b.isadmin, b.zeitstempel,
(SELECT COUNT(*) FROM egb_gaestebuch WHERE benutzer_id = b.id) as postCount
FROM
egb_benutzer AS b
WHERE
email = :EMAIL"
);
$stmt->bindValue(":EMAIL", $email);
$stmt->execute();
@ -62,14 +75,20 @@ class User implements JsonSerializable
if (!$data) throw new Exception("NotFound");
return new User($data["id"], $data["benutzer"], $data["status"], $email, $data["zeitstempel"], $data["image"], $data["isadmin"] === 1);
return new User($data["id"], $data["benutzer"], $data["status"], $email, $data["zeitstempel"], $data["image"], $data["isadmin"] === 1, $data["postCount"]);
}
public static function getByToken(string $token): User
{
$db = Database::getInstance();
$stmt = $db->prepare(
"SELECT id, benutzer, status, email, image, isadmin, zeitstempel FROM egb_benutzer WHERE token = :TOKEN"
"SELECT
b.id, b.benutzer, b.status, b.email, b.image, b.isadmin, b.zeitstempel,
(SELECT COUNT(*) FROM egb_gaestebuch WHERE benutzer_id = b.id) as postCount
FROM
egb_benutzer AS b
WHERE
token = :TOKEN"
);
$stmt->bindValue(":TOKEN", $token);
$stmt->execute();
@ -77,20 +96,29 @@ class User implements JsonSerializable
if (!$data) throw new Exception("NotFound");
return new User($data["id"], $data["benutzer"], $data["status"], $data["email"], $data["zeitstempel"], $data["image"], $data["isadmin"] === 1);
return new User($data["id"], $data["benutzer"], $data["status"], $data["email"], $data["zeitstempel"], $data["image"], $data["isadmin"] === 1, $data["postCount"]);
}
public static function logIn(string $email, string $password): array
{
$db = Database::getInstance();
// Get user data
$stmt = $db->prepare("SELECT * FROM egb_benutzer WHERE email LIKE :EMAIL AND status = 1");
$stmt = $db->prepare(
"SELECT
b.*,
(SELECT COUNT(*) FROM egb_gaestebuch WHERE benutzer_id = b.id) as postCount
FROM
egb_benutzer AS b
WHERE
email LIKE :EMAIL AND
status = 1"
);
$stmt->bindValue(":EMAIL", $email);
$stmt->execute();
$data = $stmt->fetch();
if ($data) {
$user = new User($data["id"], $data["benutzer"], $data["status"], $email, $data["zeitstempel"], $data["image"], $data["isadmin"] === 1);
$user = new User($data["id"], $data["benutzer"], $data["status"], $email, $data["zeitstempel"], $data["image"], $data["isadmin"] === 1, $data["postCount"]);
if (password_verify($password, $data["passwort"])) {
// REHASH for safety should it somehow change
if (password_needs_rehash($data["passwort"], PASSWORD_DEFAULT)) {
@ -219,6 +247,11 @@ class User implements JsonSerializable
return $this->memberSince;
}
public function getPostCount(): int
{
return $this->postCount;
}
/*
* JSON
*/
@ -226,13 +259,14 @@ class User implements JsonSerializable
public function jsonSerialize(): array
{
return [
'id' => $this->getId(),
'username' => $this->getUsername(),
'status' => $this->getStatus(),
'email' => $this->getEmail(),
'image' => $this->getImage(),
'isAdmin' => $this->getIsAdmin(),
'memberSince' => $this->getMemberSince(),
'id' => $this->id,
'username' => $this->username,
'status' => $this->status,
'email' => $this->email,
'image' => $this->image,
'isAdmin' => $this->isAdmin,
'memberSince' => $this->memberSince,
'postCount' => $this->postCount,
];
}
}