User posts
This commit is contained in:
@@ -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
Reference in New Issue
Block a user