From 1b23e44b06e253d828f487cce784a3182195eb82 Mon Sep 17 00:00:00 2001 From: Niels Tholenaar Date: Thu, 14 Apr 2016 12:08:21 +0200 Subject: [PATCH 1/2] Added REST tags endpoint (https://github.com/flarum/core/issues/600) --- .../src/Api/Controller/ListTagsController.php | 47 +++++++++++++++++++ extensions/tags/src/Listener/AddTagsApi.php | 1 + extensions/tags/src/TagRepository.php | 2 +- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 extensions/tags/src/Api/Controller/ListTagsController.php diff --git a/extensions/tags/src/Api/Controller/ListTagsController.php b/extensions/tags/src/Api/Controller/ListTagsController.php new file mode 100644 index 000000000..3c10f9697 --- /dev/null +++ b/extensions/tags/src/Api/Controller/ListTagsController.php @@ -0,0 +1,47 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Tags\Api\Controller; + +use Flarum\Api\Controller\AbstractCollectionController; +use Flarum\Tags\Api\Serializer\TagSerializer; +use Flarum\Tags\TagRepository; +use Psr\Http\Message\ServerRequestInterface; +use Tobscure\JsonApi\Document; + +class ListTagsController extends AbstractCollectionController +{ + /** + * {@inheritdoc} + */ + public $serializer = TagSerializer::class; + + /** + * @var \Flarum\Tags\TagRepository + */ + protected $tags; + + /** + * @param \Flarum\Tags\TagRepository $tags + */ + public function __construct(TagRepository $tags) + { + $this->tags = $tags; + } + + /** + * {@inheritdoc} + */ + protected function data(ServerRequestInterface $request, Document $document) + { + return $this->tags->all(); + } +} diff --git a/extensions/tags/src/Listener/AddTagsApi.php b/extensions/tags/src/Listener/AddTagsApi.php index 29f66d846..6190202e6 100755 --- a/extensions/tags/src/Listener/AddTagsApi.php +++ b/extensions/tags/src/Listener/AddTagsApi.php @@ -24,6 +24,7 @@ class AddTagsApi public function configureApiRoutes(ConfigureApiRoutes $event) { + $event->get('/tags', 'tags.index', Controller\ListTagsController::class); $event->post('/tags', 'tags.create', Controller\CreateTagController::class); $event->post('/tags/order', 'tags.order', Controller\OrderTagsController::class); $event->patch('/tags/{id}', 'tags.update', Controller\UpdateTagController::class); diff --git a/extensions/tags/src/TagRepository.php b/extensions/tags/src/TagRepository.php index 76687ec5e..4cad9deb5 100644 --- a/extensions/tags/src/TagRepository.php +++ b/extensions/tags/src/TagRepository.php @@ -41,7 +41,7 @@ class TagRepository */ public function all(User $user = null) { - $query = Tag::newQuery(); + $query = Tag::query(); return $this->scopeVisibleTo($query, $user)->get(); } From 14eb7c97d058cdb0bc40f0db3dc7750a88b3eb43 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 15 Apr 2016 21:00:43 +0900 Subject: [PATCH 2/2] Only list tags available to current user --- extensions/tags/src/Api/Controller/ListTagsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/tags/src/Api/Controller/ListTagsController.php b/extensions/tags/src/Api/Controller/ListTagsController.php index 3c10f9697..2aa112783 100644 --- a/extensions/tags/src/Api/Controller/ListTagsController.php +++ b/extensions/tags/src/Api/Controller/ListTagsController.php @@ -42,6 +42,6 @@ class ListTagsController extends AbstractCollectionController */ protected function data(ServerRequestInterface $request, Document $document) { - return $this->tags->all(); + return $this->tags->all($request->getAttribute('actor')); } }