3 namespace Wallabag\AnnotationBundle\Controller
;
5 use FOS\RestBundle\Controller\FOSRestController
;
6 use Symfony\Component\HttpFoundation\Request
;
7 use Symfony\Component\HttpFoundation\Response
;
8 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter
;
9 use Wallabag\AnnotationBundle\Entity\Annotation
;
10 use Wallabag\CoreBundle\Entity\Entry
;
12 class WallabagAnnotationController
extends FOSRestController
15 * Retrieve annotations for an entry.
19 * @see Wallabag\ApiBundle\Controller\WallabagRestController
23 public function getAnnotationsAction(Entry
$entry)
25 $annotationRows = $this
27 ->getRepository('WallabagAnnotationBundle:Annotation')
28 ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId());
29 $total = count($annotationRows);
30 $annotations = ['total' => $total, 'rows' => $annotationRows];
32 $json = $this->get('serializer')->serialize($annotations, 'json');
34 return $this->renderJsonResponse($json);
38 * Creates a new annotation.
42 * @see Wallabag\ApiBundle\Controller\WallabagRestController
46 public function postAnnotationAction(Request
$request, Entry
$entry)
48 $data = json_decode($request->getContent(), true);
50 $em = $this->getDoctrine()->getManager();
52 $annotation = new Annotation($this->getUser());
54 $annotation->setText($data['text']);
55 if (array_key_exists('quote', $data)) {
56 $annotation->setQuote($data['quote']);
58 if (array_key_exists('ranges', $data)) {
59 $annotation->setRanges($data['ranges']);
62 $annotation->setEntry($entry);
64 $em->persist($annotation);
67 $json = $this->get('serializer')->serialize($annotation, 'json');
69 return $this->renderJsonResponse($json);
73 * Updates an annotation.
75 * @see Wallabag\ApiBundle\Controller\WallabagRestController
77 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
81 public function putAnnotationAction(Annotation
$annotation, Request
$request)
83 $data = json_decode($request->getContent(), true);
85 if (!is_null($data['text'])) {
86 $annotation->setText($data['text']);
89 $em = $this->getDoctrine()->getManager();
92 $json = $this->get('serializer')->serialize($annotation, 'json');
94 return $this->renderJsonResponse($json);
98 * Removes an annotation.
100 * @see Wallabag\ApiBundle\Controller\WallabagRestController
102 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
106 public function deleteAnnotationAction(Annotation
$annotation)
108 $em = $this->getDoctrine()->getManager();
109 $em->remove($annotation);
112 $json = $this->get('serializer')->serialize($annotation, 'json');
114 return $this->renderJsonResponse($json);
118 * Send a JSON Response.
119 * We don't use the Symfony JsonRespone, because it takes an array as parameter instead of a JSON string.
121 * @param string $json
125 private function renderJsonResponse($json, $code = 200)
127 return new Response($json, $code, ['application/json']);