User posts
This commit is contained in:
parent
a3ec9ae318
commit
a950f6770a
36
exam/api/Users/Posts/Posts.php
Normal file
36
exam/api/Users/Posts/Posts.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Api\Users\Posts;
|
||||
|
||||
use Exception;
|
||||
use Khofmann\Api\Api;
|
||||
use Khofmann\Models\User\User;
|
||||
use Khofmann\Response\Response;
|
||||
use Khofmann\ApiError\ApiError;
|
||||
use Khofmann\Input\Input;
|
||||
|
||||
class Posts extends Api
|
||||
{
|
||||
public function get($id): void
|
||||
{
|
||||
// Fetch and constrain all parameters.
|
||||
$page = max(0, intval(Input::get("p", 0)));
|
||||
$limit = constrain(0, 30, intval(Input::get("l", 10)));
|
||||
$sort = Input::get("s", "asc");
|
||||
$sort = in_array($sort, ["asc", "desc"]) ? $sort : "asc";
|
||||
|
||||
// Try and get users posts
|
||||
// Throw errors according to situation.
|
||||
try {
|
||||
Response::json(User::getByID($id)->posts($page, $limit, $sort));
|
||||
} catch (Exception $err) {
|
||||
switch ($err->getMessage()) {
|
||||
case "NotFound":
|
||||
throw ApiError::notFound("user");
|
||||
default:
|
||||
// Due to how the failed field is handled, it's ApiError is inside the models update
|
||||
throw $err;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -593,6 +593,62 @@ paths:
|
||||
}
|
||||
tags:
|
||||
- User
|
||||
/users/{id}/posts:
|
||||
get:
|
||||
summary: Get user posts
|
||||
description: Get a users posts ID.
|
||||
security:
|
||||
- BasicAuth: []
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
description: User ID
|
||||
required: true
|
||||
schema:
|
||||
type: integer
|
||||
format: int14
|
||||
- in: query
|
||||
name: p
|
||||
schema:
|
||||
type: integer
|
||||
minimum: 0
|
||||
default: 0
|
||||
description: Current page.
|
||||
- in: query
|
||||
name: l
|
||||
schema:
|
||||
type: integer
|
||||
minimum: 0
|
||||
maximum: 30
|
||||
default: 10
|
||||
description: The number of items to return.
|
||||
- in: query
|
||||
name: s
|
||||
schema:
|
||||
type: string
|
||||
enum:
|
||||
- asc
|
||||
- desc
|
||||
default: asc
|
||||
description: Sort order by time.
|
||||
responses:
|
||||
200:
|
||||
description: Success.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/PostListResponse"
|
||||
404:
|
||||
description: User not found.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/NotFoundResponse"
|
||||
examples:
|
||||
User not found:
|
||||
value: { "code": "NotFound", "entity": "user" }
|
||||
tags:
|
||||
- User
|
||||
|
||||
externalDocs:
|
||||
url: https://khofmann.userpage.fu-berlin.de/phpCourse/exam/api/docs/
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -22,7 +22,7 @@ class Post implements JsonSerializable
|
||||
private string $content;
|
||||
private DateTime $postedAt;
|
||||
|
||||
private function __construct(int $id, ?User $user, ?string $name, ?string $image, string $content, string $postedAt)
|
||||
public function __construct(int $id, ?User $user, ?string $name, ?string $image, string $content, string $postedAt)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->user = $user;
|
||||
|
||||
@ -10,6 +10,7 @@ use Khofmann\Config\Config;
|
||||
use JsonSerializable;
|
||||
use Khofmann\ApiError\ApiError;
|
||||
use Khofmann\GUID\GUID;
|
||||
use Khofmann\Models\Post\Post;
|
||||
use PDOException;
|
||||
|
||||
class User implements JsonSerializable
|
||||
@ -426,6 +427,48 @@ class User implements JsonSerializable
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function posts(int $page, int $limit, string $sort)
|
||||
{
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare(
|
||||
"SELECT
|
||||
COUNT(*)
|
||||
FROM
|
||||
egb_gaestebuch
|
||||
WHERE
|
||||
benutzer_id = :ID"
|
||||
);
|
||||
$stmt->bindValue(":ID", $this->id);
|
||||
$stmt->execute();
|
||||
$count = $stmt->fetch(PDO::FETCH_COLUMN, 0);
|
||||
|
||||
|
||||
$stmt = $db->prepare(
|
||||
"SELECT
|
||||
*
|
||||
FROM
|
||||
egb_gaestebuch
|
||||
WHERE
|
||||
benutzer_id = :ID
|
||||
ORDER BY
|
||||
id $sort
|
||||
LIMIT $limit
|
||||
OFFSET " . ($page * $limit)
|
||||
);
|
||||
$stmt->bindValue(":ID", $this->id);
|
||||
$stmt->execute();
|
||||
$data = $stmt->fetchAll();
|
||||
|
||||
$list = array_map(
|
||||
function ($item) {
|
||||
return new Post($item["id"], $this, null, null, $item["beitrag"], $item["zeitstempel"]);
|
||||
},
|
||||
$data
|
||||
);
|
||||
|
||||
return ["pages" => intdiv($count, $limit + 1) + 1, "data" => $list];
|
||||
}
|
||||
|
||||
/*
|
||||
* Getters
|
||||
*/
|
||||
|
||||
@ -53,6 +53,8 @@ SimpleRouter::group(["middleware" => Khofmann\Auth\Auth::class], function () {
|
||||
SimpleRouter::post("/logout", [Api\Logout\Logout::class, "post"]);
|
||||
// Get user
|
||||
SimpleRouter::get("/users/{id}", [Api\Users\Users::class, "get"]);
|
||||
// Get user posts
|
||||
SimpleRouter::get("/users/{id}/posts", [Api\Users\Posts\Posts::class, "get"]);
|
||||
// Update self
|
||||
SimpleRouter::patch("/users/self", [Api\Users\Users::class, "patchSelf"]);
|
||||
// Update image self
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user