aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ApiBundle
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 /src/Wallabag/ApiBundle
parentd1fc590211b8dc7360bf5b7ee01c67ccff0577ea (diff)
downloadwallabag-80299ed282d4f18ef92a79f29f9346b96acde468.tar.gz
wallabag-80299ed282d4f18ef92a79f29f9346b96acde468.tar.zst
wallabag-80299ed282d4f18ef92a79f29f9346b96acde468.zip
Added endpoint to handle URL list to add/delete tags
Diffstat (limited to 'src/Wallabag/ApiBundle')
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php85
-rw-r--r--src/Wallabag/ApiBundle/Controller/TagRestController.php4
2 files changed, 63 insertions, 26 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={