]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Added API endpoint to handle a list of URL
authorNicolas Lœuillet <nicolas@loeuillet.org>
Mon, 24 Apr 2017 08:22:57 +0000 (10:22 +0200)
committerNicolas Lœuillet <nicolas@loeuillet.org>
Mon, 1 May 2017 07:25:34 +0000 (09:25 +0200)
By passing an array, you can add / delete URL in mass (bulk request)

src/Wallabag/ApiBundle/Controller/EntryRestController.php
tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php

index 7590efbb16b5325102ccea35afdae450bd7581b6..3833ce3c8af129c2b17bcec8e18234d54edb0e4d 100644 (file)
@@ -172,6 +172,77 @@ class EntryRestController extends WallabagRestController
             ->exportAs($request->attributes->get('_format'));
     }
 
+    /**
+     * Handles an entries list and create or remove URL.
+     *
+     * @ApiDoc(
+     *       parameters={
+     *          {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...', 'action': 'delete'}, {'url': 'http://...', 'action': 'add'}]", "description"="Urls (as an array) to handle."}
+     *       }
+     * )
+     *
+     * @return JsonResponse
+     */
+    public function postEntriesListAction(Request $request)
+    {
+        $this->validateAuthentication();
+
+        $list = json_decode($request->query->get('list', []));
+        $results = [];
+
+        // handle multiple urls
+        if (!empty($list)) {
+            $results = [];
+            foreach ($list as $key => $element) {
+                $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
+                    $element->url,
+                    $this->getUser()->getId()
+                );
+
+                $results[$key]['url'] = $element->url;
+                $results[$key]['action'] = $element->action;
+
+                switch ($element->action) {
+                    case 'delete':
+                        if (false !== $entry) {
+                            $em = $this->getDoctrine()->getManager();
+                            $em->remove($entry);
+                            $em->flush();
+
+                            // entry deleted, dispatch event about it!
+                            $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
+                        }
+
+                        $results[$key]['entry'] = $entry instanceof Entry ? true : false;
+
+                        break;
+                    case 'add':
+                        if (false === $entry) {
+                            $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
+                                new Entry($this->getUser()),
+                                $element->url
+                            );
+                        }
+
+                        $em = $this->getDoctrine()->getManager();
+                        $em->persist($entry);
+                        $em->flush();
+
+                        $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
+
+                        // entry saved, dispatch event about it!
+                        $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
+
+                        break;
+                }
+            }
+        }
+
+        $json = $this->get('serializer')->serialize($results, 'json');
+
+        return (new JsonResponse())->setJson($json);
+    }
+
     /**
      * Create an entry.
      *
index 19fb51707a0272d1290089e1c6884c081708a9da..c37d08cbcd8b225203ee73bc82629001750877ca 100644 (file)
@@ -766,20 +766,50 @@ class EntryRestControllerTest extends WallabagApiTestCase
             ],
         ];
 
-        $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list));
+        $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
+    }
+
+    public function testPostMassEntriesAction()
+    {
+        $list = [
+            [
+                'url' => 'http://0.0.0.0/entry2',
+                'action' => 'delete',
+            ],
+            [
+                'url' => 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
+                'action' => 'add',
+            ],
+            [
+                'url' => 'http://0.0.0.0/entry3',
+                'action' => 'delete',
+            ],
+            [
+                'url' => 'http://0.0.0.0/entry6',
+                'action' => 'add',
+            ],
+        ];
+
+        $this->client->request('POST', '/api/entries/lists?list='.json_encode($list));
 
         $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
 
         $content = json_decode($this->client->getResponse()->getContent(), true);
 
-        $this->assertInternalType('int', $content[0]['entry']);
-        $this->assertEquals('http://0.0.0.0/entry4', $content[0]['url']);
+        $this->assertTrue($content[0]['entry']);
+        $this->assertEquals('http://0.0.0.0/entry2', $content[0]['url']);
+        $this->assertEquals('delete', $content[0]['action']);
 
-        $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
+        $this->assertInternalType('int', $content[1]['entry']);
+        $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[1]['url']);
+        $this->assertEquals('add', $content[1]['action']);
 
-        $tags = $entry->getTags();
-        $this->assertCount(2, $tags);
+        $this->assertFalse($content[2]['entry']);
+        $this->assertEquals('http://0.0.0.0/entry3', $content[2]['url']);
+        $this->assertEquals('delete', $content[2]['action']);
+
+        $this->assertInternalType('int', $content[3]['entry']);
+        $this->assertEquals('http://0.0.0.0/entry6', $content[3]['url']);
+        $this->assertEquals('add', $content[3]['action']);
     }
 }