This commit is contained in:
2024-07-29 22:06:57 +02:00
parent 5ce2215e44
commit 6a62ae58fc
22 changed files with 670 additions and 7 deletions
+15
View File
@@ -9,8 +9,23 @@ use Khofmann\Input\Input;
use Khofmann\Response\Response;
use Khofmann\Models\User\User;
/**
* Login route handlers
*/
class Login extends Api
{
/**
* Login POST handler
*
* Log in a user. Required inputs are `email` and `password`.
*
* Returns user and tokens
*
* @throws 400 Missing field
* @throws 401 Invalid credentials (login fails)
* @throws 404 User not found
* @throws 500 Failed to log in user
*/
public function post(): void
{
// Fetch all required inputs.
+12
View File
@@ -7,8 +7,20 @@ use Khofmann\Models\User\User;
use Khofmann\Request\Request;
use Khofmann\Response\Response;
/**
* Logout route handlers
*/
class Logout extends Api
{
/**
* Logout POST handler
*
* Logout a user. User is retrieved using the authentication `token`.
*
* Returns user.
*
* @throws 404 User not found
*/
public function post(): void
{
// Get user auth token.
+42 -1
View File
@@ -11,8 +11,16 @@ use Khofmann\Models\User\User;
use Khofmann\Request\Request;
use Khofmann\Response\Response;
/**
* Posts route handlers
*/
class Posts extends Api
{
/**
* Posts GET handler
*
* Lists posts. Optional parameters are `l` (limit of returned list) and `p` (page, i.e. offset).
*/
public function get()
{
// Fetch and constrain all parameters.
@@ -24,6 +32,15 @@ class Posts extends Api
Response::json(Post::list($page, $limit, $authed));
}
/**
* Posts POST handler
*
* Create a new posts. Required inputs are `content`. Optional parameter is `l` (limit of list for which the returned pages is calculated).
*
* Returns created post and resulting amount of pages for a given limit.
*
* @throws 400 Missing fields
*/
public function post(): void
{
// Fetch all required inputs.
@@ -47,6 +64,19 @@ class Posts extends Api
}
}
/**
* Posts PATCH handler
*
* Update a posts.
*
* Returns updated post.
*
* @param mixed $id ID of post to update
*
* @throws 401 Not authorized (trying to edit a different users post if not admin)
* @throws 404 Post not found
* @throws 500 Failed to update user
*/
public function patch($id): void
{
// Fetch all inputs.
@@ -75,9 +105,20 @@ class Posts extends Api
}
}
/**
* Posts DELETE handler
*
* Delete a post. Optional parameter is `l` (limit of list for which the returned pages is calculated).
*
* Returns deleted post and resulting amount of pages for a given limit.
*
* @param mixed $id ID of posts to delete
*
* @throws 404 Post not found
*/
public function delete($id): void
{
// Fetch ax(0, intval(Input::get("p", 0)));
// Fetch and constrain all parameters.
$limit = constrain(0, 30, intval(Input::get("l", 10)));
// Try delete, 404 if post was not found.
try {
+14
View File
@@ -10,8 +10,22 @@ use Khofmann\Response\Response;
use Khofmann\Models\User\User;
use Khofmann\Request\Request;
/**
* Refresh route handlers
*/
class Refresh extends Api
{
/**
* Refresh POST handler
*
* Refresh a users session. User is retrieved using the authentication `token`.
*
* Returns user and tokens.
*
* @throws 401 Missing field
* @throws 404 User not found
* @throws 500 Failed to refresh tokens
*/
public function post(): void
{
// Fetch all required inputs.
+25
View File
@@ -9,8 +9,22 @@ use Khofmann\Input\Input;
use Khofmann\Response\Response;
use Khofmann\Models\User\User;
/**
* Register route handlers
*/
class Register extends Api
{
/**
* Register POST handler
*
* Register a new user. Required inputs are `username`, `email`, and `password`.
*
* Returns user.
*
* @throws 400 Missing fields
* @throws 400 Duplicate
* @throws 404 Failure to create
*/
public function post(): void
{
// Fetch all required inputs.
@@ -39,6 +53,17 @@ class Register extends Api
}
}
/**
* Register PATCH handler
*
* Confirms a user. Required input is `code`.
*
* Returns user.
*
* @throws 400 Missing field
* @throws 404 User not found
* @throws 404 User already confirmed
*/
public function patch(): void
{
// Fetch all required inputs.
+25
View File
@@ -10,8 +10,23 @@ use Khofmann\Response\Response;
use Khofmann\ApiError\ApiError;
use Khofmann\Request\Request;
/**
* User image route handlers
*/
class Image extends Api
{
/**
* Image POST handler
*
* Set a new user image.
*
* Returns updated user.
*
* @param mixed $id User ID
*
* @throws 404 User not found
* @throws 500 Failed to update user image
*/
public function post($id): void
{
// Fetch all inputs.
@@ -33,6 +48,16 @@ class Image extends Api
}
}
/**
* Image POST handler
*
* Set a new user image. User is retrieved using the authentication `token`.
*
* Returns updated user.
*
* @throws 404 User not found
* @throws 500 Failed to update user image
*/
public function postSelf(): void
{
// Fetch all inputs.
+12
View File
@@ -9,8 +9,20 @@ use Khofmann\Response\Response;
use Khofmann\ApiError\ApiError;
use Khofmann\Input\Input;
/**
* User posts route handlers
*/
class Posts extends Api
{
/**
* Posts GET handler
*
* Lists posts for a user. Optional parameters are `l` (limit of returned list), `p` (page, i.e. offset), `s` (sort order).
*
* Returns list of posts.
*
* @throws 404 User not found
*/
public function get($id): void
{
// Fetch and constrain all parameters.
+57 -1
View File
@@ -10,8 +10,18 @@ use Khofmann\Response\Response;
use Khofmann\ApiError\ApiError;
use Khofmann\Request\Request;
/**
* Users route handlers
*/
class Users extends Api
{
/**
* Users GET handler
*
* Lists users. Optional parameters are `l` (limit of returned list) and `p` (page, i.e. offset).
*
* Returns list of users.
*/
public function list()
{
// Fetch and constrain all parameters.
@@ -22,6 +32,17 @@ class Users extends Api
Response::json(User::list($page, $limit));
}
/**
* User GET handler
*
* Get a single user.
*
* Returns user.
*
* @param mixed $id User ID
*
* @throws 404 User not found
*/
public function get($id): void
{
// Try and get a user, 404 if not found.
@@ -37,6 +58,18 @@ class Users extends Api
}
}
/**
* Users PATCH handler
*
* Update a user.
*
* Returns updated user.
*
* @param mixed $id User ID
*
* @throws 404 User not found
* @throws 500 Failed to update user
*/
public function patch($id): void
{
// Fetch all inputs.
@@ -59,6 +92,16 @@ class Users extends Api
}
}
/**
* Users PATCH handler
*
* Update a user. User is retrieved using the authentication `token`.
*
* Returns updated user.
*
* @throws 404 User not found
* @throws 500 Failed to update user
*/
public function patchSelf(): void
{
// Fetch all inputs.
@@ -82,11 +125,24 @@ class Users extends Api
}
}
/**
* Users DELETE handler
*
* Deletes a user. Optional parameter is `l` (limit of list for which the returned pages is calculated).
*
* Returns deleted user and resulting amount of pages for a given limit.
*
* @param mixed $id User ID
*
* @throws 404 User not found
*/
public function delete($id): void
{
// Fetch and constrain all parameters.
$limit = constrain(0, 30, intval(Input::get("l", 10)));
// Try to delete user, 404 if not found.
try {
Response::json(User::getByID($id)->delete());
Response::json(User::getByID($id)->delete($limit));
} catch (Exception $err) {
switch ($err->getMessage()) {
case "NotFound":