diff --git a/exam/api/Post/Post.php b/exam/api/Post/Post.php
deleted file mode 100644
index c24d23a..0000000
--- a/exam/api/Post/Post.php
+++ /dev/null
@@ -1,58 +0,0 @@
-getIsAdmin() && $post->getUser()->getID() !== $self->getID()) throw ApiError::unauthorized("Not allowed");
-
- // Try update.
- Response::json($post->update($content));
- } catch (Exception $err) {
- switch ($err->getMessage()) {
- case "NotFound":
- throw ApiError::notFound("post");
- default:
- // Due to how the failed field is handled, it's ApiError is inside the models update
- throw $err;
- }
- }
- }
-
- public function delete($id): void
- {
- // Try delete, 404 if post was not found.
- try {
- Response::json(MPost::getByID($id)->delete());
- } catch (Exception $err) {
- switch ($err->getMessage()) {
- case "NotFound":
- throw ApiError::notFound("post");
- default:
- throw $err;
- }
- }
- }
-}
diff --git a/exam/api/Posts/Posts.php b/exam/api/Posts/Posts.php
index b853e2b..eecb5a8 100644
--- a/exam/api/Posts/Posts.php
+++ b/exam/api/Posts/Posts.php
@@ -44,4 +44,47 @@ class Posts extends Api
}
}
}
+
+ public function patch($id): void
+ {
+ // Fetch all inputs.
+ $content = Input::patch("content");
+
+ // Fetch authed user.
+ $self = User::getByToken(Request::token());
+
+ try {
+ // Try fetch the post in question, 404 if not found.
+ $post = Post::getByID($id);
+
+ // Throw 400 if we aren't admin but trying to edit another users post.
+ if (!$self->getIsAdmin() && $post->getUser()->getID() !== $self->getID()) throw ApiError::unauthorized("Not allowed");
+
+ // Try update.
+ Response::json($post->update($content));
+ } catch (Exception $err) {
+ switch ($err->getMessage()) {
+ case "NotFound":
+ throw ApiError::notFound("post");
+ default:
+ // Due to how the failed field is handled, it's ApiError is inside the models update
+ throw $err;
+ }
+ }
+ }
+
+ public function delete($id): void
+ {
+ // Try delete, 404 if post was not found.
+ try {
+ Response::json(Post::getByID($id)->delete());
+ } catch (Exception $err) {
+ switch ($err->getMessage()) {
+ case "NotFound":
+ throw ApiError::notFound("post");
+ default:
+ throw $err;
+ }
+ }
+ }
}
diff --git a/exam/api/User/User.php b/exam/api/User/User.php
deleted file mode 100644
index ea46085..0000000
--- a/exam/api/User/User.php
+++ /dev/null
@@ -1,89 +0,0 @@
-getMessage()) {
- case "NotFound":
- throw ApiError::notFound("user");
- default:
- throw $err;
- }
- }
- }
-
- public function patch($id): void
- {
- // Fetch all inputs.
- $username = Input::patch("username");
- $password = Input::patch("password");
- $image = Input::file("image");
-
- // Try and update user.
- // Throw errors according to situation.
- try {
- Response::json(MUser::getByID($id)->update($username, $password, $image));
- } 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;
- }
- }
- }
-
- public function patchSelf(): void
- {
- // Fetch all inputs.
- $token = Request::token();
- $username = Input::patch("username");
- $password = Input::patch("password");
- $image = Input::file("image");
-
- // Try and update user.
- // Throw errors according to situation.
- try {
- Response::json(MUser::getByToken($token)->update($username, $password, $image));
- } 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;
- }
- }
- }
-
- public function delete($id): void
- {
- // Try to delete user, 404 if not found.
- try {
- Response::json(MUser::getByID($id)->delete());
- } catch (Exception $err) {
- switch ($err->getMessage()) {
- case "NotFound":
- throw ApiError::notFound("user");
- default:
- throw $err;
- }
- }
- }
-}
diff --git a/exam/api/Users/Users.php b/exam/api/Users/Users.php
index 1020cb2..1bf0512 100644
--- a/exam/api/Users/Users.php
+++ b/exam/api/Users/Users.php
@@ -2,14 +2,17 @@
namespace Api\Users;
+use Exception;
use Khofmann\Api\Api;
use Khofmann\Input\Input;
use Khofmann\Models\User\User;
use Khofmann\Response\Response;
+use Khofmann\ApiError\ApiError;
+use Khofmann\Request\Request;
class Users extends Api
{
- public function get()
+ public function list()
{
// Fetch and constrain all parameters.
$page = max(0, intval(Input::get("p", 0)));
@@ -18,4 +21,79 @@ class Users extends Api
// Return list of users.
Response::json(User::list($page, $limit));
}
+
+ public function get($id): void
+ {
+ // Try and get a user, 404 if not found.
+ try {
+ Response::json(User::getByID($id));
+ } catch (Exception $err) {
+ switch ($err->getMessage()) {
+ case "NotFound":
+ throw ApiError::notFound("user");
+ default:
+ throw $err;
+ }
+ }
+ }
+
+ public function patch($id): void
+ {
+ // Fetch all inputs.
+ $username = Input::patch("username");
+ $password = Input::patch("password");
+ $image = Input::file("image");
+
+ // Try and update user.
+ // Throw errors according to situation.
+ try {
+ Response::json(User::getByID($id)->update($username, $password, $image));
+ } 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;
+ }
+ }
+ }
+
+ public function patchSelf(): void
+ {
+ // Fetch all inputs.
+ $token = Request::token();
+ $username = Input::patch("username");
+ $password = Input::patch("password");
+ $image = Input::file("image");
+
+ // Try and update user.
+ // Throw errors according to situation.
+ try {
+ Response::json(User::getByToken($token)->update($username, $password, $image));
+ } 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;
+ }
+ }
+ }
+
+ public function delete($id): void
+ {
+ // Try to delete user, 404 if not found.
+ try {
+ Response::json(User::getByID($id)->delete());
+ } catch (Exception $err) {
+ switch ($err->getMessage()) {
+ case "NotFound":
+ throw ApiError::notFound("user");
+ default:
+ throw $err;
+ }
+ }
+ }
}
diff --git a/exam/api/docs/api.yaml b/exam/api/docs/api.yaml
index 43fab94..7003808 100644
--- a/exam/api/docs/api.yaml
+++ b/exam/api/docs/api.yaml
@@ -200,7 +200,7 @@ paths:
value: { "code": "MissingField", "fields": ["content"] }
tags:
- Post
- /post/{id}:
+ /posts/{id}:
patch:
summary: Update post
description: Update post with ID.
@@ -401,8 +401,7 @@ paths:
$ref: "#/components/schemas/UserListResponse"
tags:
- User
-
- /user/{id}:
+ /users/{id}:
get:
summary: Get user
description: Get user by ID.
diff --git a/exam/api/docs/index.html b/exam/api/docs/index.html
index 9b0c613..b47c4d3 100644
--- a/exam/api/docs/index.html
+++ b/exam/api/docs/index.html
@@ -384,7 +384,7 @@ data-styled.g137[id="sc-kvXgyf"]{content:"fBvPoH,"}/*!sc*/
{- "content": "string"
}{- "id": 0,
- "user": {
- "id": 0,
- "username": "string",
- "status": 0,
- "email": "string",
- "image": "string",
- "isAdmin": true,
- "memberSince": {
- "date": "2019-08-24T14:15:22Z",
- "timezone_type": 0,
- "timezone": "string"
}, - "postCount": 0
}, - "content": "string",
- "postedAt": {
- "date": "2019-08-24T14:15:22Z",
- "timezone_type": 0,
- "timezone": "string"
}
}{- "content": "string"
}{- "id": 0,
- "user": {
- "id": 0,
- "username": "string",
- "status": 0,
- "email": "string",
- "image": "string",
- "isAdmin": true,
- "memberSince": {
- "date": "2019-08-24T14:15:22Z",
- "timezone_type": 0,
- "timezone": "string"
}, - "postCount": 0
}, - "content": "string",
- "postedAt": {
- "date": "2019-08-24T14:15:22Z",
- "timezone_type": 0,
- "timezone": "string"
}
}Update post with ID.
Requires logged in user to have admin permissions for posts not made by them.