REST Naming convention
This commit is contained in:
parent
83c519fdfd
commit
9a2673aba2
@ -1,58 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Api\Post;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Khofmann\Api\Api;
|
|
||||||
use Khofmann\ApiError\ApiError;
|
|
||||||
use Khofmann\Input\Input;
|
|
||||||
use Khofmann\Models\Post\Post as MPost;
|
|
||||||
use Khofmann\Models\User\User;
|
|
||||||
use Khofmann\Request\Request;
|
|
||||||
use Khofmann\Response\Response;
|
|
||||||
|
|
||||||
class Post 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 = MPost::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(MPost::getByID($id)->delete());
|
|
||||||
} catch (Exception $err) {
|
|
||||||
switch ($err->getMessage()) {
|
|
||||||
case "NotFound":
|
|
||||||
throw ApiError::notFound("post");
|
|
||||||
default:
|
|
||||||
throw $err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,89 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Api\User;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Khofmann\Models\User\User as MUser;
|
|
||||||
use Khofmann\Input\Input;
|
|
||||||
use Khofmann\Response\Response;
|
|
||||||
use Khofmann\Api\Api;
|
|
||||||
use Khofmann\ApiError\ApiError;
|
|
||||||
use Khofmann\Request\Request;
|
|
||||||
|
|
||||||
class User extends Api
|
|
||||||
{
|
|
||||||
public function get($id): void
|
|
||||||
{
|
|
||||||
// Try and get a user, 404 if not found.
|
|
||||||
try {
|
|
||||||
Response::json(MUser::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(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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -2,14 +2,17 @@
|
|||||||
|
|
||||||
namespace Api\Users;
|
namespace Api\Users;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Khofmann\Api\Api;
|
use Khofmann\Api\Api;
|
||||||
use Khofmann\Input\Input;
|
use Khofmann\Input\Input;
|
||||||
use Khofmann\Models\User\User;
|
use Khofmann\Models\User\User;
|
||||||
use Khofmann\Response\Response;
|
use Khofmann\Response\Response;
|
||||||
|
use Khofmann\ApiError\ApiError;
|
||||||
|
use Khofmann\Request\Request;
|
||||||
|
|
||||||
class Users extends Api
|
class Users extends Api
|
||||||
{
|
{
|
||||||
public function get()
|
public function list()
|
||||||
{
|
{
|
||||||
// Fetch and constrain all parameters.
|
// Fetch and constrain all parameters.
|
||||||
$page = max(0, intval(Input::get("p", 0)));
|
$page = max(0, intval(Input::get("p", 0)));
|
||||||
@ -18,4 +21,79 @@ class Users extends Api
|
|||||||
// Return list of users.
|
// Return list of users.
|
||||||
Response::json(User::list($page, $limit));
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -200,7 +200,7 @@ paths:
|
|||||||
value: { "code": "MissingField", "fields": ["content"] }
|
value: { "code": "MissingField", "fields": ["content"] }
|
||||||
tags:
|
tags:
|
||||||
- Post
|
- Post
|
||||||
/post/{id}:
|
/posts/{id}:
|
||||||
patch:
|
patch:
|
||||||
summary: Update post
|
summary: Update post
|
||||||
description: Update post with ID. <br>
|
description: Update post with ID. <br>
|
||||||
@ -401,8 +401,7 @@ paths:
|
|||||||
$ref: "#/components/schemas/UserListResponse"
|
$ref: "#/components/schemas/UserListResponse"
|
||||||
tags:
|
tags:
|
||||||
- User
|
- User
|
||||||
|
/users/{id}:
|
||||||
/user/{id}:
|
|
||||||
get:
|
get:
|
||||||
summary: Get user
|
summary: Get user
|
||||||
description: Get user by ID.
|
description: Get user by ID.
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@ -52,11 +52,11 @@ SimpleRouter::group(["middleware" => Khofmann\Auth\Auth::class], function () {
|
|||||||
// Logout
|
// Logout
|
||||||
SimpleRouter::post("/logout", [Api\Logout\Logout::class, "post"]);
|
SimpleRouter::post("/logout", [Api\Logout\Logout::class, "post"]);
|
||||||
// Get user
|
// Get user
|
||||||
SimpleRouter::get("/user/{id}", [Api\User\User::class, "get"]);
|
SimpleRouter::get("/users/{id}", [Api\Users\Users::class, "get"]);
|
||||||
// Update self
|
// Update self
|
||||||
SimpleRouter::patch("/user/self", [Api\User\User::class, "patchSelf"]);
|
SimpleRouter::patch("/users/self", [Api\Users\Users::class, "patchSelf"]);
|
||||||
// Update post
|
// Update post
|
||||||
SimpleRouter::patch("/post/{id}", [Api\Post\Post::class, "patch"]);
|
SimpleRouter::patch("/posts/{id}", [Api\Posts\Posts::class, "patch"]);
|
||||||
// Create post
|
// Create post
|
||||||
SimpleRouter::post("/posts", [Api\Posts\Posts::class, "post"]);
|
SimpleRouter::post("/posts", [Api\Posts\Posts::class, "post"]);
|
||||||
});
|
});
|
||||||
@ -67,11 +67,11 @@ SimpleRouter::group(["middleware" => Khofmann\Auth\Auth::class], function () {
|
|||||||
|
|
||||||
SimpleRouter::group(["middleware" => Khofmann\Auth\AdminAuth::class], function () {
|
SimpleRouter::group(["middleware" => Khofmann\Auth\AdminAuth::class], function () {
|
||||||
// List users
|
// List users
|
||||||
SimpleRouter::get("/users", [Api\Users\Users::class, "get"]);
|
SimpleRouter::get("/users", [Api\Users\Users::class, "list"]);
|
||||||
// Update user
|
// Update user
|
||||||
SimpleRouter::patch("/user/{id}", [Api\User\User::class, "patch"]);
|
SimpleRouter::patch("/users/{id}", [Api\Users\Users::class, "patch"]);
|
||||||
// Delete user
|
// Delete user
|
||||||
SimpleRouter::delete("/user/{id}", [Api\User\User::class, "delete"]);
|
SimpleRouter::delete("/users/{id}", [Api\Users\Users::class, "delete"]);
|
||||||
// Delete post
|
// Delete post
|
||||||
SimpleRouter::delete("/post/{id}", [Api\Post\Post::class, "delete"]);
|
SimpleRouter::delete("/posts/{id}", [Api\Posts\Posts::class, "delete"]);
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user