]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/ApiBundle/Controller/WallabagRestController.php
Merge pull request #2274 from wallabag/json-response-created-at
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / WallabagRestController.php
index 03eb9b0848272381bfbc3dd550c476068d55dcd3..fb7c6c1f14a0ff90f0601241f043c7047b89edb7 100644 (file)
@@ -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;
@@ -38,7 +38,7 @@ class WallabagRestController extends FOSRestController
      *       }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function getEntriesAction(Request $request)
     {
@@ -68,7 +68,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -80,7 +80,7 @@ class WallabagRestController extends FOSRestController
      *      }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function getEntryAction(Entry $entry)
     {
@@ -89,7 +89,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($entry, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -105,7 +105,7 @@ class WallabagRestController extends FOSRestController
      *       }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function postEntriesAction(Request $request)
     {
@@ -149,7 +149,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($entry, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -167,7 +167,7 @@ class WallabagRestController extends FOSRestController
      *      }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function patchEntriesAction(Entry $entry, Request $request)
     {
@@ -200,7 +200,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($entry, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -212,7 +212,7 @@ class WallabagRestController extends FOSRestController
      *      }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function deleteEntriesAction(Entry $entry)
     {
@@ -225,7 +225,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($entry, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -237,7 +237,7 @@ class WallabagRestController extends FOSRestController
      *      }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function getEntriesTagsAction(Entry $entry)
     {
@@ -246,7 +246,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -261,7 +261,7 @@ class WallabagRestController extends FOSRestController
      *       }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function postEntriesTagsAction(Request $request, Entry $entry)
     {
@@ -279,7 +279,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($entry, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -292,7 +292,7 @@ class WallabagRestController extends FOSRestController
      *      }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
     {
@@ -306,7 +306,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($entry, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -314,7 +314,7 @@ class WallabagRestController extends FOSRestController
      *
      * @ApiDoc()
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function getTagsAction()
     {
@@ -322,11 +322,84 @@ class WallabagRestController extends FOSRestController
 
         $tags = $this->getDoctrine()
             ->getRepository('WallabagCoreBundle:Tag')
-            ->findAllTags($this->getUser()->getId());
+            ->findAllTags($this->getUser()->getId())
+            ->getQuery()
+            ->getResult();
 
         $json = $this->get('serializer')->serialize($tags, 'json');
 
-        return $this->renderJsonResponse($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 (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -338,7 +411,7 @@ class WallabagRestController extends FOSRestController
      *      }
      * )
      *
-     * @return Response
+     * @return JsonResponse
      */
     public function deleteTagAction(Tag $tag)
     {
@@ -350,14 +423,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()
     {
@@ -365,7 +439,7 @@ class WallabagRestController extends FOSRestController
 
         $json = $this->get('serializer')->serialize($version, 'json');
 
-        return $this->renderJsonResponse($json);
+        return (new JsonResponse())->setJson($json);
     }
 
     /**
@@ -381,17 +455,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']);
-    }
 }