aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ApiBundle/Controller
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller')
-rw-r--r--src/Wallabag/ApiBundle/Controller/WallabagRestController.php132
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;
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()