Endpoints
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
|
||||
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;
|
||||
@@ -13,6 +15,7 @@ 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;
|
||||
@@ -30,7 +33,46 @@ class Post implements JsonSerializable
|
||||
* Statics
|
||||
*/
|
||||
|
||||
public static function list(int $page, int $limit, bool $authed = false)
|
||||
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(
|
||||
@@ -63,6 +105,35 @@ class Post implements JsonSerializable
|
||||
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
|
||||
*/
|
||||
|
||||
@@ -177,7 +177,7 @@ class User implements JsonSerializable
|
||||
}
|
||||
}
|
||||
|
||||
public static function create(string $username, string $email, string $password): bool
|
||||
public static function create(string $username, string $email, string $password): User
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$guid = GUID::v4();
|
||||
@@ -192,6 +192,8 @@ class User implements JsonSerializable
|
||||
$stmt->bindValue(":EMA", $email);
|
||||
$stmt->bindValue(":COD", $guid);
|
||||
|
||||
$user = User::getByID($db->lastInsertId());
|
||||
|
||||
try {
|
||||
$stmt->execute();
|
||||
|
||||
@@ -201,7 +203,7 @@ class User implements JsonSerializable
|
||||
"Hello $username. To activate your account, visit https://khofmann.userpage.fu-berlin.de/phpCourse/exam/confirm?c=$guid"
|
||||
);
|
||||
|
||||
return true;
|
||||
return $user;
|
||||
} catch (Exception $err) {
|
||||
if ($err->getCode() === "23000") throw new Exception("Duplicate");
|
||||
|
||||
@@ -267,7 +269,7 @@ class User implements JsonSerializable
|
||||
return $stmt->execute();
|
||||
}
|
||||
|
||||
public function update(?string $username, ?string $password, $image = null): bool
|
||||
public function update(?string $username, ?string $password, $image = null): User
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
|
||||
@@ -299,15 +301,16 @@ class User implements JsonSerializable
|
||||
}
|
||||
if ($error) throw new Exception("FailedImage");
|
||||
|
||||
return true;
|
||||
return User::getByID($this->id);
|
||||
}
|
||||
|
||||
public function delete(): bool
|
||||
public function delete(): User
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare("DELETE FROM egb_benutzer WHERE id = :ID");
|
||||
$stmt->bindValue(":ID", $this->id);
|
||||
return $stmt->execute();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -16,4 +16,9 @@ class Request
|
||||
{
|
||||
return Request::request()->getHeader($name, $defaultValue, $tryParse);
|
||||
}
|
||||
|
||||
public static function token()
|
||||
{
|
||||
return Request::header("token");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user