]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Splitted the endpoint in two
authorNicolas Lœuillet <nicolas@loeuillet.org>
Mon, 24 Apr 2017 09:31:00 +0000 (11:31 +0200)
committerNicolas Lœuillet <nicolas@loeuillet.org>
Mon, 1 May 2017 07:26:12 +0000 (09:26 +0200)
src/Wallabag/ApiBundle/Controller/EntryRestController.php
tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php

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