X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FApiBundle%2FController%2FWallabagRestController.php;h=104720a9ef98aa9567f83c8c6ab2c5770114c8fc;hb=1dc3bee6b916b228c596a45d20dc6ae14ac555cb;hp=4fae4b0a8a07842eeb2ed53cd87a056ac641e117;hpb=e43c78abd9193641d07ea27d0275fc19419b89ad;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index 4fae4b0a..104720a9 100644 --- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php @@ -7,7 +7,7 @@ use Hateoas\Configuration\Route; use Hateoas\Representation\Factory\PagerfantaFactory; use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Security\Core\Exception\AccessDeniedException; use Wallabag\CoreBundle\Entity\Entry; @@ -22,6 +22,38 @@ class WallabagRestController extends FOSRestController } } + /** + * Check if an entry exist by url. + * + * @ApiDoc( + * parameters={ + * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"} + * } + * ) + * + * @return JsonResponse + */ + public function getEntriesExistsAction(Request $request) + { + $this->validateAuthentication(); + + $url = $request->query->get('url', ''); + + if (empty($url)) { + throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$user->getId()); + } + + $res = $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Entry') + ->findByUrlAndUserId($url, $this->getUser()->getId()); + + $exists = false === $res ? false : true; + + $json = $this->get('serializer')->serialize(['exists' => $exists], 'json'); + + return (new JsonResponse())->setJson($json); + } + /** * Retrieve all entries. It could be filtered by many options. * @@ -38,7 +70,7 @@ class WallabagRestController extends FOSRestController * } * ) * - * @return Response + * @return JsonResponse */ public function getEntriesAction(Request $request) { @@ -51,10 +83,11 @@ class WallabagRestController extends FOSRestController $page = (int) $request->query->get('page', 1); $perPage = (int) $request->query->get('perPage', 30); $since = $request->query->get('since', 0); + $tags = $request->query->get('tags', ''); $pager = $this->getDoctrine() ->getRepository('WallabagCoreBundle:Entry') - ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since); + ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags); $pager->setCurrentPage($page); $pager->setMaxPerPage($perPage); @@ -67,7 +100,7 @@ class WallabagRestController extends FOSRestController $json = $this->get('serializer')->serialize($paginatedCollection, 'json'); - return $this->renderJsonResponse($json); + return (new JsonResponse())->setJson($json); } /** @@ -79,7 +112,7 @@ class WallabagRestController extends FOSRestController * } * ) * - * @return Response + * @return JsonResponse */ public function getEntryAction(Entry $entry) { @@ -88,7 +121,7 @@ class WallabagRestController extends FOSRestController $json = $this->get('serializer')->serialize($entry, 'json'); - return $this->renderJsonResponse($json); + return (new JsonResponse())->setJson($json); } /** @@ -104,7 +137,7 @@ class WallabagRestController extends FOSRestController * } * ) * - * @return Response + * @return JsonResponse */ public function postEntriesAction(Request $request) { @@ -148,7 +181,7 @@ class WallabagRestController extends FOSRestController $json = $this->get('serializer')->serialize($entry, 'json'); - return $this->renderJsonResponse($json); + return (new JsonResponse())->setJson($json); } /** @@ -166,7 +199,7 @@ class WallabagRestController extends FOSRestController * } * ) * - * @return Response + * @return JsonResponse */ public function patchEntriesAction(Entry $entry, Request $request) { @@ -199,7 +232,7 @@ class WallabagRestController extends FOSRestController $json = $this->get('serializer')->serialize($entry, 'json'); - return $this->renderJsonResponse($json); + return (new JsonResponse())->setJson($json); } /** @@ -211,7 +244,7 @@ class WallabagRestController extends FOSRestController * } * ) * - * @return Response + * @return JsonResponse */ public function deleteEntriesAction(Entry $entry) { @@ -224,7 +257,7 @@ class WallabagRestController extends FOSRestController $json = $this->get('serializer')->serialize($entry, 'json'); - return $this->renderJsonResponse($json); + return (new JsonResponse())->setJson($json); } /** @@ -236,7 +269,7 @@ class WallabagRestController extends FOSRestController * } * ) * - * @return Response + * @return JsonResponse */ public function getEntriesTagsAction(Entry $entry) { @@ -245,7 +278,7 @@ class WallabagRestController extends FOSRestController $json = $this->get('serializer')->serialize($entry->getTags(), 'json'); - return $this->renderJsonResponse($json); + return (new JsonResponse())->setJson($json); } /** @@ -260,7 +293,7 @@ class WallabagRestController extends FOSRestController * } * ) * - * @return Response + * @return JsonResponse */ public function postEntriesTagsAction(Request $request, Entry $entry) { @@ -278,7 +311,7 @@ class WallabagRestController extends FOSRestController $json = $this->get('serializer')->serialize($entry, 'json'); - return $this->renderJsonResponse($json); + return (new JsonResponse())->setJson($json); } /** @@ -291,7 +324,7 @@ class WallabagRestController extends FOSRestController * } * ) * - * @return Response + * @return JsonResponse */ public function deleteEntriesTagsAction(Entry $entry, Tag $tag) { @@ -305,7 +338,7 @@ class WallabagRestController extends FOSRestController $json = $this->get('serializer')->serialize($entry, 'json'); - return $this->renderJsonResponse($json); + return (new JsonResponse())->setJson($json); } /** @@ -313,7 +346,7 @@ class WallabagRestController extends FOSRestController * * @ApiDoc() * - * @return Response + * @return JsonResponse */ public function getTagsAction() { @@ -321,11 +354,82 @@ class WallabagRestController extends FOSRestController $tags = $this->getDoctrine() ->getRepository('WallabagCoreBundle:Tag') - ->findAllTags($this->getUser()->getId()); + ->findAllTagsWithEntries($this->getUser()->getId()); + + $json = $this->get('serializer')->serialize($tags, 'json'); + + return (new JsonResponse())->setJson($json); + } + + /** + * Permanently remove one tag from **every** entry. + * + * @ApiDoc( + * requirements={ + * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"} + * } + * ) + * + * @return JsonResponse + */ + public function deleteTagLabelAction(Request $request) + { + $this->validateAuthentication(); + $label = $request->request->get('tag', ''); + + $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label); + + if (empty($tag)) { + throw $this->createNotFoundException('Tag not found'); + } + + $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Entry') + ->removeTag($this->getUser()->getId(), $tag); + + $json = $this->get('serializer')->serialize($tag, 'json'); + + return (new JsonResponse())->setJson($json); + } + + /** + * Permanently remove some tags from **every** entry. + * + * @ApiDoc( + * requirements={ + * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"} + * } + * ) + * + * @return JsonResponse + */ + public function deleteTagsLabelAction(Request $request) + { + $this->validateAuthentication(); + + $tagsLabels = $request->request->get('tags', ''); + + $tags = []; + + foreach (explode(',', $tagsLabels) as $tagLabel) { + $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel); + + if (!empty($tagEntity)) { + $tags[] = $tagEntity; + } + } + + if (empty($tags)) { + throw $this->createNotFoundException('Tags not found'); + } + + $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Entry') + ->removeTags($this->getUser()->getId(), $tags); $json = $this->get('serializer')->serialize($tags, 'json'); - return $this->renderJsonResponse($json); + return (new JsonResponse())->setJson($json); } /** @@ -337,7 +441,7 @@ class WallabagRestController extends FOSRestController * } * ) * - * @return Response + * @return JsonResponse */ public function deleteTagAction(Tag $tag) { @@ -349,14 +453,15 @@ class WallabagRestController extends FOSRestController $json = $this->get('serializer')->serialize($tag, 'json'); - return $this->renderJsonResponse($json); + return (new JsonResponse())->setJson($json); } + /** * Retrieve version number. * * @ApiDoc() * - * @return Response + * @return JsonResponse */ public function getVersionAction() { @@ -364,7 +469,7 @@ class WallabagRestController extends FOSRestController $json = $this->get('serializer')->serialize($version, 'json'); - return $this->renderJsonResponse($json); + return (new JsonResponse())->setJson($json); } /** @@ -380,17 +485,4 @@ class WallabagRestController extends FOSRestController throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$user->getId()); } } - - /** - * Send a JSON Response. - * We don't use the Symfony JsonRespone, because it takes an array as parameter instead of a JSON string. - * - * @param string $json - * - * @return Response - */ - private function renderJsonResponse($json) - { - return new Response($json, 200, ['application/json']); - } }