aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ApiBundle/Controller
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller')
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php196
1 files changed, 94 insertions, 102 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index 7c3e778e..dbff6065 100644
--- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -5,7 +5,7 @@ namespace Wallabag\ApiBundle\Controller;
5use Hateoas\Configuration\Route; 5use Hateoas\Configuration\Route;
6use Hateoas\Representation\Factory\PagerfantaFactory; 6use Hateoas\Representation\Factory\PagerfantaFactory;
7use Nelmio\ApiDocBundle\Annotation\ApiDoc; 7use Nelmio\ApiDocBundle\Annotation\ApiDoc;
8use Symfony\Component\Config\Definition\Exception\Exception; 8use Symfony\Component\HttpKernel\Exception\HttpException;
9use Symfony\Component\HttpFoundation\Request; 9use Symfony\Component\HttpFoundation\Request;
10use Symfony\Component\HttpFoundation\JsonResponse; 10use Symfony\Component\HttpFoundation\JsonResponse;
11use Symfony\Component\Routing\Generator\UrlGeneratorInterface; 11use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@@ -45,9 +45,7 @@ class EntryRestController extends WallabagRestController
45 $results[$url] = $res instanceof Entry ? $res->getId() : false; 45 $results[$url] = $res instanceof Entry ? $res->getId() : false;
46 } 46 }
47 47
48 $json = $this->get('serializer')->serialize($results, 'json'); 48 return $this->sendResponse($results);
49
50 return (new JsonResponse())->setJson($json);
51 } 49 }
52 50
53 // let's see if it is a simple url? 51 // let's see if it is a simple url?
@@ -63,9 +61,7 @@ class EntryRestController extends WallabagRestController
63 61
64 $exists = $res instanceof Entry ? $res->getId() : false; 62 $exists = $res instanceof Entry ? $res->getId() : false;
65 63
66 $json = $this->get('serializer')->serialize(['exists' => $exists], 'json'); 64 return $this->sendResponse(['exists' => $exists]);
67
68 return (new JsonResponse())->setJson($json);
69 } 65 }
70 66
71 /** 67 /**
@@ -125,9 +121,7 @@ class EntryRestController extends WallabagRestController
125 ) 121 )
126 ); 122 );
127 123
128 $json = $this->get('serializer')->serialize($paginatedCollection, 'json'); 124 return $this->sendResponse($paginatedCollection);
129
130 return (new JsonResponse())->setJson($json);
131 } 125 }
132 126
133 /** 127 /**
@@ -146,9 +140,7 @@ class EntryRestController extends WallabagRestController
146 $this->validateAuthentication(); 140 $this->validateAuthentication();
147 $this->validateUserAccess($entry->getUser()->getId()); 141 $this->validateUserAccess($entry->getUser()->getId());
148 142
149 $json = $this->get('serializer')->serialize($entry, 'json'); 143 return $this->sendResponse($entry);
150
151 return (new JsonResponse())->setJson($json);
152 } 144 }
153 145
154 /** 146 /**
@@ -189,35 +181,35 @@ class EntryRestController extends WallabagRestController
189 $this->validateAuthentication(); 181 $this->validateAuthentication();
190 182
191 $urls = json_decode($request->query->get('urls', [])); 183 $urls = json_decode($request->query->get('urls', []));
184
185 if (empty($urls)) {
186 return $this->sendResponse([]);
187 }
188
192 $results = []; 189 $results = [];
193 190
194 // handle multiple urls 191 // handle multiple urls
195 if (!empty($urls)) { 192 foreach ($urls as $key => $url) {
196 $results = []; 193 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
197 foreach ($urls as $key => $url) { 194 $url,
198 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( 195 $this->getUser()->getId()
199 $url, 196 );
200 $this->getUser()->getId()
201 );
202
203 $results[$key]['url'] = $url;
204 197
205 if (false !== $entry) { 198 $results[$key]['url'] = $url;
206 $em = $this->getDoctrine()->getManager();
207 $em->remove($entry);
208 $em->flush();
209 199
210 // entry deleted, dispatch event about it! 200 if (false !== $entry) {
211 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); 201 $em = $this->getDoctrine()->getManager();
212 } 202 $em->remove($entry);
203 $em->flush();
213 204
214 $results[$key]['entry'] = $entry instanceof Entry ? true : false; 205 // entry deleted, dispatch event about it!
206 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
215 } 207 }
216 }
217 208
218 $json = $this->get('serializer')->serialize($results, 'json'); 209 $results[$key]['entry'] = $entry instanceof Entry ? true : false;
210 }
219 211
220 return (new JsonResponse())->setJson($json); 212 return $this->sendResponse($results);
221 } 213 }
222 214
223 /** 215 /**
@@ -231,7 +223,7 @@ class EntryRestController extends WallabagRestController
231 * 223 *
232 * @return JsonResponse 224 * @return JsonResponse
233 * 225 *
234 * @throws Symfony\Component\Config\Definition\Exception\Exception When limit is reached 226 * @throws HttpException When limit is reached
235 */ 227 */
236 public function postEntriesListAction(Request $request) 228 public function postEntriesListAction(Request $request)
237 { 229 {
@@ -243,7 +235,7 @@ class EntryRestController extends WallabagRestController
243 $limit = $this->container->getParameter('wallabag_core.api_limit_mass_actions'); 235 $limit = $this->container->getParameter('wallabag_core.api_limit_mass_actions');
244 236
245 if (count($urls) > $limit) { 237 if (count($urls) > $limit) {
246 throw new Exception('API limit reached'); 238 throw new HttpException(400, 'API limit reached');
247 } 239 }
248 240
249 // handle multiple urls 241 // handle multiple urls
@@ -274,9 +266,7 @@ class EntryRestController extends WallabagRestController
274 } 266 }
275 } 267 }
276 268
277 $json = $this->get('serializer')->serialize($results, 'json'); 269 return $this->sendResponse($results);
278
279 return (new JsonResponse())->setJson($json);
280 } 270 }
281 271
282 /** 272 /**
@@ -336,9 +326,7 @@ class EntryRestController extends WallabagRestController
336 // entry saved, dispatch event about it! 326 // entry saved, dispatch event about it!
337 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); 327 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
338 328
339 $json = $this->get('serializer')->serialize($entry, 'json'); 329 return $this->sendResponse($entry);
340
341 return (new JsonResponse())->setJson($json);
342 } 330 }
343 331
344 /** 332 /**
@@ -387,9 +375,7 @@ class EntryRestController extends WallabagRestController
387 $em = $this->getDoctrine()->getManager(); 375 $em = $this->getDoctrine()->getManager();
388 $em->flush(); 376 $em->flush();
389 377
390 $json = $this->get('serializer')->serialize($entry, 'json'); 378 return $this->sendResponse($entry);
391
392 return (new JsonResponse())->setJson($json);
393 } 379 }
394 380
395 /** 381 /**
@@ -432,9 +418,7 @@ class EntryRestController extends WallabagRestController
432 // entry saved, dispatch event about it! 418 // entry saved, dispatch event about it!
433 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); 419 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
434 420
435 $json = $this->get('serializer')->serialize($entry, 'json'); 421 return $this->sendResponse($entry);
436
437 return (new JsonResponse())->setJson($json);
438 } 422 }
439 423
440 /** 424 /**
@@ -460,9 +444,7 @@ class EntryRestController extends WallabagRestController
460 // entry deleted, dispatch event about it! 444 // entry deleted, dispatch event about it!
461 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); 445 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
462 446
463 $json = $this->get('serializer')->serialize($entry, 'json'); 447 return $this->sendResponse($entry);
464
465 return (new JsonResponse())->setJson($json);
466 } 448 }
467 449
468 /** 450 /**
@@ -481,9 +463,7 @@ class EntryRestController extends WallabagRestController
481 $this->validateAuthentication(); 463 $this->validateAuthentication();
482 $this->validateUserAccess($entry->getUser()->getId()); 464 $this->validateUserAccess($entry->getUser()->getId());
483 465
484 $json = $this->get('serializer')->serialize($entry->getTags(), 'json'); 466 return $this->sendResponse($entry->getTags());
485
486 return (new JsonResponse())->setJson($json);
487 } 467 }
488 468
489 /** 469 /**
@@ -514,9 +494,7 @@ class EntryRestController extends WallabagRestController
514 $em->persist($entry); 494 $em->persist($entry);
515 $em->flush(); 495 $em->flush();
516 496
517 $json = $this->get('serializer')->serialize($entry, 'json'); 497 return $this->sendResponse($entry);
518
519 return (new JsonResponse())->setJson($json);
520 } 498 }
521 499
522 /** 500 /**
@@ -541,9 +519,7 @@ class EntryRestController extends WallabagRestController
541 $em->persist($entry); 519 $em->persist($entry);
542 $em->flush(); 520 $em->flush();
543 521
544 $json = $this->get('serializer')->serialize($entry, 'json'); 522 return $this->sendResponse($entry);
545
546 return (new JsonResponse())->setJson($json);
547 } 523 }
548 524
549 /** 525 /**
@@ -562,45 +538,46 @@ class EntryRestController extends WallabagRestController
562 $this->validateAuthentication(); 538 $this->validateAuthentication();
563 539
564 $list = json_decode($request->query->get('list', [])); 540 $list = json_decode($request->query->get('list', []));
565 $results = []; 541
542 if (empty($list)) {
543 return $this->sendResponse([]);
544 }
566 545
567 // handle multiple urls 546 // handle multiple urls
568 if (!empty($list)) { 547 $results = [];
569 foreach ($list as $key => $element) {
570 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
571 $element->url,
572 $this->getUser()->getId()
573 );
574 548
575 $results[$key]['url'] = $element->url; 549 foreach ($list as $key => $element) {
576 $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; 550 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
551 $element->url,
552 $this->getUser()->getId()
553 );
577 554
578 $tags = $element->tags; 555 $results[$key]['url'] = $element->url;
556 $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
579 557
580 if (false !== $entry && !(empty($tags))) { 558 $tags = $element->tags;
581 $tags = explode(',', $tags);
582 foreach ($tags as $label) {
583 $label = trim($label);
584 559
585 $tag = $this->getDoctrine() 560 if (false !== $entry && !(empty($tags))) {
586 ->getRepository('WallabagCoreBundle:Tag') 561 $tags = explode(',', $tags);
587 ->findOneByLabel($label); 562 foreach ($tags as $label) {
563 $label = trim($label);
588 564
589 if (false !== $tag) { 565 $tag = $this->getDoctrine()
590 $entry->removeTag($tag); 566 ->getRepository('WallabagCoreBundle:Tag')
591 } 567 ->findOneByLabel($label);
592 }
593 568
594 $em = $this->getDoctrine()->getManager(); 569 if (false !== $tag) {
595 $em->persist($entry); 570 $entry->removeTag($tag);
596 $em->flush(); 571 }
597 } 572 }
573
574 $em = $this->getDoctrine()->getManager();
575 $em->persist($entry);
576 $em->flush();
598 } 577 }
599 } 578 }
600 579
601 $json = $this->get('serializer')->serialize($results, 'json'); 580 return $this->sendResponse($results);
602
603 return (new JsonResponse())->setJson($json);
604 } 581 }
605 582
606 /** 583 /**
@@ -619,32 +596,47 @@ class EntryRestController extends WallabagRestController
619 $this->validateAuthentication(); 596 $this->validateAuthentication();
620 597
621 $list = json_decode($request->query->get('list', [])); 598 $list = json_decode($request->query->get('list', []));
599
600 if (empty($list)) {
601 return $this->sendResponse([]);
602 }
603
622 $results = []; 604 $results = [];
623 605
624 // handle multiple urls 606 // handle multiple urls
625 if (!empty($list)) { 607 foreach ($list as $key => $element) {
626 foreach ($list as $key => $element) { 608 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
627 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( 609 $element->url,
628 $element->url, 610 $this->getUser()->getId()
629 $this->getUser()->getId() 611 );
630 );
631 612
632 $results[$key]['url'] = $element->url; 613 $results[$key]['url'] = $element->url;
633 $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; 614 $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
634 615
635 $tags = $element->tags; 616 $tags = $element->tags;
636 617
637 if (false !== $entry && !(empty($tags))) { 618 if (false !== $entry && !(empty($tags))) {
638 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); 619 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
639 620
640 $em = $this->getDoctrine()->getManager(); 621 $em = $this->getDoctrine()->getManager();
641 $em->persist($entry); 622 $em->persist($entry);
642 $em->flush(); 623 $em->flush();
643 }
644 } 624 }
645 } 625 }
646 626
647 $json = $this->get('serializer')->serialize($results, 'json'); 627 return $this->sendResponse($results);
628 }
629
630 /**
631 * Shortcut to send data serialized in json.
632 *
633 * @param mixed $data
634 *
635 * @return JsonResponse
636 */
637 private function sendResponse($data)
638 {
639 $json = $this->get('serializer')->serialize($data, 'json');
648 640
649 return (new JsonResponse())->setJson($json); 641 return (new JsonResponse())->setJson($json);
650 } 642 }