aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNicolas LÅ“uillet <nicolas@loeuillet.org>2015-02-20 20:29:33 +0100
committerNicolas LÅ“uillet <nicolas@loeuillet.org>2015-03-06 20:50:30 +0100
commit0a018fe03980b35c9f7aca838e67a8efa43b7f2d (patch)
treed531ff5c3051871dd5315e26e50ec45c7da2b16b
parent6d37a7e6c11666c2c220c9eb358a877f15bcfa0e (diff)
downloadwallabag-0a018fe03980b35c9f7aca838e67a8efa43b7f2d.tar.gz
wallabag-0a018fe03980b35c9f7aca838e67a8efa43b7f2d.tar.zst
wallabag-0a018fe03980b35c9f7aca838e67a8efa43b7f2d.zip
add relation between entry and tag
-rw-r--r--src/Wallabag/CoreBundle/Controller/WallabagRestController.php3
-rw-r--r--src/Wallabag/CoreBundle/Entity/Entry.php27
-rw-r--r--src/Wallabag/CoreBundle/Entity/Tag.php10
-rw-r--r--src/Wallabag/CoreBundle/Entity/TagsEntries.php93
-rw-r--r--src/Wallabag/CoreBundle/Repository/TagRepository.php13
-rw-r--r--src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php29
6 files changed, 53 insertions, 122 deletions
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
196 */ 196 */
197 public function getEntriesTagsAction(Entry $entry) 197 public function getEntriesTagsAction(Entry $entry)
198 { 198 {
199 $json = $this->get('serializer')->serialize($entry, 'json');
200
201 return new Response($json, 200, array('application/json'));
199 } 202 }
200 203
201 /** 204 /**
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 @@
2 2
3namespace Wallabag\CoreBundle\Entity; 3namespace Wallabag\CoreBundle\Entity;
4 4
5use Doctrine\Common\Collections\ArrayCollection;
5use Doctrine\ORM\Mapping as ORM; 6use Doctrine\ORM\Mapping as ORM;
6use Symfony\Component\Validator\Constraints as Assert; 7use Symfony\Component\Validator\Constraints as Assert;
7use Hateoas\Configuration\Annotation as Hateoas; 8use Hateoas\Configuration\Annotation as Hateoas;
@@ -118,12 +119,22 @@ class Entry
118 */ 119 */
119 private $user; 120 private $user;
120 121
122 /**
123 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist", "merge"})
124 * @ORM\JoinTable(name="tags_entries",
125 * joinColumns={@ORM\JoinColumn(name="entry_id", referencedColumnName="id")},
126 * inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
127 * )
128 */
129 private $tags;
130
121 /* 131 /*
122 * @param User $user 132 * @param User $user
123 */ 133 */
124 public function __construct(User $user) 134 public function __construct(User $user)
125 { 135 {
126 $this->user = $user; 136 $this->user = $user;
137 $this->tags = new ArrayCollection();
127 } 138 }
128 139
129 /** 140 /**
@@ -381,4 +392,20 @@ class Entry
381 { 392 {
382 $this->isPublic = $isPublic; 393 $this->isPublic = $isPublic;
383 } 394 }
395
396 /**
397 * @return ArrayCollection<Tag>
398 */
399 public function getTags()
400 {
401 return $this->tags;
402 }
403
404 /**
405 * @param Tag $tag
406 */
407 public function addTag(Tag $tag)
408 {
409 $this->tags[] = $tag;
410 }
384} 411}
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;
4 4
5use Doctrine\ORM\Mapping as ORM; 5use Doctrine\ORM\Mapping as ORM;
6use JMS\Serializer\Annotation\XmlRoot; 6use JMS\Serializer\Annotation\XmlRoot;
7use JMS\Serializer\Annotation\ExclusionPolicy;
8use JMS\Serializer\Annotation\Expose;
7 9
8/** 10/**
9 * Tag 11 * Tag
@@ -11,12 +13,14 @@ use JMS\Serializer\Annotation\XmlRoot;
11 * @XmlRoot("tag") 13 * @XmlRoot("tag")
12 * @ORM\Table(name="tag") 14 * @ORM\Table(name="tag")
13 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository") 15 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository")
16 * @ExclusionPolicy("all")
14 */ 17 */
15class Tag 18class Tag
16{ 19{
17 /** 20 /**
18 * @var integer 21 * @var integer
19 * 22 *
23 * @Expose
20 * @ORM\Column(name="id", type="integer") 24 * @ORM\Column(name="id", type="integer")
21 * @ORM\Id 25 * @ORM\Id
22 * @ORM\GeneratedValue(strategy="AUTO") 26 * @ORM\GeneratedValue(strategy="AUTO")
@@ -26,11 +30,17 @@ class Tag
26 /** 30 /**
27 * @var string 31 * @var string
28 * 32 *
33 * @Expose
29 * @ORM\Column(name="label", type="text") 34 * @ORM\Column(name="label", type="text")
30 */ 35 */
31 private $label; 36 private $label;
32 37
33 /** 38 /**
39 * @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist", "merge"})
40 */
41 private $entries;
42
43 /**
34 * Get id 44 * Get id
35 * 45 *
36 * @return integer 46 * @return integer
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 @@
1<?php
2
3namespace Wallabag\CoreBundle\Entity;
4
5use Doctrine\ORM\Mapping as ORM;
6
7/**
8 * TagsEntries
9 *
10 * @ORM\Table(name="tags_entries")
11 * @ORM\Entity
12 */
13class TagsEntries
14{
15 /**
16 * @var integer
17 *
18 * @ORM\Column(name="id", type="integer")
19 * @ORM\Id
20 * @ORM\GeneratedValue(strategy="AUTO")
21 */
22 private $id;
23
24 /**
25 * @var integer
26 *
27 * @ORM\Column(name="entry_id", type="integer")
28 */
29 private $entryId;
30
31 /**
32 * @var integer
33 *
34 * @ORM\Column(name="tag_id", type="integer")
35 */
36 private $tagId;
37
38 /**
39 * Get id
40 *
41 * @return integer
42 */
43 public function getId()
44 {
45 return $this->id;
46 }
47
48 /**
49 * Set entryId
50 *
51 * @param integer $entryId
52 * @return TagsEntries
53 */
54 public function setEntryId($entryId)
55 {
56 $this->entryId = $entryId;
57
58 return $this;
59 }
60
61 /**
62 * Get entryId
63 *
64 * @return integer
65 */
66 public function getEntryId()
67 {
68 return $this->entryId;
69 }
70
71 /**
72 * Set tagId
73 *
74 * @param integer $tagId
75 * @return TagsEntries
76 */
77 public function setTagId($tagId)
78 {
79 $this->tagId = $tagId;
80
81 return $this;
82 }
83
84 /**
85 * Get tagId
86 *
87 * @return integer
88 */
89 public function getTagId()
90 {
91 return $this->tagId;
92 }
93}
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;
6 6
7class TagRepository extends EntityRepository 7class TagRepository extends EntityRepository
8{ 8{
9 public function findByEntries($entryId)
10 {
11 $qb = $this->createQueryBuilder('t')
12 ->select('t')
13 ->leftJoin('t.id', 'u')
14 ->where('e.isStarred = true')
15 ->andWhere('u.id =:userId')->setParameter('userId', $userId)
16 ->orderBy('e.createdAt', 'desc')
17 ->getQuery();
9 18
19 $paginator = new Paginator($qb);
20
21 return $paginator;
22 }
10} 23}
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
150 150
151 $this->assertEquals(404, $client->getResponse()->getStatusCode()); 151 $this->assertEquals(404, $client->getResponse()->getStatusCode());
152 } 152 }
153
154 public function testGetOneTag()
155 {
156 $client = $this->createClient();
157 $client->request('GET', '/api/salts/admin.json');
158 $salt = json_decode($client->getResponse()->getContent());
159
160 $headers = $this->generateHeaders('admin', 'test', $salt[0]);
161
162 $tag = $client->getContainer()
163 ->get('doctrine.orm.entity_manager')
164 ->getRepository('WallabagCoreBundle:Tag')
165 ->findOneByLabel('foo');
166
167 if (!$tag) {
168 $this->markTestSkipped('No content found in db.');
169 }
170
171 $client->request('GET', '/api/tags/'.$tag->getLabel().'.json', array(), array(), $headers);
172
173 $this->assertEquals(json_encode($tag), $client->getResponse()->getContent());
174
175 $this->assertTrue(
176 $client->getResponse()->headers->contains(
177 'Content-Type',
178 'application/json'
179 )
180 );
181 }
182} 153}