REST Naming convention
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user