3 namespace Wallabag\AnnotationBundle\Controller
;
5 use FOS\RestBundle\Controller\FOSRestController
;
6 use Nelmio\ApiDocBundle\Annotation\ApiDoc
;
7 use Symfony\Component\HttpFoundation\Request
;
8 use Symfony\Component\HttpFoundation\Response
;
9 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter
;
10 use Wallabag\AnnotationBundle\Entity\Annotation
;
11 use Wallabag\CoreBundle\Entity\Entry
;
13 class WallabagAnnotationController
extends FOSRestController
16 * Retrieve annotations for an entry.
20 * @see Wallabag\ApiBundle\Controller\WallabagRestController
24 public function getAnnotationsAction(Entry
$entry)
26 $annotationRows = $this
28 ->getRepository('WallabagAnnotationBundle:Annotation')
29 ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId());
30 $total = count($annotationRows);
31 $annotations = ['total' => $total, 'rows' => $annotationRows];
33 $json = $this->get('serializer')->serialize($annotations, 'json');
35 return $this->renderJsonResponse($json);
39 * Creates a new annotation.
43 * @see Wallabag\ApiBundle\Controller\WallabagRestController
47 public function postAnnotationAction(Request
$request, Entry
$entry)
49 $data = json_decode($request->getContent(), true);
51 $em = $this->getDoctrine()->getManager();
53 $annotation = new Annotation($this->getUser());
55 $annotation->setText($data['text']);
56 if (array_key_exists('quote', $data)) {
57 $annotation->setQuote($data['quote']);
59 if (array_key_exists('ranges', $data)) {
60 $annotation->setRanges($data['ranges']);
63 $annotation->setEntry($entry);
65 $em->persist($annotation);
68 $json = $this->get('serializer')->serialize($annotation, 'json');
70 return $this->renderJsonResponse($json);
74 * Updates an annotation.
76 * @see Wallabag\ApiBundle\Controller\WallabagRestController
78 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
82 public function putAnnotationAction(Annotation
$annotation, Request
$request)
84 $data = json_decode($request->getContent(), true);
86 if (!is_null($data['text'])) {
87 $annotation->setText($data['text']);
90 $em = $this->getDoctrine()->getManager();
93 $json = $this->get('serializer')->serialize($annotation, 'json');
95 return $this->renderJsonResponse($json);
99 * Removes an annotation.
101 * @see Wallabag\ApiBundle\Controller\WallabagRestController
103 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
107 public function deleteAnnotationAction(Annotation
$annotation)
109 $em = $this->getDoctrine()->getManager();
110 $em->remove($annotation);
113 $json = $this->get('serializer')->serialize($annotation, 'json');
115 return $this->renderJsonResponse($json);
119 * Send a JSON Response.
120 * We don't use the Symfony JsonRespone, because it takes an array as parameter instead of a JSON string.
122 * @param string $json
126 private function renderJsonResponse($json, $code = 200)
128 return new Response($json, $code, ['application/json']);