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 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
22 * @return JsonResponse
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 = array('total' => $total, 'rows' => $annotationRows);
33 $json = $this->get('serializer')->serialize($annotations, 'json');
35 return (new JsonResponse())->setJson($json);
39 * Creates a new annotation.
41 * @param Request $request
43 * @return JsonResponse
44 * @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
82 * @return JsonResponse
84 public function putAnnotationAction(Annotation
$annotation, Request
$request)
86 $data = json_decode($request->getContent(), true);
88 if (!is_null($data['text'])) {
89 $annotation->setText($data['text']);
92 $em = $this->getDoctrine()->getManager();
95 $json = $this->get('serializer')->serialize($annotation, 'json');
97 return (new JsonResponse())->setJson($json);
101 * Removes an annotation.
103 * @see Wallabag\ApiBundle\Controller\WallabagRestController
105 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
107 * @param Annotation $annotation
108 * @return JsonResponse
110 public function deleteAnnotationAction(Annotation
$annotation)
112 $em = $this->getDoctrine()->getManager();
113 $em->remove($annotation);
116 $json = $this->get('serializer')->serialize($annotation, 'json');
118 return (new JsonResponse())->setJson($json);