]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
GET /api/tags/id_tag method
authorNicolas Lœuillet <nicolas@loeuillet.org>
Fri, 20 Feb 2015 15:38:24 +0000 (16:38 +0100)
committerNicolas Lœuillet <nicolas@loeuillet.org>
Fri, 6 Mar 2015 19:50:30 +0000 (20:50 +0100)
src/Wallabag/CoreBundle/Controller/WallabagRestController.php
src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Entity/Tag.php
src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php

index 276cfe1c78b18412aaa0577c753c3b3418280c08..cb68784d55d02080d5262ae3bdac5b61d4d8474c 100644 (file)
@@ -6,7 +6,6 @@ use Nelmio\ApiDocBundle\Annotation\ApiDoc;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
-use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 use Wallabag\CoreBundle\Entity\Entry;
 use Wallabag\CoreBundle\Entity\Tag;
 use Wallabag\CoreBundle\Service\Extractor;
@@ -244,12 +243,24 @@ class WallabagRestController extends Controller
      *
      * @ApiDoc(
      *       requirements={
-     *          {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag"}
+     *          {"name"="label", "dataType"="string", "requirement"="\w+", "description"="Label of the tag"}
      *       }
      * )
      */
-    public function getTagAction(Tag $tag)
+    public function getTagAction($label)
     {
+        $tag = $this
+            ->getDoctrine()
+            ->getRepository('WallabagCoreBundle:Tag')
+            ->findOneByLabel($label);
+
+        if (is_null($tag)) {
+            throw $this->createNotFoundException();
+        }
+
+        $json = $this->get('serializer')->serialize($tag, 'json');
+
+        return new Response($json, 200, array('application/json'));
     }
 
     /**
diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php
new file mode 100644 (file)
index 0000000..6b13c2b
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+
+namespace Wallabag\CoreBundle\DataFixtures\ORM;
+
+use Doctrine\Common\DataFixtures\AbstractFixture;
+use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
+use Doctrine\Common\Persistence\ObjectManager;
+use Wallabag\CoreBundle\Entity\Tag;
+
+class LoadTagData extends AbstractFixture implements OrderedFixtureInterface
+{
+    /**
+     * {@inheritDoc}
+     */
+    public function load(ObjectManager $manager)
+    {
+        $tag1 = new Tag();
+        $tag1->setLabel('foo');
+
+        $manager->persist($tag1);
+
+        $tag2 = new Tag();
+        $tag2->setLabel('bar');
+
+        $manager->persist($tag2);
+
+        $tag3 = new Tag();
+        $tag3->setLabel('baz');
+
+        $manager->persist($tag3);
+
+        $manager->flush();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function getOrder()
+    {
+        return 30;
+    }
+}
index 310175635763cbe119e5c2ebf601978806c4be7f..963f32b1ef20df88b334a3c6a658c55ea926d97e 100644 (file)
@@ -58,6 +58,6 @@ class Tag
      */
     public function getLabel()
     {
-        return $this->value;
+        return $this->label;
     }
 }
index 4164e5161fb49defb23113b875920587a11d2543..0ffe7fe626311d83da7754be64cb3576126810e3 100644 (file)
@@ -150,4 +150,33 @@ class WallabagRestControllerTest extends WallabagTestCase
 
         $this->assertEquals(404, $client->getResponse()->getStatusCode());
     }
+
+    public function testGetOneTag()
+    {
+        $client = $this->createClient();
+        $client->request('GET', '/api/salts/admin.json');
+        $salt = json_decode($client->getResponse()->getContent());
+
+        $headers = $this->generateHeaders('admin', 'test', $salt[0]);
+
+        $tag = $client->getContainer()
+            ->get('doctrine.orm.entity_manager')
+            ->getRepository('WallabagCoreBundle:Tag')
+            ->findOneByLabel('foo');
+
+        if (!$tag) {
+            $this->markTestSkipped('No content found in db.');
+        }
+
+        $client->request('GET', '/api/tags/'.$tag->getLabel().'.json', array(), array(), $headers);
+
+        $this->assertEquals(json_encode($tag), $client->getResponse()->getContent());
+
+        $this->assertTrue(
+            $client->getResponse()->headers->contains(
+                'Content-Type',
+                'application/json'
+            )
+        );
+    }
 }