szurubooru/src/Controllers/PostController.php

68 lines
2.1 KiB
PHP
Raw Normal View History

2014-09-15 11:38:24 +02:00
<?php
namespace Szurubooru\Controllers;
final class PostController extends AbstractController
{
private $privilegeService;
private $postService;
private $inputReader;
private $postViewProxy;
public function __construct(
\Szurubooru\Services\PrivilegeService $privilegeService,
\Szurubooru\Services\PostService $postService,
\Szurubooru\Helpers\InputReader $inputReader,
\Szurubooru\Controllers\ViewProxies\PostViewProxy $postViewProxy)
{
$this->privilegeService = $privilegeService;
$this->postService = $postService;
$this->inputReader = $inputReader;
$this->postViewProxy = $postViewProxy;
}
public function registerRoutes(\Szurubooru\Router $router)
{
$router->post('/api/posts', [$this, 'createPost']);
2014-09-18 17:39:51 +02:00
$router->get('/api/posts', [$this, 'getFiltered']);
$router->get('/api/posts/:postNameOrId', [$this, 'getByNameOrId']);
2014-09-23 20:18:12 +02:00
$router->delete('/api/posts/:postNameOrId', [$this, 'deletePost']);
2014-09-18 17:39:51 +02:00
}
public function getByNameOrId($postNameOrId)
{
$post = $this->postService->getByNameOrId($postNameOrId);
return $this->postViewProxy->fromEntity($post);
2014-09-15 11:38:24 +02:00
}
2014-09-17 18:34:57 +02:00
public function getFiltered()
{
$formData = new \Szurubooru\FormData\SearchFormData($this->inputReader);
$searchResult = $this->postService->getFiltered($formData);
2014-09-23 20:45:59 +02:00
$entities = $this->postViewProxy->fromArray($searchResult->getEntities());
2014-09-17 18:34:57 +02:00
return [
'data' => $entities,
2014-09-23 20:45:59 +02:00
'pageSize' => $searchResult->getPageSize(),
'totalRecords' => $searchResult->getTotalRecords()];
2014-09-17 18:34:57 +02:00
}
2014-09-15 11:38:24 +02:00
public function createPost()
{
$this->privilegeService->assertPrivilege(\Szurubooru\Privilege::UPLOAD_POSTS);
$formData = new \Szurubooru\FormData\UploadFormData($this->inputReader);
$this->privilegeService->assertPrivilege(\Szurubooru\Privilege::UPLOAD_POSTS);
if ($formData->anonymous)
$this->privilegeService->assertPrivilege(\Szurubooru\Privilege::UPLOAD_POSTS_ANONYMOUSLY);
$post = $this->postService->createPost($formData);
return $this->postViewProxy->fromEntity($post);
}
2014-09-23 20:18:12 +02:00
public function deletePost($postNameOrId)
{
$post = $this->postService->getByNameOrId($postNameOrId);
$this->postService->deletePost($post);
}
2014-09-15 11:38:24 +02:00
}