From 900c844861eac95fc41afbbbc121584b8f9e6c32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 28 Oct 2016 14:46:30 +0200 Subject: Exploded WallabagRestController into many controllers Fix #2503 --- .../ApiBundle/Controller/TagRestController.php | 171 +++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 src/Wallabag/ApiBundle/Controller/TagRestController.php (limited to 'src/Wallabag/ApiBundle/Controller/TagRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/TagRestController.php b/src/Wallabag/ApiBundle/Controller/TagRestController.php new file mode 100644 index 00000000..4e7ddc66 --- /dev/null +++ b/src/Wallabag/ApiBundle/Controller/TagRestController.php @@ -0,0 +1,171 @@ +validateAuthentication(); + + $tags = $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Tag') + ->findAllTags($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); + + $this->cleanOrphanTag($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); + + $this->cleanOrphanTag($tags); + + $json = $this->get('serializer')->serialize($tags, 'json'); + + return (new JsonResponse())->setJson($json); + } + + /** + * Permanently remove one tag from **every** entry. + * + * @ApiDoc( + * requirements={ + * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"} + * } + * ) + * + * @return JsonResponse + */ + public function deleteTagAction(Tag $tag) + { + $this->validateAuthentication(); + + $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Entry') + ->removeTag($this->getUser()->getId(), $tag); + + $this->cleanOrphanTag($tag); + + $json = $this->get('serializer')->serialize($tag, 'json'); + + return (new JsonResponse())->setJson($json); + } + + /** + * Retrieve version number. + * + * @ApiDoc() + * + * @return JsonResponse + */ + public function getVersionAction() + { + $version = $this->container->getParameter('wallabag_core.version'); + + $json = $this->get('serializer')->serialize($version, 'json'); + + return (new JsonResponse())->setJson($json); + } + + /** + * Remove orphan tag in case no entries are associated to it. + * + * @param Tag|array $tags + */ + private function cleanOrphanTag($tags) + { + if (!is_array($tags)) { + $tags = [$tags]; + } + + $em = $this->getDoctrine()->getManager(); + + foreach ($tags as $tag) { + if (count($tag->getEntries()) === 0) { + $em->remove($tag); + } + } + + $em->flush(); + } +} -- cgit v1.2.3