3 namespace Wallabag\AnnotationBundle\Controller
;
5 use FOS\RestBundle\Controller\FOSRestController
;
6 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter
;
7 use Symfony\Component\HttpFoundation\JsonResponse
;
8 use Symfony\Component\HttpFoundation\Request
;
9 use Wallabag\AnnotationBundle\Entity\Annotation
;
10 use Wallabag\AnnotationBundle\Form\EditAnnotationType
;
11 use Wallabag\AnnotationBundle\Form\NewAnnotationType
;
12 use Wallabag\CoreBundle\Entity\Entry
;
14 class WallabagAnnotationController
extends FOSRestController
17 * 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('jms_serializer')->serialize($annotations, 'json');
34 return (new JsonResponse())->setJson($json);
38 * Creates a new annotation.
40 * @return JsonResponse
42 * @see Wallabag\ApiBundle\Controller\WallabagRestController
44 public function postAnnotationAction(Request
$request, Entry
$entry)
46 $data = json_decode($request->getContent(), true);
48 $em = $this->getDoctrine()->getManager();
49 $annotation = new Annotation($this->getUser());
50 $annotation->setEntry($entry);
52 $form = $this->get('form.factory')->createNamed('', NewAnnotationType
::class, $annotation, [
53 'csrf_protection' => false,
54 'allow_extra_fields' => true,
58 if ($form->isValid()) {
59 $em->persist($annotation);
62 $json = $this->get('jms_serializer')->serialize($annotation, 'json');
64 return JsonResponse
::fromJsonString($json);
71 * Updates an annotation.
73 * @see Wallabag\ApiBundle\Controller\WallabagRestController
75 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
77 * @return JsonResponse
79 public function putAnnotationAction(Annotation
$annotation, Request
$request)
81 $data = json_decode($request->getContent(), true);
83 $form = $this->get('form.factory')->createNamed('', EditAnnotationType
::class, $annotation, [
84 'csrf_protection' => false,
85 'allow_extra_fields' => true,
89 if ($form->isValid()) {
90 $em = $this->getDoctrine()->getManager();
91 $em->persist($annotation);
94 $json = $this->get('jms_serializer')->serialize($annotation, 'json');
96 return JsonResponse
::fromJsonString($json);
103 * Removes an annotation.
105 * @see Wallabag\ApiBundle\Controller\WallabagRestController
107 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
109 * @return JsonResponse
111 public function deleteAnnotationAction(Annotation
$annotation)
113 $em = $this->getDoctrine()->getManager();
114 $em->remove($annotation);
117 $json = $this->get('jms_serializer')->serialize($annotation, 'json');
119 return (new JsonResponse())->setJson($json);