diff options
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/EntryRestController.php | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 768c4fdc..c7938633 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -4,6 +4,7 @@ namespace Wallabag\ApiBundle\Controller; | |||
4 | 4 | ||
5 | use Hateoas\Configuration\Route; | 5 | use Hateoas\Configuration\Route; |
6 | use Hateoas\Representation\Factory\PagerfantaFactory; | 6 | use Hateoas\Representation\Factory\PagerfantaFactory; |
7 | use JMS\Serializer\SerializationContext; | ||
7 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; | 8 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
8 | use Symfony\Component\HttpKernel\Exception\HttpException; | 9 | use Symfony\Component\HttpKernel\Exception\HttpException; |
9 | use Symfony\Component\HttpFoundation\Request; | 10 | use Symfony\Component\HttpFoundation\Request; |
@@ -18,9 +19,14 @@ class EntryRestController extends WallabagRestController | |||
18 | { | 19 | { |
19 | /** | 20 | /** |
20 | * Check if an entry exist by url. | 21 | * Check if an entry exist by url. |
22 | * Return ID if entry(ies) exist (and if you give the return_id parameter). | ||
23 | * Otherwise it returns false. | ||
24 | * | ||
25 | * @todo Remove that `return_id` in the next major release | ||
21 | * | 26 | * |
22 | * @ApiDoc( | 27 | * @ApiDoc( |
23 | * parameters={ | 28 | * parameters={ |
29 | * {"name"="return_id", "dataType"="string", "required"=false, "format"="1 or 0", "description"="Set 1 if you want to retrieve ID in case entry(ies) exists, 0 by default"}, | ||
24 | * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"}, | 30 | * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"}, |
25 | * {"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"} | 31 | * {"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"} |
26 | * } | 32 | * } |
@@ -32,6 +38,7 @@ class EntryRestController extends WallabagRestController | |||
32 | { | 38 | { |
33 | $this->validateAuthentication(); | 39 | $this->validateAuthentication(); |
34 | 40 | ||
41 | $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id'); | ||
35 | $urls = $request->query->get('urls', []); | 42 | $urls = $request->query->get('urls', []); |
36 | 43 | ||
37 | // handle multiple urls first | 44 | // handle multiple urls first |
@@ -42,7 +49,7 @@ class EntryRestController extends WallabagRestController | |||
42 | ->getRepository('WallabagCoreBundle:Entry') | 49 | ->getRepository('WallabagCoreBundle:Entry') |
43 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | 50 | ->findByUrlAndUserId($url, $this->getUser()->getId()); |
44 | 51 | ||
45 | $results[$url] = $res instanceof Entry ? $res->getId() : false; | 52 | $results[$url] = $this->returnExistInformation($res, $returnId); |
46 | } | 53 | } |
47 | 54 | ||
48 | return $this->sendResponse($results); | 55 | return $this->sendResponse($results); |
@@ -59,7 +66,7 @@ class EntryRestController extends WallabagRestController | |||
59 | ->getRepository('WallabagCoreBundle:Entry') | 66 | ->getRepository('WallabagCoreBundle:Entry') |
60 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | 67 | ->findByUrlAndUserId($url, $this->getUser()->getId()); |
61 | 68 | ||
62 | $exists = $res instanceof Entry ? $res->getId() : false; | 69 | $exists = $this->returnExistInformation($res, $returnId); |
63 | 70 | ||
64 | return $this->sendResponse(['exists' => $exists]); | 71 | return $this->sendResponse(['exists' => $exists]); |
65 | } | 72 | } |
@@ -617,7 +624,11 @@ class EntryRestController extends WallabagRestController | |||
617 | */ | 624 | */ |
618 | private function sendResponse($data) | 625 | private function sendResponse($data) |
619 | { | 626 | { |
620 | $json = $this->get('serializer')->serialize($data, 'json'); | 627 | // https://github.com/schmittjoh/JMSSerializerBundle/issues/293 |
628 | $context = new SerializationContext(); | ||
629 | $context->setSerializeNull(true); | ||
630 | |||
631 | $json = $this->get('serializer')->serialize($data, 'json', $context); | ||
621 | 632 | ||
622 | return (new JsonResponse())->setJson($json); | 633 | return (new JsonResponse())->setJson($json); |
623 | } | 634 | } |
@@ -694,4 +705,21 @@ class EntryRestController extends WallabagRestController | |||
694 | // entry saved, dispatch event about it! | 705 | // entry saved, dispatch event about it! |
695 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | 706 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); |
696 | } | 707 | } |
708 | |||
709 | /** | ||
710 | * Return information about the entry if it exist and depending on the id or not. | ||
711 | * | ||
712 | * @param Entry|null $entry | ||
713 | * @param bool $returnId | ||
714 | * | ||
715 | * @return bool|int | ||
716 | */ | ||
717 | private function returnExistInformation($entry, $returnId) | ||
718 | { | ||
719 | if ($returnId) { | ||
720 | return $entry instanceof Entry ? $entry->getId() : null; | ||
721 | } | ||
722 | |||
723 | return $entry instanceof Entry; | ||
724 | } | ||
697 | } | 725 | } |