aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNicolas LÅ“uillet <nicolas@loeuillet.org>2017-04-24 12:24:17 +0200
committerNicolas LÅ“uillet <nicolas@loeuillet.org>2017-04-24 12:24:17 +0200
commit80299ed282d4f18ef92a79f29f9346b96acde468 (patch)
treeaeac62044b1d21f649ffd2a8f6a469d0491629ee
parentd1fc590211b8dc7360bf5b7ee01c67ccff0577ea (diff)
downloadwallabag-80299ed282d4f18ef92a79f29f9346b96acde468.tar.gz
wallabag-80299ed282d4f18ef92a79f29f9346b96acde468.tar.zst
wallabag-80299ed282d4f18ef92a79f29f9346b96acde468.zip
Added endpoint to handle URL list to add/delete tags
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php85
-rw-r--r--src/Wallabag/ApiBundle/Controller/TagRestController.php4
-rw-r--r--tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php59
3 files changed, 111 insertions, 37 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index fc46e782..5ccaa4ef 100644
--- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -440,17 +440,17 @@ class EntryRestController extends WallabagRestController
440 } 440 }
441 441
442 /** 442 /**
443 * Handles an entries list and add / delete to them some tags. 443 * Handles an entries list delete tags from them.
444 * 444 *
445 * @ApiDoc( 445 * @ApiDoc(
446 * parameters={ 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."} 447 * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...','tags': 'tag1, tag2'}, {'url': 'http://...','tags': 'tag1, tag2'}]", "description"="Urls (as an array) to handle."}
448 * } 448 * }
449 * ) 449 * )
450 * 450 *
451 * @return JsonResponse 451 * @return JsonResponse
452 */ 452 */
453 public function postEntriesTagsListAction(Request $request) 453 public function deleteEntriesTagsListAction(Request $request)
454 { 454 {
455 $this->validateAuthentication(); 455 $this->validateAuthentication();
456 456
@@ -467,32 +467,22 @@ class EntryRestController extends WallabagRestController
467 ); 467 );
468 468
469 $results[$key]['url'] = $element->url; 469 $results[$key]['url'] = $element->url;
470 $results[$key]['action'] = $element->action;
471 $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; 470 $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
472 471
473 $tags = $element->tags; 472 $tags = $element->tags;
474 473
475 if (false !== $entry && !(empty($tags))) { 474 if (false !== $entry && !(empty($tags))) {
476 switch ($element->action) { 475 $tags = explode(',', $tags);
477 case 'delete': 476 foreach ($tags as $label) {
478 $tags = explode(',', $tags); 477 $label = trim($label);
479 foreach ($tags as $label) { 478
480 $label = trim($label); 479 $tag = $this->getDoctrine()
481 480 ->getRepository('WallabagCoreBundle:Tag')
482 $tag = $this->getDoctrine() 481 ->findOneByLabel($label);
483 ->getRepository('WallabagCoreBundle:Tag') 482
484 ->findOneByLabel($label); 483 if (false !== $tag) {
485 484 $entry->removeTag($tag);
486 if (false !== $tag) { 485 }
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 } 486 }
497 487
498 $em = $this->getDoctrine()->getManager(); 488 $em = $this->getDoctrine()->getManager();
@@ -506,4 +496,51 @@ class EntryRestController extends WallabagRestController
506 496
507 return (new JsonResponse())->setJson($json); 497 return (new JsonResponse())->setJson($json);
508 } 498 }
499
500 /**
501 * Handles an entries list and add tags to them.
502 *
503 * @ApiDoc(
504 * parameters={
505 * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...','tags': 'tag1, tag2'}, {'url': 'http://...','tags': 'tag1, tag2'}]", "description"="Urls (as an array) to handle."}
506 * }
507 * )
508 *
509 * @return JsonResponse
510 */
511 public function postEntriesTagsListAction(Request $request)
512 {
513 $this->validateAuthentication();
514
515 $list = json_decode($request->query->get('list', []));
516 $results = [];
517
518 // handle multiple urls
519 if (!empty($list)) {
520 $results = [];
521 foreach ($list as $key => $element) {
522 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
523 $element->url,
524 $this->getUser()->getId()
525 );
526
527 $results[$key]['url'] = $element->url;
528 $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
529
530 $tags = $element->tags;
531
532 if (false !== $entry && !(empty($tags))) {
533 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
534
535 $em = $this->getDoctrine()->getManager();
536 $em->persist($entry);
537 $em->flush();
538 }
539 }
540 }
541
542 $json = $this->get('serializer')->serialize($results, 'json');
543
544 return (new JsonResponse())->setJson($json);
545 }
509} 546}
diff --git a/src/Wallabag/ApiBundle/Controller/TagRestController.php b/src/Wallabag/ApiBundle/Controller/TagRestController.php
index bc6d4e64..47298d7e 100644
--- a/src/Wallabag/ApiBundle/Controller/TagRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/TagRestController.php
@@ -31,7 +31,7 @@ class TagRestController extends WallabagRestController
31 } 31 }
32 32
33 /** 33 /**
34 * Permanently remove one tag from **every** entry. 34 * Permanently remove one tag from **every** entry by passing the Tag label.
35 * 35 *
36 * @ApiDoc( 36 * @ApiDoc(
37 * requirements={ 37 * requirements={
@@ -106,7 +106,7 @@ class TagRestController extends WallabagRestController
106 } 106 }
107 107
108 /** 108 /**
109 * Permanently remove one tag from **every** entry. 109 * Permanently remove one tag from **every** entry by passing the Tag ID.
110 * 110 *
111 * @ApiDoc( 111 * @ApiDoc(
112 * requirements={ 112 * requirements={
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
index 1f5c7a4f..638e8bcd 100644
--- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
@@ -717,32 +717,69 @@ class EntryRestControllerTest extends WallabagApiTestCase
717 717
718 public function testPostEntriesTagsListAction() 718 public function testPostEntriesTagsListAction()
719 { 719 {
720 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
721 ->getRepository('WallabagCoreBundle:Entry')
722 ->findByUrlAndUserId('http://0.0.0.0/entry2', 1);
723
724 $tags = $entry->getTags();
725
726 $this->assertCount(4, $tags);
727
720 $list = [ 728 $list = [
721 [ 729 [
722 'url' => 'http://0.0.0.0/entry1', 730 'url' => 'http://0.0.0.0/entry2',
723 'tags' => 'foo bar, baz', 731 'tags' => 'new tag 1, new tag 2',
724 'action' => 'delete',
725 ], 732 ],
733 ];
734
735 $this->client->request('POST', '/api/entries/tags/lists?list='.json_encode($list));
736
737 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
738
739 $content = json_decode($this->client->getResponse()->getContent(), true);
740
741 $this->assertInternalType('int', $content[0]['entry']);
742 $this->assertEquals('http://0.0.0.0/entry2', $content[0]['url']);
743
744 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
745 ->getRepository('WallabagCoreBundle:Entry')
746 ->findByUrlAndUserId('http://0.0.0.0/entry2', 1);
747
748 $tags = $entry->getTags();
749 $this->assertCount(6, $tags);
750 }
751
752 public function testDeleteEntriesTagsListAction()
753 {
754 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
755 ->getRepository('WallabagCoreBundle:Entry')
756 ->findByUrlAndUserId('http://0.0.0.0/entry2', 1);
757
758 $tags = $entry->getTags();
759
760 $this->assertCount(6, $tags);
761
762 $list = [
726 [ 763 [
727 'url' => 'http://0.0.0.0/entry2', 764 'url' => 'http://0.0.0.0/entry2',
728 'tags' => 'new tag 1, new tag 2', 765 'tags' => 'new tag 1, new tag 2',
729 'action' => 'add',
730 ], 766 ],
731 ]; 767 ];
732 768
733 $this->client->request('POST', '/api/entries/tags/lists?list='.json_encode($list)); 769 $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list));
734 770
735 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 771 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
736 772
737 $content = json_decode($this->client->getResponse()->getContent(), true); 773 $content = json_decode($this->client->getResponse()->getContent(), true);
738 774
775 $this->assertInternalType('int', $content[0]['entry']);
776 $this->assertEquals('http://0.0.0.0/entry2', $content[0]['url']);
739 777
740 $this->assertFalse($content[0]['entry']); 778 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
741 $this->assertEquals('http://0.0.0.0/entry1', $content[0]['url']); 779 ->getRepository('WallabagCoreBundle:Entry')
742 $this->assertEquals('delete', $content[0]['action']); 780 ->findByUrlAndUserId('http://0.0.0.0/entry2', 1);
743 781
744 $this->assertInternalType('int', $content[1]['entry']); 782 $tags = $entry->getTags();
745 $this->assertEquals('http://0.0.0.0/entry2', $content[1]['url']); 783 $this->assertCount(4, $tags);
746 $this->assertEquals('add', $content[1]['action']);
747 } 784 }
748} 785}