aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorThomas Citharel <tcit@tcit.fr>2016-03-18 16:41:23 +0100
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-10-22 09:06:07 +0200
commit351eb8d97ea1520f87ae762faf297083a716c945 (patch)
tree98a213975b0fbf30f59025d2f6926005df8c4c4e
parente8331dd9e7772a396bb211459eca4a682c8e947e (diff)
downloadwallabag-351eb8d97ea1520f87ae762faf297083a716c945.tar.gz
wallabag-351eb8d97ea1520f87ae762faf297083a716c945.tar.zst
wallabag-351eb8d97ea1520f87ae762faf297083a716c945.zip
bring annotations to API
-rw-r--r--src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php28
-rw-r--r--src/Wallabag/ApiBundle/Controller/WallabagRestController.php132
2 files changed, 138 insertions, 22 deletions
diff --git a/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php b/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php
index ad083e31..80ac0035 100644
--- a/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php
+++ b/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php
@@ -15,11 +15,9 @@ class WallabagAnnotationController extends FOSRestController
15 /** 15 /**
16 * Retrieve annotations for an entry. 16 * Retrieve annotations for an entry.
17 * 17 *
18 * @ApiDoc( 18 * @param Entry $entry
19 * requirements={ 19 *
20 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} 20 * @see Wallabag\ApiBundle\Controller\WallabagRestController
21 * }
22 * )
23 * 21 *
24 * @return Response 22 * @return Response
25 */ 23 */
@@ -42,13 +40,7 @@ class WallabagAnnotationController extends FOSRestController
42 * 40 *
43 * @param Entry $entry 41 * @param Entry $entry
44 * 42 *
45 * @ApiDoc( 43 * @see Wallabag\ApiBundle\Controller\WallabagRestController
46 * requirements={
47 * {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"},
48 * {"name"="quote", "dataType"="string", "required"=false, "description"="Optional, quote for the annotation"},
49 * {"name"="text", "dataType"="string", "required"=true, "description"=""},
50 * }
51 * )
52 * 44 *
53 * @return Response 45 * @return Response
54 */ 46 */
@@ -81,11 +73,7 @@ class WallabagAnnotationController extends FOSRestController
81 /** 73 /**
82 * Updates an annotation. 74 * Updates an annotation.
83 * 75 *
84 * @ApiDoc( 76 * @see Wallabag\ApiBundle\Controller\WallabagRestController
85 * requirements={
86 * {"name"="annotation", "dataType"="string", "requirement"="\w+", "description"="The annotation ID"}
87 * }
88 * )
89 * 77 *
90 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") 78 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
91 * 79 *
@@ -110,11 +98,7 @@ class WallabagAnnotationController extends FOSRestController
110 /** 98 /**
111 * Removes an annotation. 99 * Removes an annotation.
112 * 100 *
113 * @ApiDoc( 101 * @see Wallabag\ApiBundle\Controller\WallabagRestController
114 * requirements={
115 * {"name"="annotation", "dataType"="string", "requirement"="\w+", "description"="The annotation ID"}
116 * }
117 * )
118 * 102 *
119 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") 103 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
120 * 104 *
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
index 3437bb9b..6275afa0 100644
--- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
@@ -6,12 +6,14 @@ use FOS\RestBundle\Controller\FOSRestController;
6use Hateoas\Configuration\Route as HateoasRoute; 6use Hateoas\Configuration\Route as HateoasRoute;
7use Hateoas\Representation\Factory\PagerfantaFactory; 7use Hateoas\Representation\Factory\PagerfantaFactory;
8use Nelmio\ApiDocBundle\Annotation\ApiDoc; 8use Nelmio\ApiDocBundle\Annotation\ApiDoc;
9use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
9use Symfony\Component\HttpFoundation\Request; 10use Symfony\Component\HttpFoundation\Request;
10use Symfony\Component\HttpFoundation\JsonResponse; 11use Symfony\Component\HttpFoundation\JsonResponse;
11use Symfony\Component\Routing\Generator\UrlGeneratorInterface; 12use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
12use Symfony\Component\Security\Core\Exception\AccessDeniedException; 13use Symfony\Component\Security\Core\Exception\AccessDeniedException;
13use Wallabag\CoreBundle\Entity\Entry; 14use Wallabag\CoreBundle\Entity\Entry;
14use Wallabag\CoreBundle\Entity\Tag; 15use Wallabag\CoreBundle\Entity\Tag;
16use Wallabag\AnnotationBundle\Entity\Annotation;
15 17
16class WallabagRestController extends FOSRestController 18class WallabagRestController extends FOSRestController
17{ 19{
@@ -518,6 +520,136 @@ class WallabagRestController extends FOSRestController
518 } 520 }
519 521
520 /** 522 /**
523 * Retrieve annotations for an entry.
524 *
525 * @ApiDoc(
526 * requirements={
527 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
528 * }
529 * )
530 *
531 * @return Response
532 */
533 public function getAnnotationsAction(Entry $entry)
534 {
535
536 $this->validateAuthentication();
537
538 $annotationRows = $this
539 ->getDoctrine()
540 ->getRepository('WallabagAnnotationBundle:Annotation')
541 ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId());
542 $total = count($annotationRows);
543 $annotations = array('total' => $total, 'rows' => $annotationRows);
544
545 $json = $this->get('serializer')->serialize($annotations, 'json');
546
547 return $this->renderJsonResponse($json);
548 }
549
550 /**
551 * Creates a new annotation.
552 *
553 * @param Entry $entry
554 *
555 * @ApiDoc(
556 * requirements={
557 * {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"},
558 * {"name"="quote", "dataType"="string", "required"=false, "description"="Optional, quote for the annotation"},
559 * {"name"="text", "dataType"="string", "required"=true, "description"=""},
560 * }
561 * )
562 *
563 * @return Response
564 */
565 public function postAnnotationAction(Request $request, Entry $entry)
566 {
567 $this->validateAuthentication();
568
569 $data = json_decode($request->getContent(), true);
570
571 $em = $this->getDoctrine()->getManager();
572
573 $annotation = new Annotation($this->getUser());
574
575 $annotation->setText($data['text']);
576 if (array_key_exists('quote', $data)) {
577 $annotation->setQuote($data['quote']);
578 }
579 if (array_key_exists('ranges', $data)) {
580 $annotation->setRanges($data['ranges']);
581 }
582
583 $annotation->setEntry($entry);
584
585 $em->persist($annotation);
586 $em->flush();
587
588 $json = $this->get('serializer')->serialize($annotation, 'json');
589
590 return $this->renderJsonResponse($json);
591 }
592
593 /**
594 * Updates an annotation.
595 *
596 * @ApiDoc(
597 * requirements={
598 * {"name"="annotation", "dataType"="string", "requirement"="\w+", "description"="The annotation ID"}
599 * }
600 * )
601 *
602 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
603 *
604 * @return Response
605 */
606 public function putAnnotationAction(Annotation $annotation, Request $request)
607 {
608
609 $this->validateAuthentication();
610
611 $data = json_decode($request->getContent(), true);
612
613 if (!is_null($data['text'])) {
614 $annotation->setText($data['text']);
615 }
616
617 $em = $this->getDoctrine()->getManager();
618 $em->flush();
619
620 $json = $this->get('serializer')->serialize($annotation, 'json');
621
622 return $this->renderJsonResponse($json);
623 }
624
625 /**
626 * Removes an annotation.
627 *
628 * @ApiDoc(
629 * requirements={
630 * {"name"="annotation", "dataType"="string", "requirement"="\w+", "description"="The annotation ID"}
631 * }
632 * )
633 *
634 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
635 *
636 * @return Response
637 */
638 public function deleteAnnotationAction(Annotation $annotation)
639 {
640
641 $this->validateAuthentication();
642
643 $em = $this->getDoctrine()->getManager();
644 $em->remove($annotation);
645 $em->flush();
646
647 $json = $this->get('serializer')->serialize($annotation, 'json');
648
649 return $this->renderJsonResponse($json);
650 }
651
652 /**
521 * Retrieve version number. 653 * Retrieve version number.
522 * 654 *
523 * @ApiDoc() 655 * @ApiDoc()