From 0a018fe03980b35c9f7aca838e67a8efa43b7f2d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 20 Feb 2015 20:29:33 +0100 Subject: [PATCH] add relation between entry and tag --- .../Controller/WallabagRestController.php | 3 + src/Wallabag/CoreBundle/Entity/Entry.php | 27 ++++++ src/Wallabag/CoreBundle/Entity/Tag.php | 10 ++ .../CoreBundle/Entity/TagsEntries.php | 93 ------------------- .../CoreBundle/Repository/TagRepository.php | 13 +++ .../Controller/WallabagRestControllerTest.php | 29 ------ 6 files changed, 53 insertions(+), 122 deletions(-) delete mode 100644 src/Wallabag/CoreBundle/Entity/TagsEntries.php diff --git a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php b/src/Wallabag/CoreBundle/Controller/WallabagRestController.php index 2384325f..b895b67c 100644 --- a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/CoreBundle/Controller/WallabagRestController.php @@ -196,6 +196,9 @@ class WallabagRestController extends Controller */ public function getEntriesTagsAction(Entry $entry) { + $json = $this->get('serializer')->serialize($entry, 'json'); + + return new Response($json, 200, array('application/json')); } /** diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index e47848b6..e0d1b839 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php @@ -2,6 +2,7 @@ namespace Wallabag\CoreBundle\Entity; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Hateoas\Configuration\Annotation as Hateoas; @@ -118,12 +119,22 @@ class Entry */ private $user; + /** + * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist", "merge"}) + * @ORM\JoinTable(name="tags_entries", + * joinColumns={@ORM\JoinColumn(name="entry_id", referencedColumnName="id")}, + * inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")} + * ) + */ + private $tags; + /* * @param User $user */ public function __construct(User $user) { $this->user = $user; + $this->tags = new ArrayCollection(); } /** @@ -381,4 +392,20 @@ class Entry { $this->isPublic = $isPublic; } + + /** + * @return ArrayCollection + */ + public function getTags() + { + return $this->tags; + } + + /** + * @param Tag $tag + */ + public function addTag(Tag $tag) + { + $this->tags[] = $tag; + } } diff --git a/src/Wallabag/CoreBundle/Entity/Tag.php b/src/Wallabag/CoreBundle/Entity/Tag.php index 0d7f8c2b..1cdf4df0 100644 --- a/src/Wallabag/CoreBundle/Entity/Tag.php +++ b/src/Wallabag/CoreBundle/Entity/Tag.php @@ -4,6 +4,8 @@ namespace Wallabag\CoreBundle\Entity; use Doctrine\ORM\Mapping as ORM; use JMS\Serializer\Annotation\XmlRoot; +use JMS\Serializer\Annotation\ExclusionPolicy; +use JMS\Serializer\Annotation\Expose; /** * Tag @@ -11,12 +13,14 @@ use JMS\Serializer\Annotation\XmlRoot; * @XmlRoot("tag") * @ORM\Table(name="tag") * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository") + * @ExclusionPolicy("all") */ class Tag { /** * @var integer * + * @Expose * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") @@ -26,10 +30,16 @@ class Tag /** * @var string * + * @Expose * @ORM\Column(name="label", type="text") */ private $label; + /** + * @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist", "merge"}) + */ + private $entries; + /** * Get id * diff --git a/src/Wallabag/CoreBundle/Entity/TagsEntries.php b/src/Wallabag/CoreBundle/Entity/TagsEntries.php deleted file mode 100644 index 22826387..00000000 --- a/src/Wallabag/CoreBundle/Entity/TagsEntries.php +++ /dev/null @@ -1,93 +0,0 @@ -id; - } - - /** - * Set entryId - * - * @param integer $entryId - * @return TagsEntries - */ - public function setEntryId($entryId) - { - $this->entryId = $entryId; - - return $this; - } - - /** - * Get entryId - * - * @return integer - */ - public function getEntryId() - { - return $this->entryId; - } - - /** - * Set tagId - * - * @param integer $tagId - * @return TagsEntries - */ - public function setTagId($tagId) - { - $this->tagId = $tagId; - - return $this; - } - - /** - * Get tagId - * - * @return integer - */ - public function getTagId() - { - return $this->tagId; - } -} diff --git a/src/Wallabag/CoreBundle/Repository/TagRepository.php b/src/Wallabag/CoreBundle/Repository/TagRepository.php index 903a99cd..0f362f79 100644 --- a/src/Wallabag/CoreBundle/Repository/TagRepository.php +++ b/src/Wallabag/CoreBundle/Repository/TagRepository.php @@ -6,5 +6,18 @@ use Doctrine\ORM\EntityRepository; class TagRepository extends EntityRepository { + public function findByEntries($entryId) + { + $qb = $this->createQueryBuilder('t') + ->select('t') + ->leftJoin('t.id', 'u') + ->where('e.isStarred = true') + ->andWhere('u.id =:userId')->setParameter('userId', $userId) + ->orderBy('e.createdAt', 'desc') + ->getQuery(); + $paginator = new Paginator($qb); + + return $paginator; + } } diff --git a/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php index 0ffe7fe6..4164e516 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php @@ -150,33 +150,4 @@ 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