Post Count
This commit is contained in:
parent
7b897071f5
commit
d94c342bd6
@ -20,7 +20,7 @@ class User implements JsonSerializable
|
|||||||
private DateTime $memberSince;
|
private DateTime $memberSince;
|
||||||
private int $postCount;
|
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->id = $id;
|
||||||
$this->username = $username;
|
$this->username = $username;
|
||||||
@ -29,6 +29,7 @@ class User implements JsonSerializable
|
|||||||
$this->image = $image;
|
$this->image = $image;
|
||||||
$this->isAdmin = $isAdmin;
|
$this->isAdmin = $isAdmin;
|
||||||
$this->memberSince = new DateTime($timestamp);
|
$this->memberSince = new DateTime($timestamp);
|
||||||
|
$this->postCount = $postCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -39,7 +40,13 @@ class User implements JsonSerializable
|
|||||||
{
|
{
|
||||||
$db = Database::getInstance();
|
$db = Database::getInstance();
|
||||||
$stmt = $db->prepare(
|
$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->bindValue(":ID", $id);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
@ -47,14 +54,20 @@ class User implements JsonSerializable
|
|||||||
|
|
||||||
if (!$data) throw new Exception("NotFound");
|
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
|
public static function getByEmail(string $email): User
|
||||||
{
|
{
|
||||||
$db = Database::getInstance();
|
$db = Database::getInstance();
|
||||||
$stmt = $db->prepare(
|
$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->bindValue(":EMAIL", $email);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
@ -62,14 +75,20 @@ class User implements JsonSerializable
|
|||||||
|
|
||||||
if (!$data) throw new Exception("NotFound");
|
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
|
public static function getByToken(string $token): User
|
||||||
{
|
{
|
||||||
$db = Database::getInstance();
|
$db = Database::getInstance();
|
||||||
$stmt = $db->prepare(
|
$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->bindValue(":TOKEN", $token);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
@ -77,20 +96,29 @@ class User implements JsonSerializable
|
|||||||
|
|
||||||
if (!$data) throw new Exception("NotFound");
|
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
|
public static function logIn(string $email, string $password): array
|
||||||
{
|
{
|
||||||
$db = Database::getInstance();
|
$db = Database::getInstance();
|
||||||
// Get user data
|
// 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->bindValue(":EMAIL", $email);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$data = $stmt->fetch();
|
$data = $stmt->fetch();
|
||||||
|
|
||||||
if ($data) {
|
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"])) {
|
if (password_verify($password, $data["passwort"])) {
|
||||||
// REHASH for safety should it somehow change
|
// REHASH for safety should it somehow change
|
||||||
if (password_needs_rehash($data["passwort"], PASSWORD_DEFAULT)) {
|
if (password_needs_rehash($data["passwort"], PASSWORD_DEFAULT)) {
|
||||||
@ -219,6 +247,11 @@ class User implements JsonSerializable
|
|||||||
return $this->memberSince;
|
return $this->memberSince;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getPostCount(): int
|
||||||
|
{
|
||||||
|
return $this->postCount;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* JSON
|
* JSON
|
||||||
*/
|
*/
|
||||||
@ -226,13 +259,14 @@ class User implements JsonSerializable
|
|||||||
public function jsonSerialize(): array
|
public function jsonSerialize(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'id' => $this->getId(),
|
'id' => $this->id,
|
||||||
'username' => $this->getUsername(),
|
'username' => $this->username,
|
||||||
'status' => $this->getStatus(),
|
'status' => $this->status,
|
||||||
'email' => $this->getEmail(),
|
'email' => $this->email,
|
||||||
'image' => $this->getImage(),
|
'image' => $this->image,
|
||||||
'isAdmin' => $this->getIsAdmin(),
|
'isAdmin' => $this->isAdmin,
|
||||||
'memberSince' => $this->getMemberSince(),
|
'memberSince' => $this->memberSince,
|
||||||
|
'postCount' => $this->postCount,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user