From 4da01f492b20312461d3f4f612a98e3b52c76fa9 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Sat, 25 Jun 2016 18:37:41 +0200 Subject: Delete tag or tags by label Tests not included --- .../Controller/WallabagRestController.php | 61 ++++++++++++++++++++++ .../CoreBundle/Repository/EntryRepository.php | 13 +++++ 2 files changed, 74 insertions(+) (limited to 'src/Wallabag') diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index 03eb9b08..8eaff5f6 100644 --- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php @@ -352,6 +352,67 @@ class WallabagRestController extends FOSRestController return $this->renderJsonResponse($json); } + + /** + * Permanently remove one tag from **every** entry. + * + * @ApiDoc( + * requirements={ + * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag as a string"} + * } + * ) + * + * @return Response + */ + public function deleteTagLabelAction(Request $request) + { + $this->validateAuthentication(); + $label = $request->query->get('tag',''); + + $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label); + $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Entry') + ->removeTag($this->getUser()->getId(), $tag); + + $json = $this->get('serializer')->serialize($tag, 'json'); + + return $this->renderJsonResponse($json); + } + + /** + * Permanently remove some tags from **every** entry. + * + * @ApiDoc( + * requirements={ + * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="The tags as strings"} + * } + * ) + * + * @return Response + */ + public function deleteTagsLabelAction(Request $request) + { + $this->validateAuthentication(); + + $tagsLabels = $request->query->get('tags', ''); + + $tags = array(); + + foreach (explode(',', $tagsLabels) as $tagLabel) { + $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel); + $tags[] = $tagEntity; + } + + $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Entry') + ->removeTags($this->getUser()->getId(), $tags); + + $json = $this->get('serializer')->serialize($tags, 'json'); + + return $this->renderJsonResponse($json); + } + + /** * Retrieve version number. * diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index e9351d85..63c4c3be 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -222,6 +222,19 @@ class EntryRepository extends EntityRepository $this->getEntityManager()->flush(); } + /** + * Remove tags from all user entries + * + * @param int $userId + * @param Array $tags + */ + + public function removeTags($userId, $tags) { + foreach ($tags as $tag) { + $this->removeTag($userId, $tag); + } + } + /** * Find all entries that are attached to a give tag id. * -- cgit v1.2.3