diff --git a/src/Controllers/ScoreController.php b/src/Controllers/ScoreController.php deleted file mode 100644 index c0efc8ae..00000000 --- a/src/Controllers/ScoreController.php +++ /dev/null @@ -1,89 +0,0 @@ -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), - ]; - } -} diff --git a/src/Routes/Scores/AbstractScoreRoute.php b/src/Routes/Scores/AbstractScoreRoute.php new file mode 100644 index 00000000..44807816 --- /dev/null +++ b/src/Routes/Scores/AbstractScoreRoute.php @@ -0,0 +1,48 @@ +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(), + ]; + } +} diff --git a/src/Routes/Scores/GetCommentScore.php b/src/Routes/Scores/GetCommentScore.php new file mode 100644 index 00000000..50d87ea3 --- /dev/null +++ b/src/Routes/Scores/GetCommentScore.php @@ -0,0 +1,45 @@ +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); + } +} diff --git a/src/Routes/Scores/GetPostScore.php b/src/Routes/Scores/GetPostScore.php new file mode 100644 index 00000000..f56ea993 --- /dev/null +++ b/src/Routes/Scores/GetPostScore.php @@ -0,0 +1,45 @@ +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); + } +} diff --git a/src/Routes/Scores/SetCommentScore.php b/src/Routes/Scores/SetCommentScore.php new file mode 100644 index 00000000..91597e6c --- /dev/null +++ b/src/Routes/Scores/SetCommentScore.php @@ -0,0 +1,45 @@ +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); + } +} diff --git a/src/Routes/Scores/SetPostScore.php b/src/Routes/Scores/SetPostScore.php new file mode 100644 index 00000000..1256369e --- /dev/null +++ b/src/Routes/Scores/SetPostScore.php @@ -0,0 +1,45 @@ +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); + } +} diff --git a/src/di.php b/src/di.php index f4047fcf..930511e1 100644 --- a/src/di.php +++ b/src/di.php @@ -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), ]; }), ];