68 lines
1.5 KiB
PHP
68 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace Api\Post;
|
|
|
|
use Exception;
|
|
use Khofmann\Api\Api;
|
|
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 post(): void
|
|
{
|
|
$content = Input::patch("content");
|
|
|
|
$self = User::getByToken(Request::token());
|
|
|
|
try {
|
|
Response::json(MPost::create($self, $content));
|
|
} catch (Exception $err) {
|
|
switch ($err->getMessage()) {
|
|
default:
|
|
throw $err;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function patch($id): void
|
|
{
|
|
$content = Input::patch("content");
|
|
|
|
$self = User::getByToken(Request::token());
|
|
$post = MPost::getByID($id);
|
|
|
|
if (!$self->getIsAdmin() && $post->getUser()->getID() !== $self->getID()) throw new Exception("Not Authorized", 401);
|
|
|
|
try {
|
|
Response::json(MPost::getByID($id)->update($content));
|
|
} catch (Exception $err) {
|
|
switch ($err->getMessage()) {
|
|
case "NotFound":
|
|
throw new Exception("Post not found", 404);
|
|
case "FailedContent":
|
|
throw new Exception("Failed to update content", 500);
|
|
default:
|
|
throw $err;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function delete($id): void
|
|
{
|
|
try {
|
|
Response::json(MPost::getByID($id)->delete());
|
|
} catch (Exception $err) {
|
|
switch ($err->getMessage()) {
|
|
case "NotFound":
|
|
throw new Exception("Post not found", 404);
|
|
default:
|
|
throw $err;
|
|
}
|
|
}
|
|
}
|
|
}
|