diff options
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller/WallabagRestController.php')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/WallabagRestController.php | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index 85875589..fa573988 100644 --- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php | |||
@@ -28,7 +28,8 @@ class WallabagRestController extends FOSRestController | |||
28 | * | 28 | * |
29 | * @ApiDoc( | 29 | * @ApiDoc( |
30 | * parameters={ | 30 | * parameters={ |
31 | * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"} | 31 | * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"}, |
32 | * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="Urls (as an array) to check if it exists"} | ||
32 | * } | 33 | * } |
33 | * ) | 34 | * ) |
34 | * | 35 | * |
@@ -38,6 +39,25 @@ class WallabagRestController extends FOSRestController | |||
38 | { | 39 | { |
39 | $this->validateAuthentication(); | 40 | $this->validateAuthentication(); |
40 | 41 | ||
42 | $urls = $request->query->get('urls', []); | ||
43 | |||
44 | // handle multiple urls first | ||
45 | if (!empty($urls)) { | ||
46 | $results = []; | ||
47 | foreach ($urls as $url) { | ||
48 | $res = $this->getDoctrine() | ||
49 | ->getRepository('WallabagCoreBundle:Entry') | ||
50 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | ||
51 | |||
52 | $results[$url] = false === $res ? false : true; | ||
53 | } | ||
54 | |||
55 | $json = $this->get('serializer')->serialize($results, 'json'); | ||
56 | |||
57 | return (new JsonResponse())->setJson($json); | ||
58 | } | ||
59 | |||
60 | // let's see if it is a simple url? | ||
41 | $url = $request->query->get('url', ''); | 61 | $url = $request->query->get('url', ''); |
42 | 62 | ||
43 | if (empty($url)) { | 63 | if (empty($url)) { |
@@ -392,7 +412,7 @@ class WallabagRestController extends FOSRestController | |||
392 | 412 | ||
393 | $tags = $this->getDoctrine() | 413 | $tags = $this->getDoctrine() |
394 | ->getRepository('WallabagCoreBundle:Tag') | 414 | ->getRepository('WallabagCoreBundle:Tag') |
395 | ->findAllTagsWithEntries($this->getUser()->getId()); | 415 | ->findAllTags($this->getUser()->getId()); |
396 | 416 | ||
397 | $json = $this->get('serializer')->serialize($tags, 'json'); | 417 | $json = $this->get('serializer')->serialize($tags, 'json'); |
398 | 418 | ||
@@ -425,6 +445,8 @@ class WallabagRestController extends FOSRestController | |||
425 | ->getRepository('WallabagCoreBundle:Entry') | 445 | ->getRepository('WallabagCoreBundle:Entry') |
426 | ->removeTag($this->getUser()->getId(), $tag); | 446 | ->removeTag($this->getUser()->getId(), $tag); |
427 | 447 | ||
448 | $this->cleanOrphanTag($tag); | ||
449 | |||
428 | $json = $this->get('serializer')->serialize($tag, 'json'); | 450 | $json = $this->get('serializer')->serialize($tag, 'json'); |
429 | 451 | ||
430 | return (new JsonResponse())->setJson($json); | 452 | return (new JsonResponse())->setJson($json); |
@@ -465,6 +487,8 @@ class WallabagRestController extends FOSRestController | |||
465 | ->getRepository('WallabagCoreBundle:Entry') | 487 | ->getRepository('WallabagCoreBundle:Entry') |
466 | ->removeTags($this->getUser()->getId(), $tags); | 488 | ->removeTags($this->getUser()->getId(), $tags); |
467 | 489 | ||
490 | $this->cleanOrphanTag($tags); | ||
491 | |||
468 | $json = $this->get('serializer')->serialize($tags, 'json'); | 492 | $json = $this->get('serializer')->serialize($tags, 'json'); |
469 | 493 | ||
470 | return (new JsonResponse())->setJson($json); | 494 | return (new JsonResponse())->setJson($json); |
@@ -489,6 +513,8 @@ class WallabagRestController extends FOSRestController | |||
489 | ->getRepository('WallabagCoreBundle:Entry') | 513 | ->getRepository('WallabagCoreBundle:Entry') |
490 | ->removeTag($this->getUser()->getId(), $tag); | 514 | ->removeTag($this->getUser()->getId(), $tag); |
491 | 515 | ||
516 | $this->cleanOrphanTag($tag); | ||
517 | |||
492 | $json = $this->get('serializer')->serialize($tag, 'json'); | 518 | $json = $this->get('serializer')->serialize($tag, 'json'); |
493 | 519 | ||
494 | return (new JsonResponse())->setJson($json); | 520 | return (new JsonResponse())->setJson($json); |
@@ -511,6 +537,28 @@ class WallabagRestController extends FOSRestController | |||
511 | } | 537 | } |
512 | 538 | ||
513 | /** | 539 | /** |
540 | * Remove orphan tag in case no entries are associated to it. | ||
541 | * | ||
542 | * @param Tag|array $tags | ||
543 | */ | ||
544 | private function cleanOrphanTag($tags) | ||
545 | { | ||
546 | if (!is_array($tags)) { | ||
547 | $tags = [$tags]; | ||
548 | } | ||
549 | |||
550 | $em = $this->getDoctrine()->getManager(); | ||
551 | |||
552 | foreach ($tags as $tag) { | ||
553 | if (count($tag->getEntries()) === 0) { | ||
554 | $em->remove($tag); | ||
555 | } | ||
556 | } | ||
557 | |||
558 | $em->flush(); | ||
559 | } | ||
560 | |||
561 | /** | ||
514 | * Validate that the first id is equal to the second one. | 562 | * Validate that the first id is equal to the second one. |
515 | * If not, throw exception. It means a user try to access information from an other user. | 563 | * If not, throw exception. It means a user try to access information from an other user. |
516 | * | 564 | * |