3 namespace Wallabag\AnnotationBundle\Controller
;
5 use FOS\RestBundle\Controller\FOSRestController
;
6 use Symfony\Component\HttpFoundation\JsonResponse
;
7 use Symfony\Component\HttpFoundation\Request
;
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
21 * @return JsonResponse
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 (new JsonResponse())->setJson($json);
38 * Creates a new annotation.
40 * @param Request $request
43 * @return JsonResponse
45 * @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 (new JsonResponse())->setJson($json);
74 * Updates an annotation.
76 * @see Wallabag\ApiBundle\Controller\WallabagRestController
78 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
80 * @param Annotation $annotation
81 * @param Request $request
83 * @return JsonResponse
85 public function putAnnotationAction(Annotation
$annotation, Request
$request)
87 $data = json_decode($request->getContent(), true);
89 if (!is_null($data['text'])) {
90 $annotation->setText($data['text']);
93 $em = $this->getDoctrine()->getManager();
96 $json = $this->get('serializer')->serialize($annotation, 'json');
98 return (new JsonResponse())->setJson($json);
102 * Removes an annotation.
104 * @see Wallabag\ApiBundle\Controller\WallabagRestController
106 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
108 * @param Annotation $annotation
110 * @return JsonResponse
112 public function deleteAnnotationAction(Annotation
$annotation)
114 $em = $this->getDoctrine()->getManager();
115 $em->remove($annotation);
118 $json = $this->get('serializer')->serialize($annotation, 'json');
120 return (new JsonResponse())->setJson($json);