Moved scores controller to routes
This commit is contained in:
parent
969f70318b
commit
2195b2c9a1
7 changed files with 232 additions and 90 deletions
|
@ -1,89 +0,0 @@
|
|||
<?php
|
||||
namespace Szurubooru\Controllers;
|
||||
use Szurubooru\Entities\Entity;
|
||||
use Szurubooru\Helpers\InputReader;
|
||||
use Szurubooru\Router;
|
||||
use Szurubooru\Services\AuthService;
|
||||
use Szurubooru\Services\CommentService;
|
||||
use Szurubooru\Services\PostService;
|
||||
use Szurubooru\Services\PrivilegeService;
|
||||
use Szurubooru\Services\ScoreService;
|
||||
|
||||
final class ScoreController extends AbstractController
|
||||
{
|
||||
private $privilegeService;
|
||||
private $authService;
|
||||
private $postService;
|
||||
private $commentService;
|
||||
private $scoreService;
|
||||
private $inputReader;
|
||||
|
||||
public function __construct(
|
||||
PrivilegeService $privilegeService,
|
||||
AuthService $authService,
|
||||
PostService $postService,
|
||||
CommentService $commentService,
|
||||
ScoreService $scoreService,
|
||||
InputReader $inputReader)
|
||||
{
|
||||
$this->privilegeService = $privilegeService;
|
||||
$this->authService = $authService;
|
||||
$this->postService = $postService;
|
||||
$this->commentService = $commentService;
|
||||
$this->scoreService = $scoreService;
|
||||
$this->inputReader = $inputReader;
|
||||
}
|
||||
|
||||
public function registerRoutes(Router $router)
|
||||
{
|
||||
$router->get('/api/posts/:postNameOrId/score', [$this, 'getPostScore']);
|
||||
$router->post('/api/posts/:postNameOrId/score', [$this, 'setPostScore']);
|
||||
$router->get('/api/comments/:commentId/score', [$this, 'getCommentScore']);
|
||||
$router->post('/api/comments/:commentId/score', [$this, 'setCommentScore']);
|
||||
}
|
||||
|
||||
public function getPostScore($postNameOrId)
|
||||
{
|
||||
$post = $this->postService->getByNameOrId($postNameOrId);
|
||||
return $this->getScore($post);
|
||||
}
|
||||
|
||||
public function setPostScore($postNameOrId)
|
||||
{
|
||||
$post = $this->postService->getByNameOrId($postNameOrId);
|
||||
return $this->setScore($post);
|
||||
}
|
||||
|
||||
public function getCommentScore($commentId)
|
||||
{
|
||||
$comment = $this->commentService->getById($commentId);
|
||||
return $this->getScore($comment);
|
||||
}
|
||||
|
||||
public function setCommentScore($commentId)
|
||||
{
|
||||
$comment = $this->commentService->getById($commentId);
|
||||
return $this->setScore($comment);
|
||||
}
|
||||
|
||||
private function setScore(Entity $entity)
|
||||
{
|
||||
$this->privilegeService->assertLoggedIn();
|
||||
$score = intval($this->inputReader->score);
|
||||
$user = $this->authService->getLoggedInUser();
|
||||
$result = $this->scoreService->setUserScore($user, $entity, $score);
|
||||
return [
|
||||
'score' => $this->scoreService->getScoreValue($entity),
|
||||
'ownScore' => $result->getScore(),
|
||||
];
|
||||
}
|
||||
|
||||
private function getScore(Entity $entity)
|
||||
{
|
||||
$user = $this->authService->getLoggedInUser();
|
||||
return [
|
||||
'score' => $this->scoreService->getScoreValue($entity),
|
||||
'ownScore' => $this->scoreService->getUserScoreValue($user, $entity),
|
||||
];
|
||||
}
|
||||
}
|
48
src/Routes/Scores/AbstractScoreRoute.php
Normal file
48
src/Routes/Scores/AbstractScoreRoute.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
namespace Szurubooru\Routes\Scores;
|
||||
use Szurubooru\Helpers\InputReader;
|
||||
use Szurubooru\Entities\Entity;
|
||||
use Szurubooru\Services\AuthService;
|
||||
use Szurubooru\Services\PrivilegeService;
|
||||
use Szurubooru\Services\ScoreService;
|
||||
use Szurubooru\Routes\AbstractRoute;
|
||||
|
||||
abstract class AbstractScoreRoute extends AbstractRoute
|
||||
{
|
||||
private $privilegeService;
|
||||
private $scoreService;
|
||||
private $authService;
|
||||
|
||||
public function __construct(
|
||||
AuthService $authService,
|
||||
InputReader $inputReader,
|
||||
PrivilegeService $privilegeService,
|
||||
ScoreService $scoreService)
|
||||
{
|
||||
$this->authService = $authService;
|
||||
$this->inputReader = $inputReader;
|
||||
$this->privilegeService = $privilegeService;
|
||||
$this->scoreService = $scoreService;
|
||||
}
|
||||
|
||||
protected function getScore(Entity $entity)
|
||||
{
|
||||
$user = $this->authService->getLoggedInUser();
|
||||
return [
|
||||
'score' => $this->scoreService->getScoreValue($entity),
|
||||
'ownScore' => $this->scoreService->getUserScoreValue($user, $entity),
|
||||
];
|
||||
}
|
||||
|
||||
protected function setScore(Entity $entity)
|
||||
{
|
||||
$this->privilegeService->assertLoggedIn();
|
||||
$score = intval($this->inputReader->score);
|
||||
$user = $this->authService->getLoggedInUser();
|
||||
$result = $this->scoreService->setUserScore($user, $entity, $score);
|
||||
return [
|
||||
'score' => $this->scoreService->getScoreValue($entity),
|
||||
'ownScore' => $result->getScore(),
|
||||
];
|
||||
}
|
||||
}
|
45
src/Routes/Scores/GetCommentScore.php
Normal file
45
src/Routes/Scores/GetCommentScore.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
namespace Szurubooru\Routes\Scores;
|
||||
use Szurubooru\Entities\Entity;
|
||||
use Szurubooru\Helpers\InputReader;
|
||||
use Szurubooru\Services\AuthService;
|
||||
use Szurubooru\Services\CommentService;
|
||||
use Szurubooru\Services\PrivilegeService;
|
||||
use Szurubooru\Services\ScoreService;
|
||||
|
||||
class GetCommentScore extends AbstractScoreRoute
|
||||
{
|
||||
private $commentService;
|
||||
|
||||
public function __construct(
|
||||
PrivilegeService $privilegeService,
|
||||
AuthService $authService,
|
||||
CommentService $commentService,
|
||||
ScoreService $scoreService,
|
||||
InputReader $inputReader)
|
||||
{
|
||||
parent::__construct(
|
||||
$authService,
|
||||
$inputReader,
|
||||
$privilegeService,
|
||||
$scoreService);
|
||||
|
||||
$this->commentService = $commentService;
|
||||
}
|
||||
|
||||
public function getMethods()
|
||||
{
|
||||
return ['GET'];
|
||||
}
|
||||
|
||||
public function getUrl()
|
||||
{
|
||||
return '/api/comments/:commentId/score';
|
||||
}
|
||||
|
||||
public function work()
|
||||
{
|
||||
$comment = $this->commentService->getById($this->getArgument('commentId'));
|
||||
return $this->getScore($comment);
|
||||
}
|
||||
}
|
45
src/Routes/Scores/GetPostScore.php
Normal file
45
src/Routes/Scores/GetPostScore.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
namespace Szurubooru\Routes\Scores;
|
||||
use Szurubooru\Entities\Entity;
|
||||
use Szurubooru\Helpers\InputReader;
|
||||
use Szurubooru\Services\AuthService;
|
||||
use Szurubooru\Services\PostService;
|
||||
use Szurubooru\Services\PrivilegeService;
|
||||
use Szurubooru\Services\ScoreService;
|
||||
|
||||
class GetPostScore extends AbstractScoreRoute
|
||||
{
|
||||
private $postService;
|
||||
|
||||
public function __construct(
|
||||
PrivilegeService $privilegeService,
|
||||
AuthService $authService,
|
||||
PostService $postService,
|
||||
ScoreService $scoreService,
|
||||
InputReader $inputReader)
|
||||
{
|
||||
parent::__construct(
|
||||
$authService,
|
||||
$inputReader,
|
||||
$privilegeService,
|
||||
$scoreService);
|
||||
|
||||
$this->postService = $postService;
|
||||
}
|
||||
|
||||
public function getMethods()
|
||||
{
|
||||
return ['GET'];
|
||||
}
|
||||
|
||||
public function getUrl()
|
||||
{
|
||||
return '/api/posts/:postNameOrId/score';
|
||||
}
|
||||
|
||||
public function work()
|
||||
{
|
||||
$post = $this->postService->getByNameOrId($this->getArgument('postNameOrId'));
|
||||
return $this->getScore($post);
|
||||
}
|
||||
}
|
45
src/Routes/Scores/SetCommentScore.php
Normal file
45
src/Routes/Scores/SetCommentScore.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
namespace Szurubooru\Routes\Scores;
|
||||
use Szurubooru\Entities\Entity;
|
||||
use Szurubooru\Helpers\InputReader;
|
||||
use Szurubooru\Services\AuthService;
|
||||
use Szurubooru\Services\CommentService;
|
||||
use Szurubooru\Services\PrivilegeService;
|
||||
use Szurubooru\Services\ScoreService;
|
||||
|
||||
class SetCommentScore extends AbstractScoreRoute
|
||||
{
|
||||
private $commentService;
|
||||
|
||||
public function __construct(
|
||||
PrivilegeService $privilegeService,
|
||||
AuthService $authService,
|
||||
CommentService $commentService,
|
||||
ScoreService $scoreService,
|
||||
InputReader $inputReader)
|
||||
{
|
||||
parent::__construct(
|
||||
$authService,
|
||||
$inputReader,
|
||||
$privilegeService,
|
||||
$scoreService);
|
||||
|
||||
$this->commentService = $commentService;
|
||||
}
|
||||
|
||||
public function getMethods()
|
||||
{
|
||||
return ['POST', 'PUT'];
|
||||
}
|
||||
|
||||
public function getUrl()
|
||||
{
|
||||
return '/api/comments/:commentId/score';
|
||||
}
|
||||
|
||||
public function work()
|
||||
{
|
||||
$comment = $this->commentService->getById($this->getArgument('commentId'));
|
||||
return $this->setScore($comment);
|
||||
}
|
||||
}
|
45
src/Routes/Scores/SetPostScore.php
Normal file
45
src/Routes/Scores/SetPostScore.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
namespace Szurubooru\Routes\Scores;
|
||||
use Szurubooru\Entities\Entity;
|
||||
use Szurubooru\Helpers\InputReader;
|
||||
use Szurubooru\Services\AuthService;
|
||||
use Szurubooru\Services\PostService;
|
||||
use Szurubooru\Services\PrivilegeService;
|
||||
use Szurubooru\Services\ScoreService;
|
||||
|
||||
class SetPostScore extends AbstractScoreRoute
|
||||
{
|
||||
private $postService;
|
||||
|
||||
public function __construct(
|
||||
PrivilegeService $privilegeService,
|
||||
AuthService $authService,
|
||||
PostService $postService,
|
||||
ScoreService $scoreService,
|
||||
InputReader $inputReader)
|
||||
{
|
||||
parent::__construct(
|
||||
$authService,
|
||||
$inputReader,
|
||||
$privilegeService,
|
||||
$scoreService);
|
||||
|
||||
$this->postService = $postService;
|
||||
}
|
||||
|
||||
public function getMethods()
|
||||
{
|
||||
return ['POST', 'PUT'];
|
||||
}
|
||||
|
||||
public function getUrl()
|
||||
{
|
||||
return '/api/posts/:postNameOrId/score';
|
||||
}
|
||||
|
||||
public function work()
|
||||
{
|
||||
$post = $this->postService->getByNameOrId($this->getArgument('postNameOrId'));
|
||||
return $this->setScore($post);
|
||||
}
|
||||
}
|
|
@ -58,7 +58,6 @@ return [
|
|||
'controllers' => DI\factory(function (DI\container $container) {
|
||||
return [
|
||||
$container->get(\Szurubooru\Controllers\UserAvatarController::class),
|
||||
$container->get(\Szurubooru\Controllers\ScoreController::class),
|
||||
];
|
||||
}),
|
||||
|
||||
|
@ -103,6 +102,10 @@ return [
|
|||
$container->get(\Szurubooru\Routes\Users\GetUsers::class),
|
||||
$container->get(\Szurubooru\Routes\Users\PasswordReset::class),
|
||||
$container->get(\Szurubooru\Routes\Users\UpdateUser::class),
|
||||
$container->get(\Szurubooru\Routes\Scores\GetCommentScore::class),
|
||||
$container->get(\Szurubooru\Routes\Scores\SetCommentScore::class),
|
||||
$container->get(\Szurubooru\Routes\Scores\GetPostScore::class),
|
||||
$container->get(\Szurubooru\Routes\Scores\SetPostScore::class),
|
||||
];
|
||||
}),
|
||||
];
|
||||
|
|
Loading…
Reference in a new issue