diff --git a/src/Routes/Favorites/AddToFavorites.php b/src/Routes/Favorites/AddToFavorites.php new file mode 100644 index 00000000..e1ecc1cd --- /dev/null +++ b/src/Routes/Favorites/AddToFavorites.php @@ -0,0 +1,52 @@ +privilegeService = $privilegeService; + $this->authService = $authService; + $this->postService = $postService; + $this->favoritesService = $favoritesService; + $this->userViewProxy = $userViewProxy; + } + + public function getMethods() + { + return ['POST', 'PUT']; + } + + public function getUrl() + { + return '/api/posts/:postNameOrId/favorites'; + } + + public function work() + { + $this->privilegeService->assertLoggedIn(); + $user = $this->authService->getLoggedInUser(); + $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $this->favoritesService->addFavorite($user, $post); + + $users = $this->favoritesService->getFavoriteUsers($post); + return ['data' => $this->userViewProxy->fromArray($users)]; + } +} diff --git a/src/Routes/Favorites/GetFavoriteUsers.php b/src/Routes/Favorites/GetFavoriteUsers.php new file mode 100644 index 00000000..0c99a66e --- /dev/null +++ b/src/Routes/Favorites/GetFavoriteUsers.php @@ -0,0 +1,48 @@ +privilegeService = $privilegeService; + $this->authService = $authService; + $this->postService = $postService; + $this->favoritesService = $favoritesService; + $this->userViewProxy = $userViewProxy; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/posts/:postNameOrId/favorites'; + } + + public function work() + { + $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $users = $this->favoritesService->getFavoriteUsers($post); + return ['data' => $this->userViewProxy->fromArray($users)]; + } +} diff --git a/src/Controllers/FavoritesController.php b/src/Routes/Favorites/RemoveFromFavorites.php similarity index 51% rename from src/Controllers/FavoritesController.php rename to src/Routes/Favorites/RemoveFromFavorites.php index da8875c3..b96037ea 100644 --- a/src/Controllers/FavoritesController.php +++ b/src/Routes/Favorites/RemoveFromFavorites.php @@ -1,13 +1,13 @@ userViewProxy = $userViewProxy; } - public function registerRoutes(Router $router) + public function getMethods() { - $router->get('/api/posts/:postNameOrId/favorites', [$this, 'getFavoriteUsers']); - $router->post('/api/posts/:postNameOrId/favorites', [$this, 'addFavorite']); - $router->delete('/api/posts/:postNameOrId/favorites', [$this, 'deleteFavorite']); + return ['DELETE']; } - public function getFavoriteUsers($postNameOrId) + public function getUrl() { - $post = $this->postService->getByNameOrId($postNameOrId); + return '/api/posts/:postNameOrId/favorites'; + } + + public function work() + { + $this->privilegeService->assertLoggedIn(); + $user = $this->authService->getLoggedInUser(); + $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $this->favoritesService->deleteFavorite($user, $post); + $users = $this->favoritesService->getFavoriteUsers($post); return ['data' => $this->userViewProxy->fromArray($users)]; } - - public function addFavorite($postNameOrId) - { - $this->privilegeService->assertLoggedIn(); - $user = $this->authService->getLoggedInUser(); - $post = $this->postService->getByNameOrId($postNameOrId); - $this->favoritesService->addFavorite($user, $post); - return $this->getFavoriteUsers($postNameOrId); - } - - public function deleteFavorite($postNameOrId) - { - $this->privilegeService->assertLoggedIn(); - $user = $this->authService->getLoggedInUser(); - $post = $this->postService->getByNameOrId($postNameOrId); - $this->favoritesService->deleteFavorite($user, $post); - return $this->getFavoriteUsers($postNameOrId); - } } diff --git a/src/di.php b/src/di.php index 7deef7d5..05a26113 100644 --- a/src/di.php +++ b/src/di.php @@ -64,7 +64,6 @@ return [ $container->get(\Szurubooru\Controllers\PostNotesController::class), $container->get(\Szurubooru\Controllers\GlobalParamController::class), $container->get(\Szurubooru\Controllers\HistoryController::class), - $container->get(\Szurubooru\Controllers\FavoritesController::class), $container->get(\Szurubooru\Controllers\ScoreController::class), $container->get(\Szurubooru\Controllers\TagController::class), ]; @@ -78,6 +77,9 @@ return [ $container->get(\Szurubooru\Routes\Comments\DeleteComment::class), $container->get(\Szurubooru\Routes\Comments\GetComments::class), $container->get(\Szurubooru\Routes\Comments\GetPostComments::class), + $container->get(\Szurubooru\Routes\Favorites\GetFavoriteUsers::class), + $container->get(\Szurubooru\Routes\Favorites\AddToFavorites::class), + $container->get(\Szurubooru\Routes\Favorites\RemoveFromFavorites::class), ]; }), ];