diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2017-04-24 11:12:41 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2017-04-24 11:12:41 +0200 |
commit | d1fc590211b8dc7360bf5b7ee01c67ccff0577ea (patch) | |
tree | ec5466ed672b5930d68867201568f8e4ad5e1250 | |
parent | b8acf672c0b7563d56841172d42429d1c57f0004 (diff) | |
download | wallabag-d1fc590211b8dc7360bf5b7ee01c67ccff0577ea.tar.gz wallabag-d1fc590211b8dc7360bf5b7ee01c67ccff0577ea.tar.zst wallabag-d1fc590211b8dc7360bf5b7ee01c67ccff0577ea.zip |
Added API endpoint to handle a list of URL and to add/delete tags
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/EntryRestController.php | 68 | ||||
-rw-r--r-- | tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php | 31 |
2 files changed, 99 insertions, 0 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index af5f7603..fc46e782 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -438,4 +438,72 @@ class EntryRestController extends WallabagRestController | |||
438 | 438 | ||
439 | return (new JsonResponse())->setJson($json); | 439 | return (new JsonResponse())->setJson($json); |
440 | } | 440 | } |
441 | |||
442 | /** | ||
443 | * Handles an entries list and add / delete to them some tags. | ||
444 | * | ||
445 | * @ApiDoc( | ||
446 | * parameters={ | ||
447 | * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...','tags': 'tag1, tag2','action': 'delete'}, {'url': 'http://...','tags': 'tag1, tag2','action': 'add'}]", "description"="Urls (as an array) to handle."} | ||
448 | * } | ||
449 | * ) | ||
450 | * | ||
451 | * @return JsonResponse | ||
452 | */ | ||
453 | public function postEntriesTagsListAction(Request $request) | ||
454 | { | ||
455 | $this->validateAuthentication(); | ||
456 | |||
457 | $list = json_decode($request->query->get('list', [])); | ||
458 | $results = []; | ||
459 | |||
460 | // handle multiple urls | ||
461 | if (!empty($list)) { | ||
462 | $results = []; | ||
463 | foreach ($list as $key => $element) { | ||
464 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | ||
465 | $element->url, | ||
466 | $this->getUser()->getId() | ||
467 | ); | ||
468 | |||
469 | $results[$key]['url'] = $element->url; | ||
470 | $results[$key]['action'] = $element->action; | ||
471 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | ||
472 | |||
473 | $tags = $element->tags; | ||
474 | |||
475 | if (false !== $entry && !(empty($tags))) { | ||
476 | switch ($element->action) { | ||
477 | case 'delete': | ||
478 | $tags = explode(',', $tags); | ||
479 | foreach ($tags as $label) { | ||
480 | $label = trim($label); | ||
481 | |||
482 | $tag = $this->getDoctrine() | ||
483 | ->getRepository('WallabagCoreBundle:Tag') | ||
484 | ->findOneByLabel($label); | ||
485 | |||
486 | if (false !== $tag) { | ||
487 | $entry->removeTag($tag); | ||
488 | } | ||
489 | } | ||
490 | |||
491 | break; | ||
492 | case 'add': | ||
493 | $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); | ||
494 | |||
495 | break; | ||
496 | } | ||
497 | |||
498 | $em = $this->getDoctrine()->getManager(); | ||
499 | $em->persist($entry); | ||
500 | $em->flush(); | ||
501 | } | ||
502 | } | ||
503 | } | ||
504 | |||
505 | $json = $this->get('serializer')->serialize($results, 'json'); | ||
506 | |||
507 | return (new JsonResponse())->setJson($json); | ||
508 | } | ||
441 | } | 509 | } |
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index dc5160c7..1f5c7a4f 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php | |||
@@ -714,4 +714,35 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
714 | 714 | ||
715 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | 715 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
716 | } | 716 | } |
717 | |||
718 | public function testPostEntriesTagsListAction() | ||
719 | { | ||
720 | $list = [ | ||
721 | [ | ||
722 | 'url' => 'http://0.0.0.0/entry1', | ||
723 | 'tags' => 'foo bar, baz', | ||
724 | 'action' => 'delete', | ||
725 | ], | ||
726 | [ | ||
727 | 'url' => 'http://0.0.0.0/entry2', | ||
728 | 'tags' => 'new tag 1, new tag 2', | ||
729 | 'action' => 'add', | ||
730 | ], | ||
731 | ]; | ||
732 | |||
733 | $this->client->request('POST', '/api/entries/tags/lists?list='.json_encode($list)); | ||
734 | |||
735 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | ||
736 | |||
737 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
738 | |||
739 | |||
740 | $this->assertFalse($content[0]['entry']); | ||
741 | $this->assertEquals('http://0.0.0.0/entry1', $content[0]['url']); | ||
742 | $this->assertEquals('delete', $content[0]['action']); | ||
743 | |||
744 | $this->assertInternalType('int', $content[1]['entry']); | ||
745 | $this->assertEquals('http://0.0.0.0/entry2', $content[1]['url']); | ||
746 | $this->assertEquals('add', $content[1]['action']); | ||
747 | } | ||
717 | } | 748 | } |