diff options
author | Thomas Citharel <tcit@tcit.fr> | 2016-03-18 16:41:23 +0100 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-10-22 09:06:07 +0200 |
commit | 351eb8d97ea1520f87ae762faf297083a716c945 (patch) | |
tree | 98a213975b0fbf30f59025d2f6926005df8c4c4e /src/Wallabag/ApiBundle/Controller/WallabagRestController.php | |
parent | e8331dd9e7772a396bb211459eca4a682c8e947e (diff) | |
download | wallabag-351eb8d97ea1520f87ae762faf297083a716c945.tar.gz wallabag-351eb8d97ea1520f87ae762faf297083a716c945.tar.zst wallabag-351eb8d97ea1520f87ae762faf297083a716c945.zip |
bring annotations to API
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller/WallabagRestController.php')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/WallabagRestController.php | 132 |
1 files changed, 132 insertions, 0 deletions
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; | |||
6 | use Hateoas\Configuration\Route as HateoasRoute; | 6 | use Hateoas\Configuration\Route as HateoasRoute; |
7 | use Hateoas\Representation\Factory\PagerfantaFactory; | 7 | use Hateoas\Representation\Factory\PagerfantaFactory; |
8 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; | 8 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
9 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; | ||
9 | use Symfony\Component\HttpFoundation\Request; | 10 | use Symfony\Component\HttpFoundation\Request; |
10 | use Symfony\Component\HttpFoundation\JsonResponse; | 11 | use Symfony\Component\HttpFoundation\JsonResponse; |
11 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | 12 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
12 | use Symfony\Component\Security\Core\Exception\AccessDeniedException; | 13 | use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
13 | use Wallabag\CoreBundle\Entity\Entry; | 14 | use Wallabag\CoreBundle\Entity\Entry; |
14 | use Wallabag\CoreBundle\Entity\Tag; | 15 | use Wallabag\CoreBundle\Entity\Tag; |
16 | use Wallabag\AnnotationBundle\Entity\Annotation; | ||
15 | 17 | ||
16 | class WallabagRestController extends FOSRestController | 18 | class 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() |