]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Controller/WallabagRestController.php
first draft of hypermedia implementation
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / WallabagRestController.php
index c298d84959ccee911d27f171581072cd354e2fd9..cadd7e755cabf4a815fbe6f03127e37dae84543d 100644 (file)
@@ -5,13 +5,38 @@ namespace Wallabag\CoreBundle\Controller;
 use Nelmio\ApiDocBundle\Annotation\ApiDoc;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 use Wallabag\CoreBundle\Entity\Entry;
 use Wallabag\CoreBundle\Entity\Tag;
 use Wallabag\CoreBundle\Service\Extractor;
+use Hateoas\HateoasBuilder;
 
 class WallabagRestController extends Controller
 {
+    /**
+     * Retrieve salt for a giver user.
+     *
+     * @ApiDoc(
+     *       parameters={
+     *          {"name"="username", "dataType"="string", "required"=true, "description"="username"}
+     *       }
+     * )
+     * @return array
+     */
+    public function getSaltAction($username)
+    {
+        $user = $this
+            ->getDoctrine()
+            ->getRepository('WallabagCoreBundle:User')
+            ->findOneByUsername($username);
+
+        if (is_null($user)) {
+            throw $this->createNotFoundException();
+        }
+
+        return array($user->getSalt() ?: null);
+    }
     /**
      * Retrieve all entries. It could be filtered by many options.
      *
@@ -43,12 +68,15 @@ class WallabagRestController extends Controller
         $entries = $this
             ->getDoctrine()
             ->getRepository('WallabagCoreBundle:Entry')
-            ->findEntries(1, $isArchived, $isStarred, $isDeleted, $sort, $order);
+            ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $isDeleted, $sort, $order);
 
-        if (!is_array($entries)) {
+        if (!($entries)) {
             throw $this->createNotFoundException();
         }
 
+        $hateoas = HateoasBuilder::create()->build();
+        $json = $hateoas->serialize($entries, 'json');
+
         return $entries;
     }
 
@@ -64,7 +92,10 @@ class WallabagRestController extends Controller
      */
     public function getEntryAction(Entry $entry)
     {
-        return $entry;
+        $hateoas = HateoasBuilder::create()->build();
+        $json = $hateoas->serialize($entry, 'json');
+
+        return new Response($json, 200, array('application/json'));
     }
 
     /**
@@ -85,8 +116,7 @@ class WallabagRestController extends Controller
         $url = $request->request->get('url');
 
         $content = Extractor::extract($url);
-        $entry = new Entry();
-        $entry->setUserId(1);
+        $entry = new Entry($this->getUser());
         $entry->setUrl($url);
         $entry->setTitle($request->request->get('title') ?: $content->getTitle());
         $entry->setContent($content->getBody());