]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Add since parameter
authorThomas Citharel <tcit@tcit.fr>
Sat, 25 Jun 2016 19:05:50 +0000 (21:05 +0200)
committerThomas Citharel <tcit@tcit.fr>
Sat, 25 Jun 2016 19:05:50 +0000 (21:05 +0200)
src/Wallabag/ApiBundle/Controller/WallabagRestController.php
src/Wallabag/CoreBundle/Repository/EntryRepository.php
tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php

index af24e4988650b33aa7b6b2ae6462f21204a42706..7d624812154d7b7558db20f82c3609d2d73ac37e 100644 (file)
@@ -34,6 +34,7 @@ class WallabagRestController extends FOSRestController
      *          {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
      *          {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
      *          {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
+     *          {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."},
      *       }
      * )
      *
@@ -49,10 +50,11 @@ class WallabagRestController extends FOSRestController
         $order = $request->query->get('order', 'desc');
         $page = (int) $request->query->get('page', 1);
         $perPage = (int) $request->query->get('perPage', 30);
+        $since = $request->query->get('since',0);
 
         $pager = $this->getDoctrine()
             ->getRepository('WallabagCoreBundle:Entry')
-            ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order);
+            ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since);
 
         $pager->setCurrentPage($page);
         $pager->setMaxPerPage($perPage);
index 4d45e5f51080bf85e049b4f5b90bfc119741bfba..4b607c81671cd749a52ff30a4a28bc4ff4b7118d 100644 (file)
@@ -95,7 +95,7 @@ class EntryRepository extends EntityRepository
      *
      * @return array
      */
-    public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC')
+    public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0)
     {
         $qb = $this->createQueryBuilder('e')
             ->where('e.user =:userId')->setParameter('userId', $userId);
@@ -108,6 +108,10 @@ class EntryRepository extends EntityRepository
             $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
         }
 
+        if ($since >= 0) {
+            $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since)));
+        }
+
         if ('created' === $sort) {
             $qb->orderBy('e.id', $order);
         } elseif ('updated' === $sort) {
index c39cc357763e706a3da6364c53aecfd2421c5c53..f256a7f9fb37f290c1c0d01478a7bb40aec15cda 100644 (file)
@@ -121,6 +121,51 @@ class WallabagRestControllerTest extends WallabagApiTestCase
         );
     }
 
+    public function testGetDatedEntries()
+    {
+        $this->client->request('GET', '/api/entries', ['since' => 1]);
+
+        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
+
+        $content = json_decode($this->client->getResponse()->getContent(), true);
+
+        $this->assertGreaterThanOrEqual(1, count($content));
+        $this->assertNotEmpty($content['_embedded']['items']);
+        $this->assertGreaterThanOrEqual(1, $content['total']);
+        $this->assertEquals(1, $content['page']);
+        $this->assertGreaterThanOrEqual(1, $content['pages']);
+
+        $this->assertTrue(
+            $this->client->getResponse()->headers->contains(
+                'Content-Type',
+                'application/json'
+            )
+        );
+    }
+
+    public function testGetDatedSupEntries()
+    {
+        $future = new \DateTime(date('Y-m-d H:i:s'));
+        $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]);
+
+        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
+
+        $content = json_decode($this->client->getResponse()->getContent(), true);
+
+        $this->assertGreaterThanOrEqual(1, count($content));
+        $this->assertEmpty($content['_embedded']['items']);
+        $this->assertEquals(0, $content['total']);
+        $this->assertEquals(1, $content['page']);
+        $this->assertEquals(1, $content['pages']);
+
+        $this->assertTrue(
+            $this->client->getResponse()->headers->contains(
+                'Content-Type',
+                'application/json'
+            )
+        );
+    }
+
     public function testDeleteEntry()
     {
         $entry = $this->client->getContainer()