From 2691cf04384239c546e141af6cc3c22b210dae58 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 20 Feb 2015 16:38:24 +0100 Subject: [PATCH] GET /api/tags/id_tag method --- .../Controller/WallabagRestController.php | 17 ++++++-- .../DataFixtures/ORM/LoadTagData.php | 42 +++++++++++++++++++ src/Wallabag/CoreBundle/Entity/Tag.php | 2 +- .../Controller/WallabagRestControllerTest.php | 29 +++++++++++++ 4 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php diff --git a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php b/src/Wallabag/CoreBundle/Controller/WallabagRestController.php index 276cfe1c..cb68784d 100644 --- a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/CoreBundle/Controller/WallabagRestController.php @@ -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 index 00000000..6b13c2be --- /dev/null +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php @@ -0,0 +1,42 @@ +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; + } +} diff --git a/src/Wallabag/CoreBundle/Entity/Tag.php b/src/Wallabag/CoreBundle/Entity/Tag.php index 31017563..963f32b1 100644 --- a/src/Wallabag/CoreBundle/Entity/Tag.php +++ b/src/Wallabag/CoreBundle/Entity/Tag.php @@ -58,6 +58,6 @@ class Tag */ public function getLabel() { - return $this->value; + return $this->label; } } diff --git a/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php index 4164e516..0ffe7fe6 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php @@ -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' + ) + ); + } } -- 2.41.0