szurubooru/src/Controllers/PostController.php

148 lines
5.1 KiB
PHP
Raw Normal View History

2014-09-15 11:38:24 +02:00
<?php
namespace Szurubooru\Controllers;
final class PostController extends AbstractController
{
2014-09-26 19:14:34 +02:00
private $config;
2014-09-15 11:38:24 +02:00
private $privilegeService;
private $postService;
2014-09-26 19:14:34 +02:00
private $postSearchParser;
2014-09-15 11:38:24 +02:00
private $inputReader;
private $postViewProxy;
2014-09-26 20:41:28 +02:00
private $snapshotViewProxy;
2014-09-15 11:38:24 +02:00
public function __construct(
2014-09-26 19:14:34 +02:00
\Szurubooru\Config $config,
2014-09-15 11:38:24 +02:00
\Szurubooru\Services\PrivilegeService $privilegeService,
\Szurubooru\Services\PostService $postService,
2014-09-26 19:14:34 +02:00
\Szurubooru\SearchServices\Parsers\PostSearchParser $postSearchParser,
2014-09-15 11:38:24 +02:00
\Szurubooru\Helpers\InputReader $inputReader,
2014-09-26 20:41:28 +02:00
\Szurubooru\Controllers\ViewProxies\PostViewProxy $postViewProxy,
\Szurubooru\Controllers\ViewProxies\SnapshotViewProxy $snapshotViewProxy)
2014-09-15 11:38:24 +02:00
{
2014-09-26 19:14:34 +02:00
$this->config = $config;
2014-09-15 11:38:24 +02:00
$this->privilegeService = $privilegeService;
$this->postService = $postService;
2014-09-26 19:14:34 +02:00
$this->postSearchParser = $postSearchParser;
2014-09-15 11:38:24 +02:00
$this->inputReader = $inputReader;
$this->postViewProxy = $postViewProxy;
2014-09-26 20:41:28 +02:00
$this->snapshotViewProxy = $snapshotViewProxy;
2014-09-15 11:38:24 +02:00
}
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-26 20:41:28 +02:00
$router->get('/api/posts/:postNameOrId/history', [$this, 'getHistory']);
2014-09-25 19:11:41 +02:00
$router->put('/api/posts/:postNameOrId', [$this, 'updatePost']);
2014-09-23 20:18:12 +02:00
$router->delete('/api/posts/:postNameOrId', [$this, 'deletePost']);
2014-09-24 23:24:51 +02:00
$router->post('/api/posts/:postNameOrId/feature', [$this, 'featurePost']);
$router->put('/api/posts/:postNameOrId/feature', [$this, 'featurePost']);
}
2014-09-26 20:41:28 +02:00
public function getByNameOrId($postNameOrId)
2014-09-24 23:24:51 +02:00
{
2014-09-26 20:41:28 +02:00
$post = $this->getByNameOrIdWithoutProxy($postNameOrId);
2014-09-25 23:53:47 +02:00
return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig());
2014-09-18 17:39:51 +02:00
}
2014-09-26 20:41:28 +02:00
public function getHistory($postNameOrId)
2014-09-18 17:39:51 +02:00
{
2014-09-26 20:41:28 +02:00
$post = $this->getByNameOrIdWithoutProxy($postNameOrId);
return ['data' => $this->snapshotViewProxy->fromArray($this->postService->getHistory($post))];
2014-09-15 11:38:24 +02:00
}
2014-09-17 18:34:57 +02:00
public function getFiltered()
{
2014-09-26 19:14:34 +02:00
$filter = $this->postSearchParser->createFilterFromInputReader($this->inputReader);
$filter->setPageSize($this->config->posts->postsPerPage);
$result = $this->postService->getFiltered($filter);
$entities = $this->postViewProxy->fromArray($result->getEntities(), $this->getLightFetchConfig());
2014-09-17 18:34:57 +02:00
return [
'data' => $entities,
2014-09-26 19:14:34 +02:00
'pageSize' => $result->getPageSize(),
'totalRecords' => $result->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);
2014-09-25 23:53:47 +02:00
return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig());
2014-09-15 11:38:24 +02:00
}
2014-09-23 20:18:12 +02:00
2014-09-25 19:11:41 +02:00
public function updatePost($postNameOrId)
{
$post = $this->postService->getByNameOrId($postNameOrId);
$formData = new \Szurubooru\FormData\PostEditFormData($this->inputReader);
if ($formData->content !== null)
$this->privilegeService->assertPrivilege(\Szurubooru\Privilege::CHANGE_POST_CONTENT);
if ($formData->thumbnail !== null)
$this->privilegeService->assertPrivilege(\Szurubooru\Privilege::CHANGE_POST_THUMBNAIL);
if ($formData->safety !== null)
$this->privilegeService->assertPrivilege(\Szurubooru\Privilege::CHANGE_POST_SAFETY);
if ($formData->source !== null)
$this->privilegeService->assertPrivilege(\Szurubooru\Privilege::CHANGE_POST_SOURCE);
if ($formData->tags !== null)
$this->privilegeService->assertPrivilege(\Szurubooru\Privilege::CHANGE_POST_TAGS);
$this->postService->updatePost($post, $formData);
$post = $this->postService->getByNameOrId($postNameOrId);
2014-09-25 23:53:47 +02:00
return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig());
2014-09-25 19:11:41 +02:00
}
2014-09-23 20:18:12 +02:00
public function deletePost($postNameOrId)
{
$post = $this->postService->getByNameOrId($postNameOrId);
$this->postService->deletePost($post);
}
2014-09-24 23:24:51 +02:00
public function featurePost($postNameOrId)
{
$post = $this->postService->getByNameOrId($postNameOrId);
$this->postService->featurePost($post);
}
2014-09-25 23:53:47 +02:00
2014-09-26 20:41:28 +02:00
private function getByNameOrIdWithoutProxy($postNameOrId)
{
if ($postNameOrId === 'featured')
return $this->postService->getFeatured();
else
return $this->postService->getByNameOrId($postNameOrId);
}
2014-09-25 23:53:47 +02:00
private function getFullFetchConfig()
{
return
[
\Szurubooru\Controllers\ViewProxies\PostViewProxy::FETCH_RELATIONS => true,
\Szurubooru\Controllers\ViewProxies\PostViewProxy::FETCH_TAGS => true,
\Szurubooru\Controllers\ViewProxies\PostViewProxy::FETCH_USER => true,
2014-09-28 16:56:15 +02:00
\Szurubooru\Controllers\ViewProxies\PostViewProxy::FETCH_HISTORY => true,
\Szurubooru\Controllers\ViewProxies\PostViewProxy::FETCH_OWN_SCORE => true,
\Szurubooru\Controllers\ViewProxies\PostViewProxy::FETCH_FAVORITES => true,
2014-09-25 23:53:47 +02:00
];
}
private function getLightFetchConfig()
{
return
[
\Szurubooru\Controllers\ViewProxies\PostViewProxy::FETCH_TAGS => true,
];
}
2014-09-15 11:38:24 +02:00
}