Split favorites controller to routes
This commit is contained in:
parent
333c538f1e
commit
602d7a1f45
4 changed files with 119 additions and 28 deletions
52
src/Routes/Favorites/AddToFavorites.php
Normal file
52
src/Routes/Favorites/AddToFavorites.php
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
namespace Szurubooru\Routes\Favorites;
|
||||
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
|
||||
use Szurubooru\Routes\AbstractRoute;
|
||||
use Szurubooru\Services\AuthService;
|
||||
use Szurubooru\Services\FavoritesService;
|
||||
use Szurubooru\Services\PostService;
|
||||
use Szurubooru\Services\PrivilegeService;
|
||||
|
||||
class AddToFavorites extends AbstractRoute
|
||||
{
|
||||
private $privilegeService;
|
||||
private $authService;
|
||||
private $postService;
|
||||
private $favoritesService;
|
||||
private $userViewProxy;
|
||||
|
||||
public function __construct(
|
||||
PrivilegeService $privilegeService,
|
||||
AuthService $authService,
|
||||
PostService $postService,
|
||||
FavoritesService $favoritesService,
|
||||
UserViewProxy $userViewProxy)
|
||||
{
|
||||
$this->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)];
|
||||
}
|
||||
}
|
48
src/Routes/Favorites/GetFavoriteUsers.php
Normal file
48
src/Routes/Favorites/GetFavoriteUsers.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
namespace Szurubooru\Routes\Favorites;
|
||||
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
|
||||
use Szurubooru\Routes\AbstractRoute;
|
||||
use Szurubooru\Services\AuthService;
|
||||
use Szurubooru\Services\FavoritesService;
|
||||
use Szurubooru\Services\PostService;
|
||||
use Szurubooru\Services\PrivilegeService;
|
||||
|
||||
class GetFavoriteUsers extends AbstractRoute
|
||||
{
|
||||
private $privilegeService;
|
||||
private $authService;
|
||||
private $postService;
|
||||
private $favoritesService;
|
||||
private $userViewProxy;
|
||||
|
||||
public function __construct(
|
||||
PrivilegeService $privilegeService,
|
||||
AuthService $authService,
|
||||
PostService $postService,
|
||||
FavoritesService $favoritesService,
|
||||
UserViewProxy $userViewProxy)
|
||||
{
|
||||
$this->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)];
|
||||
}
|
||||
}
|
|
@ -1,13 +1,13 @@
|
|||
<?php
|
||||
namespace Szurubooru\Controllers;
|
||||
namespace Szurubooru\Routes\Favorites;
|
||||
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
|
||||
use Szurubooru\Router;
|
||||
use Szurubooru\Routes\AbstractRoute;
|
||||
use Szurubooru\Services\AuthService;
|
||||
use Szurubooru\Services\FavoritesService;
|
||||
use Szurubooru\Services\PostService;
|
||||
use Szurubooru\Services\PrivilegeService;
|
||||
|
||||
final class FavoritesController extends AbstractController
|
||||
class RemoveFromFavorites extends AbstractRoute
|
||||
{
|
||||
private $privilegeService;
|
||||
private $authService;
|
||||
|
@ -29,35 +29,24 @@ final class FavoritesController extends AbstractController
|
|||
$this->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);
|
||||
}
|
||||
}
|
|
@ -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),
|
||||
];
|
||||
}),
|
||||
];
|
||||
|
|
Loading…
Reference in a new issue