]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/ApiBundle/Controller/WallabagRestController.php
Remove 'content' from API
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / WallabagRestController.php
index 459c41729d8a8b9543a98c64cc6147267da3ce87..35a90edde12bee3bc8b923edda9fa7311a4ff963 100644 (file)
@@ -3,41 +3,18 @@
 namespace Wallabag\ApiBundle\Controller;
 
 use FOS\RestBundle\Controller\FOSRestController;
+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\Routing\Generator\UrlGeneratorInterface;
+use Symfony\Component\Security\Core\Exception\AccessDeniedException;
 use Wallabag\CoreBundle\Entity\Entry;
 use Wallabag\CoreBundle\Entity\Tag;
-use Hateoas\Configuration\Route;
-use Hateoas\Representation\Factory\PagerfantaFactory;
 
 class WallabagRestController extends FOSRestController
 {
-    /**
-     * @param Entry  $entry
-     * @param string $tags
-     */
-    private function assignTagsToEntry(Entry $entry, $tags)
-    {
-        foreach (explode(',', $tags) as $label) {
-            $label = trim($label);
-            $tagEntity = $this
-                ->getDoctrine()
-                ->getRepository('WallabagCoreBundle:Tag')
-                ->findOneByLabel($label);
-
-            if (is_null($tagEntity)) {
-                $tagEntity = new Tag();
-                $tagEntity->setLabel($label);
-            }
-
-            // only add the tag on the entry if the relation doesn't exist
-            if (!$entry->getTags()->contains($tagEntity)) {
-                $entry->addTag($tagEntity);
-            }
-        }
-    }
-
     private function validateAuthentication()
     {
         if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
@@ -60,7 +37,7 @@ class WallabagRestController extends FOSRestController
      *       }
      * )
      *
-     * @return Entry
+     * @return Response
      */
     public function getEntriesAction(Request $request)
     {
@@ -72,7 +49,6 @@ class WallabagRestController extends FOSRestController
         $order = $request->query->get('order', 'desc');
         $page = (int) $request->query->get('page', 1);
         $perPage = (int) $request->query->get('perPage', 30);
-        $tags = $request->query->get('tags', []);
 
         $pager = $this->getDoctrine()
             ->getRepository('WallabagCoreBundle:Entry')
@@ -84,7 +60,7 @@ class WallabagRestController extends FOSRestController
         $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
         $paginatedCollection = $pagerfantaFactory->createRepresentation(
             $pager,
-            new Route('api_get_entries', [], $absolute = true)
+            new Route('api_get_entries', [], UrlGeneratorInterface::ABSOLUTE_URL)
         );
 
         $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
@@ -101,7 +77,7 @@ class WallabagRestController extends FOSRestController
      *      }
      * )
      *
-     * @return Entry
+     * @return Response
      */
     public function getEntryAction(Entry $entry)
     {
@@ -121,16 +97,20 @@ class WallabagRestController extends FOSRestController
      *          {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
      *          {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
      *          {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
+     *          {"name"="starred", "dataType"="boolean", "required"=false, "format"="true or false", "description"="entry already starred"},
+     *          {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false", "description"="entry already archived"},
      *       }
      * )
      *
-     * @return Entry
+     * @return Response
      */
     public function postEntriesAction(Request $request)
     {
         $this->validateAuthentication();
 
         $url = $request->request->get('url');
+        $isArchived = $request->request->get('archive');
+        $isStarred = $request->request->get('starred');
 
         $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
             new Entry($this->getUser()),
@@ -139,11 +119,20 @@ class WallabagRestController extends FOSRestController
 
         $tags = $request->request->get('tags', '');
         if (!empty($tags)) {
-            $this->assignTagsToEntry($entry, $tags);
+            $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
+        }
+
+        if (true === (bool) $isStarred) {
+            $entry->setStarred(true);
+        }
+
+        if (true === (bool) $isArchived) {
+            $entry->setArchived(true);
         }
 
         $em = $this->getDoctrine()->getManager();
         $em->persist($entry);
+
         $em->flush();
 
         $json = $this->get('serializer')->serialize($entry, 'json');
@@ -166,7 +155,7 @@ class WallabagRestController extends FOSRestController
      *      }
      * )
      *
-     * @return Entry
+     * @return Response
      */
     public function patchEntriesAction(Entry $entry, Request $request)
     {
@@ -191,7 +180,7 @@ class WallabagRestController extends FOSRestController
 
         $tags = $request->request->get('tags', '');
         if (!empty($tags)) {
-            $this->assignTagsToEntry($entry, $tags);
+            $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
         }
 
         $em = $this->getDoctrine()->getManager();
@@ -211,7 +200,7 @@ class WallabagRestController extends FOSRestController
      *      }
      * )
      *
-     * @return Entry
+     * @return Response
      */
     public function deleteEntriesAction(Entry $entry)
     {
@@ -235,6 +224,8 @@ class WallabagRestController extends FOSRestController
      *          {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
      *      }
      * )
+     *
+     * @return Response
      */
     public function getEntriesTagsAction(Entry $entry)
     {
@@ -257,6 +248,8 @@ class WallabagRestController extends FOSRestController
      *          {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
      *       }
      * )
+     *
+     * @return Response
      */
     public function postEntriesTagsAction(Request $request, Entry $entry)
     {
@@ -265,7 +258,7 @@ class WallabagRestController extends FOSRestController
 
         $tags = $request->request->get('tags', '');
         if (!empty($tags)) {
-            $this->assignTagsToEntry($entry, $tags);
+            $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
         }
 
         $em = $this->getDoctrine()->getManager();
@@ -286,6 +279,8 @@ class WallabagRestController extends FOSRestController
      *          {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
      *      }
      * )
+     *
+     * @return Response
      */
     public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
     {
@@ -306,6 +301,8 @@ class WallabagRestController extends FOSRestController
      * Retrieve all tags.
      *
      * @ApiDoc()
+     *
+     * @return Response
      */
     public function getTagsAction()
     {
@@ -328,6 +325,8 @@ class WallabagRestController extends FOSRestController
      *          {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"}
      *      }
      * )
+     *
+     * @return Response
      */
     public function deleteTagAction(Tag $tag)
     {