aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorNicolas Lœuillet <nicolas@loeuillet.org>2015-03-06 21:48:33 +0100
committerNicolas Lœuillet <nicolas@loeuillet.org>2015-03-06 21:48:33 +0100
commitf37d1427a1b75f9d7a2e273b2e9fb0e895a769ab (patch)
treee6ea5fb4786e843b970b14d5a96da8c32548a315 /src
parent73b774438395d0ed74d0fd863194d2ebfb68d9ce (diff)
parent6e22bd737b2e543c85487864a9a25b0f2d5cc3f2 (diff)
downloadwallabag-f37d1427a1b75f9d7a2e273b2e9fb0e895a769ab.tar.gz
wallabag-f37d1427a1b75f9d7a2e273b2e9fb0e895a769ab.tar.zst
wallabag-f37d1427a1b75f9d7a2e273b2e9fb0e895a769ab.zip
Merge pull request #1110 from wallabag/v2-api-hypermedia
[WIP] API : hypermedia & tags
Diffstat (limited to 'src')
-rw-r--r--src/Wallabag/CoreBundle/Controller/EntryController.php5
-rw-r--r--src/Wallabag/CoreBundle/Controller/WallabagRestController.php170
-rw-r--r--src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php26
-rw-r--r--src/Wallabag/CoreBundle/Entity/Entry.php59
-rw-r--r--src/Wallabag/CoreBundle/Entity/Tag.php40
-rw-r--r--src/Wallabag/CoreBundle/Entity/TagsEntries.php93
-rw-r--r--src/Wallabag/CoreBundle/Entity/User.php34
-rw-r--r--src/Wallabag/CoreBundle/Repository/EntryRepository.php38
-rw-r--r--src/Wallabag/CoreBundle/Repository/TagRepository.php9
-rw-r--r--src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php9
-rw-r--r--src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php81
11 files changed, 373 insertions, 191 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php
index 81ab7788..8a8f3cd7 100644
--- a/src/Wallabag/CoreBundle/Controller/EntryController.php
+++ b/src/Wallabag/CoreBundle/Controller/EntryController.php
@@ -192,8 +192,9 @@ class EntryController extends Controller
192 { 192 {
193 $this->checkUserAction($entry); 193 $this->checkUserAction($entry);
194 194
195 $entry->setDeleted(1); 195 $em = $this->getDoctrine()->getManager();
196 $this->getDoctrine()->getManager()->flush(); 196 $em->remove($entry);
197 $em->flush();
197 198
198 $this->get('session')->getFlashBag()->add( 199 $this->get('session')->getFlashBag()->add(
199 'notice', 200 'notice',
diff --git a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php b/src/Wallabag/CoreBundle/Controller/WallabagRestController.php
index e9cd8c93..14f42c48 100644
--- a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php
+++ b/src/Wallabag/CoreBundle/Controller/WallabagRestController.php
@@ -5,14 +5,41 @@ namespace Wallabag\CoreBundle\Controller;
5use Nelmio\ApiDocBundle\Annotation\ApiDoc; 5use Nelmio\ApiDocBundle\Annotation\ApiDoc;
6use Symfony\Bundle\FrameworkBundle\Controller\Controller; 6use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7use Symfony\Component\HttpFoundation\Request; 7use Symfony\Component\HttpFoundation\Request;
8use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 8use Symfony\Component\HttpFoundation\Response;
9use Wallabag\CoreBundle\Entity\Entry; 9use Wallabag\CoreBundle\Entity\Entry;
10use Wallabag\CoreBundle\Entity\Tag; 10use Wallabag\CoreBundle\Entity\Tag;
11use Wallabag\CoreBundle\Service\Extractor; 11use Wallabag\CoreBundle\Service\Extractor;
12use Hateoas\Configuration\Route;
13use Hateoas\Representation\Factory\PagerfantaFactory;
12 14
13class WallabagRestController extends Controller 15class WallabagRestController extends Controller
14{ 16{
15 /** 17 /**
18 * @param Entry $entry
19 * @param string $tags
20 */
21 private function assignTagsToEntry(Entry $entry, $tags)
22 {
23 foreach (explode(',', $tags) as $label) {
24 $label = trim($label);
25 $tagEntity = $this
26 ->getDoctrine()
27 ->getRepository('WallabagCoreBundle:Tag')
28 ->findOneByLabel($label);
29
30 if (is_null($tagEntity)) {
31 $tagEntity = new Tag($this->getUser());
32 $tagEntity->setLabel($label);
33 }
34
35 // only add the tag on the entry if the relation doesn't exist
36 if (!$entry->getTags()->contains($tagEntity)) {
37 $entry->addTag($tagEntity);
38 }
39 }
40 }
41
42 /**
16 * Retrieve salt for a giver user. 43 * Retrieve salt for a giver user.
17 * 44 *
18 * @ApiDoc( 45 * @ApiDoc(
@@ -42,7 +69,6 @@ class WallabagRestController extends Controller
42 * parameters={ 69 * parameters={
43 * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by archived status."}, 70 * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by archived status."},
44 * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by starred status."}, 71 * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by starred status."},
45 * {"name"="delete", "dataType"="boolean", "required"=false, "format"="true or false, default '0'", "description"="filter by deleted status."},
46 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."}, 72 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."},
47 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."}, 73 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
48 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."}, 74 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
@@ -56,23 +82,33 @@ class WallabagRestController extends Controller
56 { 82 {
57 $isArchived = $request->query->get('archive'); 83 $isArchived = $request->query->get('archive');
58 $isStarred = $request->query->get('star'); 84 $isStarred = $request->query->get('star');
59 $isDeleted = $request->query->get('delete', 0);
60 $sort = $request->query->get('sort', 'created'); 85 $sort = $request->query->get('sort', 'created');
61 $order = $request->query->get('order', 'desc'); 86 $order = $request->query->get('order', 'desc');
62 $page = $request->query->get('page', 1); 87 $page = (int) $request->query->get('page', 1);
63 $perPage = $request->query->get('perPage', 30); 88 $perPage = (int) $request->query->get('perPage', 30);
64 $tags = $request->query->get('tags', array()); 89 $tags = $request->query->get('tags', array());
65 90
66 $entries = $this 91 $pager = $this
67 ->getDoctrine() 92 ->getDoctrine()
68 ->getRepository('WallabagCoreBundle:Entry') 93 ->getRepository('WallabagCoreBundle:Entry')
69 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $isDeleted, $sort, $order); 94 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order);
70 95
71 if (!($entries)) { 96 if (0 === $pager->getNbResults()) {
72 throw $this->createNotFoundException(); 97 throw $this->createNotFoundException();
73 } 98 }
74 99
75 return $entries; 100 $pager->setCurrentPage($page);
101 $pager->setMaxPerPage($perPage);
102
103 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
104 $paginatedCollection = $pagerfantaFactory->createRepresentation(
105 $pager,
106 new Route('api_get_entries', [], $absolute = true)
107 );
108
109 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
110
111 return new Response($json, 200, array('application/json'));
76 } 112 }
77 113
78 /** 114 /**
@@ -87,7 +123,13 @@ class WallabagRestController extends Controller
87 */ 123 */
88 public function getEntryAction(Entry $entry) 124 public function getEntryAction(Entry $entry)
89 { 125 {
90 return $entry; 126 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
127 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId());
128 }
129
130 $json = $this->get('serializer')->serialize($entry, 'json');
131
132 return new Response($json, 200, array('application/json'));
91 } 133 }
92 134
93 /** 135 /**
@@ -104,7 +146,6 @@ class WallabagRestController extends Controller
104 */ 146 */
105 public function postEntriesAction(Request $request) 147 public function postEntriesAction(Request $request)
106 { 148 {
107 //TODO gérer si on passe les tags
108 $url = $request->request->get('url'); 149 $url = $request->request->get('url');
109 150
110 $content = Extractor::extract($url); 151 $content = Extractor::extract($url);
@@ -112,11 +153,19 @@ class WallabagRestController extends Controller
112 $entry->setUrl($url); 153 $entry->setUrl($url);
113 $entry->setTitle($request->request->get('title') ?: $content->getTitle()); 154 $entry->setTitle($request->request->get('title') ?: $content->getTitle());
114 $entry->setContent($content->getBody()); 155 $entry->setContent($content->getBody());
156
157 $tags = $request->request->get('tags', '');
158 if (!empty($tags)) {
159 $this->assignTagsToEntry($entry, $tags);
160 }
161
115 $em = $this->getDoctrine()->getManager(); 162 $em = $this->getDoctrine()->getManager();
116 $em->persist($entry); 163 $em->persist($entry);
117 $em->flush(); 164 $em->flush();
118 165
119 return $entry; 166 $json = $this->get('serializer')->serialize($entry, 'json');
167
168 return new Response($json, 200, array('application/json'));
120 } 169 }
121 170
122 /** 171 /**
@@ -131,17 +180,18 @@ class WallabagRestController extends Controller
131 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, 180 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
132 * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false", "description"="archived the entry."}, 181 * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false", "description"="archived the entry."},
133 * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false", "description"="starred the entry."}, 182 * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false", "description"="starred the entry."},
134 * {"name"="delete", "dataType"="boolean", "required"=false, "format"="true or false", "description"="flag as deleted. Default false. In case that you don't want to *really* remove it.."}, 183 * }
135 * }
136 * ) 184 * )
137 * @return Entry 185 * @return Entry
138 */ 186 */
139 public function patchEntriesAction(Entry $entry, Request $request) 187 public function patchEntriesAction(Entry $entry, Request $request)
140 { 188 {
189 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
190 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId());
191 }
192
141 $title = $request->request->get("title"); 193 $title = $request->request->get("title");
142 $tags = $request->request->get("tags", array());
143 $isArchived = $request->request->get("archive"); 194 $isArchived = $request->request->get("archive");
144 $isDeleted = $request->request->get("delete");
145 $isStarred = $request->request->get("star"); 195 $isStarred = $request->request->get("star");
146 196
147 if (!is_null($title)) { 197 if (!is_null($title)) {
@@ -152,18 +202,21 @@ class WallabagRestController extends Controller
152 $entry->setArchived($isArchived); 202 $entry->setArchived($isArchived);
153 } 203 }
154 204
155 if (!is_null($isDeleted)) {
156 $entry->setDeleted($isDeleted);
157 }
158
159 if (!is_null($isStarred)) { 205 if (!is_null($isStarred)) {
160 $entry->setStarred($isStarred); 206 $entry->setStarred($isStarred);
161 } 207 }
162 208
209 $tags = $request->request->get('tags', '');
210 if (!empty($tags)) {
211 $this->assignTagsToEntry($entry, $tags);
212 }
213
163 $em = $this->getDoctrine()->getManager(); 214 $em = $this->getDoctrine()->getManager();
164 $em->flush(); 215 $em->flush();
165 216
166 return $entry; 217 $json = $this->get('serializer')->serialize($entry, 'json');
218
219 return new Response($json, 200, array('application/json'));
167 } 220 }
168 221
169 /** 222 /**
@@ -178,15 +231,17 @@ class WallabagRestController extends Controller
178 */ 231 */
179 public function deleteEntriesAction(Entry $entry) 232 public function deleteEntriesAction(Entry $entry)
180 { 233 {
181 if ($entry->isDeleted()) { 234 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
182 throw new NotFoundHttpException('This entry is already deleted'); 235 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId());
183 } 236 }
184 237
185 $em = $this->getDoctrine()->getManager(); 238 $em = $this->getDoctrine()->getManager();
186 $entry->setDeleted(1); 239 $em->remove($entry);
187 $em->flush(); 240 $em->flush();
188 241
189 return $entry; 242 $json = $this->get('serializer')->serialize($entry, 'json');
243
244 return new Response($json, 200, array('application/json'));
190 } 245 }
191 246
192 /** 247 /**
@@ -200,6 +255,13 @@ class WallabagRestController extends Controller
200 */ 255 */
201 public function getEntriesTagsAction(Entry $entry) 256 public function getEntriesTagsAction(Entry $entry)
202 { 257 {
258 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
259 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId());
260 }
261
262 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
263
264 return new Response($json, 200, array('application/json'));
203 } 265 }
204 266
205 /** 267 /**
@@ -214,8 +276,24 @@ class WallabagRestController extends Controller
214 * } 276 * }
215 * ) 277 * )
216 */ 278 */
217 public function postEntriesTagsAction(Entry $entry) 279 public function postEntriesTagsAction(Request $request, Entry $entry)
218 { 280 {
281 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
282 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId());
283 }
284
285 $tags = $request->request->get('tags', '');
286 if (!empty($tags)) {
287 $this->assignTagsToEntry($entry, $tags);
288 }
289
290 $em = $this->getDoctrine()->getManager();
291 $em->persist($entry);
292 $em->flush();
293
294 $json = $this->get('serializer')->serialize($entry, 'json');
295
296 return new Response($json, 200, array('application/json'));
219 } 297 }
220 298
221 /** 299 /**
@@ -230,29 +308,30 @@ class WallabagRestController extends Controller
230 */ 308 */
231 public function deleteEntriesTagsAction(Entry $entry, Tag $tag) 309 public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
232 { 310 {
311 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
312 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId());
313 }
314
315 $entry->removeTag($tag);
316 $em = $this->getDoctrine()->getManager();
317 $em->persist($entry);
318 $em->flush();
319
320 $json = $this->get('serializer')->serialize($entry, 'json');
321
322 return new Response($json, 200, array('application/json'));
233 } 323 }
234 324
235 /** 325 /**
236 * Retrieve all tags 326 * Retrieve all tags
237 * 327 *
238 * @ApiDoc( 328 * @ApiDoc()
239 * )
240 */ 329 */
241 public function getTagsAction() 330 public function getTagsAction()
242 { 331 {
243 } 332 $json = $this->get('serializer')->serialize($this->getUser()->getTags(), 'json');
244 333
245 /** 334 return new Response($json, 200, array('application/json'));
246 * Retrieve a single tag
247 *
248 * @ApiDoc(
249 * requirements={
250 * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag"}
251 * }
252 * )
253 */
254 public function getTagAction(Tag $tag)
255 {
256 } 335 }
257 336
258 /** 337 /**
@@ -266,5 +345,16 @@ class WallabagRestController extends Controller
266 */ 345 */
267 public function deleteTagAction(Tag $tag) 346 public function deleteTagAction(Tag $tag)
268 { 347 {
348 if ($tag->getUser()->getId() != $this->getUser()->getId()) {
349 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$tag->getUser()->getId().', logged user id: '.$this->getUser()->getId());
350 }
351
352 $em = $this->getDoctrine()->getManager();
353 $em->remove($tag);
354 $em->flush();
355
356 $json = $this->get('serializer')->serialize($tag, 'json');
357
358 return new Response($json, 200, array('application/json'));
269 } 359 }
270} 360}
diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php
index 3be323ed..ce12ec5d 100644
--- a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php
+++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php
@@ -6,6 +6,7 @@ use Doctrine\Common\DataFixtures\AbstractFixture;
6use Doctrine\Common\DataFixtures\OrderedFixtureInterface; 6use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
7use Doctrine\Common\Persistence\ObjectManager; 7use Doctrine\Common\Persistence\ObjectManager;
8use Wallabag\CoreBundle\Entity\Entry; 8use Wallabag\CoreBundle\Entity\Entry;
9use Wallabag\CoreBundle\Entity\Tag;
9 10
10class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface 11class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface
11{ 12{
@@ -37,10 +38,35 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface
37 $entry3->setTitle('test title entry3'); 38 $entry3->setTitle('test title entry3');
38 $entry3->setContent('This is my content /o/'); 39 $entry3->setContent('This is my content /o/');
39 40
41 $tag1 = new Tag($this->getReference('bob-user'));
42 $tag1->setLabel("foo");
43 $tag2 = new Tag($this->getReference('bob-user'));
44 $tag2->setLabel("bar");
45
46 $entry3->addTag($tag1);
47 $entry3->addTag($tag2);
48
40 $manager->persist($entry3); 49 $manager->persist($entry3);
41 50
42 $this->addReference('entry3', $entry3); 51 $this->addReference('entry3', $entry3);
43 52
53 $entry4 = new Entry($this->getReference('admin-user'));
54 $entry4->setUrl('http://0.0.0.0');
55 $entry4->setTitle('test title entry4');
56 $entry4->setContent('This is my content /o/');
57
58 $tag1 = new Tag($this->getReference('admin-user'));
59 $tag1->setLabel("foo");
60 $tag2 = new Tag($this->getReference('admin-user'));
61 $tag2->setLabel("bar");
62
63 $entry4->addTag($tag1);
64 $entry4->addTag($tag2);
65
66 $manager->persist($entry4);
67
68 $this->addReference('entry4', $entry4);
69
44 $manager->flush(); 70 $manager->flush();
45 } 71 }
46 72
diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php
index 937213b4..75aeae84 100644
--- a/src/Wallabag/CoreBundle/Entity/Entry.php
+++ b/src/Wallabag/CoreBundle/Entity/Entry.php
@@ -2,19 +2,24 @@
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;
8use Hateoas\Configuration\Annotation as Hateoas;
9use JMS\Serializer\Annotation\XmlRoot;
7 10
8/** 11/**
9 * Entry 12 * Entry
10 * 13 *
14 * @XmlRoot("entry")
11 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository") 15 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository")
12 * @ORM\Table(name="entry") 16 * @ORM\Table(name="entry")
13 * @ORM\HasLifecycleCallbacks() 17 * @ORM\HasLifecycleCallbacks()
14 * 18 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
15 */ 19 */
16class Entry 20class Entry
17{ 21{
22 /** @Serializer\XmlAttribute */
18 /** 23 /**
19 * @var integer 24 * @var integer
20 * 25 *
@@ -54,13 +59,6 @@ class Entry
54 private $isStarred = false; 59 private $isStarred = false;
55 60
56 /** 61 /**
57 * @var boolean
58 *
59 * @ORM\Column(name="is_deleted", type="boolean")
60 */
61 private $isDeleted = false;
62
63 /**
64 * @var string 62 * @var string
65 * 63 *
66 * @ORM\Column(name="content", type="text", nullable=true) 64 * @ORM\Column(name="content", type="text", nullable=true)
@@ -121,12 +119,19 @@ class Entry
121 */ 119 */
122 private $user; 120 private $user;
123 121
122 /**
123 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
124 * @ORM\JoinTable(name="entry_tags")
125 */
126 private $tags;
127
124 /* 128 /*
125 * @param User $user 129 * @param User $user
126 */ 130 */
127 public function __construct(User $user) 131 public function __construct(User $user)
128 { 132 {
129 $this->user = $user; 133 $this->user = $user;
134 $this->tags = new ArrayCollection();
130 } 135 }
131 136
132 /** 137 /**
@@ -279,22 +284,6 @@ class Entry
279 /** 284 /**
280 * @return string 285 * @return string
281 */ 286 */
282 public function isDeleted()
283 {
284 return $this->isDeleted;
285 }
286
287 /**
288 * @param string $isDeleted
289 */
290 public function setDeleted($isDeleted)
291 {
292 $this->isDeleted = $isDeleted;
293 }
294
295 /**
296 * @return string
297 */
298 public function getCreatedAt() 287 public function getCreatedAt()
299 { 288 {
300 return $this->createdAt; 289 return $this->createdAt;
@@ -400,4 +389,26 @@ class Entry
400 { 389 {
401 $this->isPublic = $isPublic; 390 $this->isPublic = $isPublic;
402 } 391 }
392
393 /**
394 * @return ArrayCollection<Tag>
395 */
396 public function getTags()
397 {
398 return $this->tags;
399 }
400
401 /**
402 * @param Tag $tag
403 */
404 public function addTag(Tag $tag)
405 {
406 $this->tags[] = $tag;
407 $tag->addEntry($this);
408 }
409
410 public function removeTag(Tag $tag)
411 {
412 $this->tags->removeElement($tag);
413 }
403} 414}
diff --git a/src/Wallabag/CoreBundle/Entity/Tag.php b/src/Wallabag/CoreBundle/Entity/Tag.php
index 31017563..9ae5867c 100644
--- a/src/Wallabag/CoreBundle/Entity/Tag.php
+++ b/src/Wallabag/CoreBundle/Entity/Tag.php
@@ -3,18 +3,25 @@
3namespace Wallabag\CoreBundle\Entity; 3namespace Wallabag\CoreBundle\Entity;
4 4
5use Doctrine\ORM\Mapping as ORM; 5use Doctrine\ORM\Mapping as ORM;
6use JMS\Serializer\Annotation\XmlRoot;
7use JMS\Serializer\Annotation\ExclusionPolicy;
8use JMS\Serializer\Annotation\Expose;
9use Doctrine\Common\Collections\ArrayCollection;
6 10
7/** 11/**
8 * Tag 12 * Tag
9 * 13 *
14 * @XmlRoot("tag")
10 * @ORM\Table(name="tag") 15 * @ORM\Table(name="tag")
11 * @ORM\Entity 16 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository")
17 * @ExclusionPolicy("all")
12 */ 18 */
13class Tag 19class Tag
14{ 20{
15 /** 21 /**
16 * @var integer 22 * @var integer
17 * 23 *
24 * @Expose
18 * @ORM\Column(name="id", type="integer") 25 * @ORM\Column(name="id", type="integer")
19 * @ORM\Id 26 * @ORM\Id
20 * @ORM\GeneratedValue(strategy="AUTO") 27 * @ORM\GeneratedValue(strategy="AUTO")
@@ -24,11 +31,27 @@ class Tag
24 /** 31 /**
25 * @var string 32 * @var string
26 * 33 *
34 * @Expose
27 * @ORM\Column(name="label", type="text") 35 * @ORM\Column(name="label", type="text")
28 */ 36 */
29 private $label; 37 private $label;
30 38
31 /** 39 /**
40 * @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist"})
41 */
42 private $entries;
43
44 /**
45 * @ORM\ManyToOne(targetEntity="User", inversedBy="tags")
46 */
47 private $user;
48
49 public function __construct(User $user)
50 {
51 $this->user = $user;
52 $this->entries = new ArrayCollection();
53 }
54 /**
32 * Get id 55 * Get id
33 * 56 *
34 * @return integer 57 * @return integer
@@ -58,6 +81,19 @@ class Tag
58 */ 81 */
59 public function getLabel() 82 public function getLabel()
60 { 83 {
61 return $this->value; 84 return $this->label;
85 }
86
87 public function addEntry(Entry $entry)
88 {
89 $this->entries[] = $entry;
90 }
91
92 /**
93 * @return User
94 */
95 public function getUser()
96 {
97 return $this->user;
62 } 98 }
63} 99}
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/Entity/User.php b/src/Wallabag/CoreBundle/Entity/User.php
index ed5cfe53..f05c8760 100644
--- a/src/Wallabag/CoreBundle/Entity/User.php
+++ b/src/Wallabag/CoreBundle/Entity/User.php
@@ -7,6 +7,8 @@ use Doctrine\ORM\Mapping as ORM;
7use Symfony\Component\Security\Core\User\UserInterface; 7use Symfony\Component\Security\Core\User\UserInterface;
8use Symfony\Component\Security\Core\User\AdvancedUserInterface; 8use Symfony\Component\Security\Core\User\AdvancedUserInterface;
9use Symfony\Component\Validator\Constraints as Assert; 9use Symfony\Component\Validator\Constraints as Assert;
10use JMS\Serializer\Annotation\ExclusionPolicy;
11use JMS\Serializer\Annotation\Expose;
10 12
11/** 13/**
12 * User 14 * User
@@ -14,12 +16,14 @@ use Symfony\Component\Validator\Constraints as Assert;
14 * @ORM\Table(name="user") 16 * @ORM\Table(name="user")
15 * @ORM\Entity 17 * @ORM\Entity
16 * @ORM\HasLifecycleCallbacks() 18 * @ORM\HasLifecycleCallbacks()
19 * @ExclusionPolicy("all")
17 */ 20 */
18class User implements AdvancedUserInterface, \Serializable 21class User implements AdvancedUserInterface, \Serializable
19{ 22{
20 /** 23 /**
21 * @var integer 24 * @var integer
22 * 25 *
26 * @Expose
23 * @ORM\Column(name="id", type="integer") 27 * @ORM\Column(name="id", type="integer")
24 * @ORM\Id 28 * @ORM\Id
25 * @ORM\GeneratedValue(strategy="AUTO") 29 * @ORM\GeneratedValue(strategy="AUTO")
@@ -97,10 +101,17 @@ class User implements AdvancedUserInterface, \Serializable
97 */ 101 */
98 private $config; 102 private $config;
99 103
104 /**
105 * @ORM\OneToMany(targetEntity="Tag", mappedBy="user", cascade={"remove"})
106 */
107 private $tags;
108
100 public function __construct() 109 public function __construct()
101 { 110 {
102 $this->salt = md5(uniqid(null, true)); 111 $this->isActive = true;
103 $this->entries = new ArrayCollection(); 112 $this->salt = md5(uniqid(null, true));
113 $this->entries = new ArrayCollection();
114 $this->tags = new ArrayCollection();
104 } 115 }
105 116
106 /** 117 /**
@@ -275,6 +286,25 @@ class User implements AdvancedUserInterface, \Serializable
275 } 286 }
276 287
277 /** 288 /**
289 * @param Entry $entry
290 *
291 * @return User
292 */
293 public function addTag(Tag $tag)
294 {
295 $this->tags[] = $tag;
296
297 return $this;
298 }
299
300 /**
301 * @return ArrayCollection<Tag>
302 */
303 public function getTags()
304 {
305 return $this->tags;
306 }
307 /**
278 * @inheritDoc 308 * @inheritDoc
279 */ 309 */
280 public function eraseCredentials() 310 public function eraseCredentials()
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
index bedc90d2..53e8e2ba 100644
--- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php
+++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
@@ -4,6 +4,8 @@ namespace Wallabag\CoreBundle\Repository;
4 4
5use Doctrine\ORM\EntityRepository; 5use Doctrine\ORM\EntityRepository;
6use Doctrine\ORM\Tools\Pagination\Paginator; 6use Doctrine\ORM\Tools\Pagination\Paginator;
7use Pagerfanta\Adapter\DoctrineORMAdapter;
8use Pagerfanta\Pagerfanta;
7 9
8class EntryRepository extends EntityRepository 10class EntryRepository extends EntityRepository
9{ 11{
@@ -24,7 +26,6 @@ class EntryRepository extends EntityRepository
24 ->leftJoin('e.user', 'u') 26 ->leftJoin('e.user', 'u')
25 ->where('e.isArchived = false') 27 ->where('e.isArchived = false')
26 ->andWhere('u.id =:userId')->setParameter('userId', $userId) 28 ->andWhere('u.id =:userId')->setParameter('userId', $userId)
27 ->andWhere('e.isDeleted=false')
28 ->orderBy('e.createdAt', 'desc') 29 ->orderBy('e.createdAt', 'desc')
29 ->getQuery(); 30 ->getQuery();
30 31
@@ -51,7 +52,6 @@ class EntryRepository extends EntityRepository
51 ->leftJoin('e.user', 'u') 52 ->leftJoin('e.user', 'u')
52 ->where('e.isArchived = true') 53 ->where('e.isArchived = true')
53 ->andWhere('u.id =:userId')->setParameter('userId', $userId) 54 ->andWhere('u.id =:userId')->setParameter('userId', $userId)
54 ->andWhere('e.isDeleted=false')
55 ->orderBy('e.createdAt', 'desc') 55 ->orderBy('e.createdAt', 'desc')
56 ->getQuery(); 56 ->getQuery();
57 57
@@ -78,7 +78,6 @@ class EntryRepository extends EntityRepository
78 ->leftJoin('e.user', 'u') 78 ->leftJoin('e.user', 'u')
79 ->where('e.isStarred = true') 79 ->where('e.isStarred = true')
80 ->andWhere('u.id =:userId')->setParameter('userId', $userId) 80 ->andWhere('u.id =:userId')->setParameter('userId', $userId)
81 ->andWhere('e.isDeleted = false')
82 ->orderBy('e.createdAt', 'desc') 81 ->orderBy('e.createdAt', 'desc')
83 ->getQuery(); 82 ->getQuery();
84 83
@@ -93,17 +92,15 @@ class EntryRepository extends EntityRepository
93 * @param int $userId 92 * @param int $userId
94 * @param bool $isArchived 93 * @param bool $isArchived
95 * @param bool $isStarred 94 * @param bool $isStarred
96 * @param bool $isDeleted
97 * @param string $sort 95 * @param string $sort
98 * @param string $order 96 * @param string $order
99 * 97 *
100 * @return array 98 * @return array
101 */ 99 */
102 public function findEntries($userId, $isArchived = null, $isStarred = null, $isDeleted = null, $sort = 'created', $order = 'ASC') 100 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC')
103 { 101 {
104 $qb = $this->createQueryBuilder('e') 102 $qb = $this->createQueryBuilder('e')
105 ->leftJoin('e.user', 'u') 103 ->where('e.user =:userId')->setParameter('userId', $userId);
106 ->where('u.id =:userId')->setParameter('userId', $userId);
107 104
108 if (null !== $isArchived) { 105 if (null !== $isArchived) {
109 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived); 106 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
@@ -113,18 +110,31 @@ class EntryRepository extends EntityRepository
113 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred); 110 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
114 } 111 }
115 112
116 if (null !== $isDeleted) {
117 $qb->andWhere('e.isDeleted =:isDeleted')->setParameter('isDeleted', (bool) $isDeleted);
118 }
119
120 if ('created' === $sort) { 113 if ('created' === $sort) {
121 $qb->orderBy('e.createdAt', $order); 114 $qb->orderBy('e.createdAt', $order);
122 } elseif ('updated' === $sort) { 115 } elseif ('updated' === $sort) {
123 $qb->orderBy('e.updatedAt', $order); 116 $qb->orderBy('e.updatedAt', $order);
124 } 117 }
125 118
126 return $qb 119 $pagerAdapter = new DoctrineORMAdapter($qb);
127 ->getQuery() 120
128 ->getResult(); 121 return new Pagerfanta($pagerAdapter);
122 }
123
124 /**
125 * Fetch an entry with a tag. Only used for tests.
126 *
127 * @return Entry
128 */
129 public function findOneWithTags($userId)
130 {
131 $qb = $this->createQueryBuilder('e')
132 ->innerJoin('e.tags', 't')
133 ->innerJoin('e.user', 'u')
134 ->addSelect('t', 'u')
135 ->where('e.user=:userId')->setParameter('userId', $userId)
136 ;
137
138 return $qb->getQuery()->getResult();
129 } 139 }
130} 140}
diff --git a/src/Wallabag/CoreBundle/Repository/TagRepository.php b/src/Wallabag/CoreBundle/Repository/TagRepository.php
new file mode 100644
index 00000000..52f319f1
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Repository/TagRepository.php
@@ -0,0 +1,9 @@
1<?php
2
3namespace Wallabag\CoreBundle\Repository;
4
5use Doctrine\ORM\EntityRepository;
6
7class TagRepository extends EntityRepository
8{
9}
diff --git a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php
index 2634141e..99a3a945 100644
--- a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php
+++ b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php
@@ -161,18 +161,15 @@ class EntryControllerTest extends WallabagTestCase
161 $content = $client->getContainer() 161 $content = $client->getContainer()
162 ->get('doctrine.orm.entity_manager') 162 ->get('doctrine.orm.entity_manager')
163 ->getRepository('WallabagCoreBundle:Entry') 163 ->getRepository('WallabagCoreBundle:Entry')
164 ->findOneByIsDeleted(false); 164 ->findOneById(1);
165 165
166 $client->request('GET', '/delete/'.$content->getId()); 166 $client->request('GET', '/delete/'.$content->getId());
167 167
168 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 168 $this->assertEquals(302, $client->getResponse()->getStatusCode());
169 169
170 $res = $client->getContainer() 170 $client->request('GET', '/delete/'.$content->getId());
171 ->get('doctrine.orm.entity_manager')
172 ->getRepository('WallabagCoreBundle:Entry')
173 ->findOneById($content->getId());
174 171
175 $this->assertEquals($res->isDeleted(), true); 172 $this->assertEquals(404, $client->getResponse()->getStatusCode());
176 } 173 }
177 174
178 public function testViewOtherUserEntry() 175 public function testViewOtherUserEntry()
diff --git a/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php
index fcfa8ccf..0eca7cde 100644
--- a/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php
+++ b/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php
@@ -44,10 +44,6 @@ class WallabagRestControllerTest extends WallabagTestCase
44 public function testWithBadHeaders() 44 public function testWithBadHeaders()
45 { 45 {
46 $client = $this->createClient(); 46 $client = $this->createClient();
47 $client->request('GET', '/api/salts/admin.json');
48 $salt = json_decode($client->getResponse()->getContent());
49
50 $headers = $this->generateHeaders('admin', 'mypassword', $salt[0]);
51 47
52 $entry = $client->getContainer() 48 $entry = $client->getContainer()
53 ->get('doctrine.orm.entity_manager') 49 ->get('doctrine.orm.entity_manager')
@@ -130,7 +126,7 @@ class WallabagRestControllerTest extends WallabagTestCase
130 $entry = $client->getContainer() 126 $entry = $client->getContainer()
131 ->get('doctrine.orm.entity_manager') 127 ->get('doctrine.orm.entity_manager')
132 ->getRepository('WallabagCoreBundle:Entry') 128 ->getRepository('WallabagCoreBundle:Entry')
133 ->findOneByIsDeleted(false); 129 ->findOneByUser(1);
134 130
135 if (!$entry) { 131 if (!$entry) {
136 $this->markTestSkipped('No content found in db.'); 132 $this->markTestSkipped('No content found in db.');
@@ -140,10 +136,79 @@ class WallabagRestControllerTest extends WallabagTestCase
140 136
141 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 137 $this->assertEquals(200, $client->getResponse()->getStatusCode());
142 138
143 $res = $client->getContainer() 139 // We'll try to delete this entry again
140 $client->request('GET', '/api/salts/admin.json');
141 $salt = json_decode($client->getResponse()->getContent());
142
143 $headers = $this->generateHeaders('admin', 'mypassword', $salt[0]);
144
145 $client->request('DELETE', '/api/entries/'.$entry->getId().'.json', array(), array(), $headers);
146
147 $this->assertEquals(404, $client->getResponse()->getStatusCode());
148 }
149
150 public function testGetTagsEntry()
151 {
152 $client = $this->createClient();
153 $client->request('GET', '/api/salts/admin.json');
154 $salt = json_decode($client->getResponse()->getContent());
155 $headers = $this->generateHeaders('admin', 'mypassword', $salt[0]);
156
157 $entry = $client->getContainer()
158 ->get('doctrine.orm.entity_manager')
159 ->getRepository('WallabagCoreBundle:Entry')
160 ->findOneWithTags(1);
161
162 $entry = $entry[0];
163
164 if (!$entry) {
165 $this->markTestSkipped('No content found in db.');
166 }
167
168 $tags = array();
169 foreach ($entry->getTags() as $tag) {
170 $tags[] = array('id' => $tag->getId(), 'label' => $tag->getLabel());
171 }
172
173 $client->request('GET', '/api/entries/'.$entry->getId().'/tags', array(), array(), $headers);
174
175 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $client->getResponse()->getContent());
176 }
177
178 public function testPostTagsOnEntry()
179 {
180 $client = $this->createClient();
181 $client->request('GET', '/api/salts/admin.json');
182 $salt = json_decode($client->getResponse()->getContent());
183 $headers = $this->generateHeaders('admin', 'mypassword', $salt[0]);
184
185 $entry = $client->getContainer()
186 ->get('doctrine.orm.entity_manager')
187 ->getRepository('WallabagCoreBundle:Entry')
188 ->findOneByUser(1);
189
190 if (!$entry) {
191 $this->markTestSkipped('No content found in db.');
192 }
193
194 $newTags = 'tag1,tag2,tag3';
195
196 $client->request('POST', '/api/entries/'.$entry->getId().'/tags', array('tags' => $newTags), array(), $headers);
197
198 $this->assertEquals(200, $client->getResponse()->getStatusCode());
199
200 $entryDB = $client->getContainer()
144 ->get('doctrine.orm.entity_manager') 201 ->get('doctrine.orm.entity_manager')
145 ->getRepository('WallabagCoreBundle:Entry') 202 ->getRepository('WallabagCoreBundle:Entry')
146 ->findOneById($entry->getId()); 203 ->find($entry->getId());
147 $this->assertEquals($res->isDeleted(), true); 204
205 $tagsInDB = array();
206 foreach ($entryDB->getTags()->toArray() as $tag) {
207 $tagsInDB[$tag->getId()] = $tag->getLabel();
208 }
209
210 foreach (explode(',', $newTags) as $tag) {
211 $this->assertContains($tag, $tagsInDB);
212 }
148 } 213 }
149} 214}