]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
add relation between entry and tag
authorNicolas Lœuillet <nicolas@loeuillet.org>
Fri, 20 Feb 2015 19:29:33 +0000 (20:29 +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/Entity/Entry.php
src/Wallabag/CoreBundle/Entity/Tag.php
src/Wallabag/CoreBundle/Entity/TagsEntries.php [deleted file]
src/Wallabag/CoreBundle/Repository/TagRepository.php
src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php

index 2384325f49d5eb57ab965aee11cc52dae5d5ea80..b895b67cd913b881bbd765f06918d54972576c64 100644 (file)
@@ -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'));
     }
 
     /**
index e47848b61d9bd93bf9105af7d63a23ac43d6ca49..e0d1b839c6a43d0a6100238d9e057b3797d08514 100644 (file)
@@ -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<Tag>
+     */
+    public function getTags()
+    {
+        return $this->tags;
+    }
+
+    /**
+     * @param Tag $tag
+     */
+    public function addTag(Tag $tag)
+    {
+        $this->tags[] = $tag;
+    }
 }
index 0d7f8c2b26201850f1cf133e51ad510a1dc1f57d..1cdf4df027cc772dcbedf8f8923276ded593a77a 100644 (file)
@@ -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 (file)
index 2282638..0000000
+++ /dev/null
@@ -1,93 +0,0 @@
-<?php
-
-namespace Wallabag\CoreBundle\Entity;
-
-use Doctrine\ORM\Mapping as ORM;
-
-/**
- * TagsEntries
- *
- * @ORM\Table(name="tags_entries")
- * @ORM\Entity
- */
-class TagsEntries
-{
-    /**
-     * @var integer
-     *
-     * @ORM\Column(name="id", type="integer")
-     * @ORM\Id
-     * @ORM\GeneratedValue(strategy="AUTO")
-     */
-    private $id;
-
-    /**
-     * @var integer
-     *
-     * @ORM\Column(name="entry_id", type="integer")
-     */
-    private $entryId;
-
-    /**
-     * @var integer
-     *
-     * @ORM\Column(name="tag_id", type="integer")
-     */
-    private $tagId;
-
-    /**
-     * Get id
-     *
-     * @return integer
-     */
-    public function getId()
-    {
-        return $this->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;
-    }
-}
index 903a99cda3804bddfa61751cdf611a3c6a209e75..0f362f79ebb0efdc37d4a9f2599e4100e24723e6 100644 (file)
@@ -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;
+    }
 }
index 0ffe7fe626311d83da7754be64cb3576126810e3..4164e5161fb49defb23113b875920587a11d2543 100644 (file)
@@ -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'
-            )
-        );
-    }
 }