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.
21 * @see Wallabag\ApiBundle\Controller\WallabagRestController
23 * @return JsonResponse
25 public function getAnnotationsAction(Entry
$entry)
27 $annotationRows = $this
29 ->getRepository('WallabagAnnotationBundle:Annotation')
30 ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId());
31 $total = count($annotationRows);
32 $annotations = ['total' => $total, 'rows' => $annotationRows];
34 $json = $this->get('jms_serializer')->serialize($annotations, 'json');
36 return (new JsonResponse())->setJson($json);
40 * Creates a new annotation.
42 * @param Request $request
45 * @return JsonResponse
47 * @see Wallabag\ApiBundle\Controller\WallabagRestController
49 public function postAnnotationAction(Request
$request, Entry
$entry)
51 $data = json_decode($request->getContent(), true);
53 $em = $this->getDoctrine()->getManager();
54 $annotation = new Annotation($this->getUser());
55 $annotation->setEntry($entry);
57 $form = $this->get('form.factory')->createNamed('', NewAnnotationType
::class, $annotation, [
58 'csrf_protection' => false,
59 'allow_extra_fields' => true,
63 if ($form->isValid()) {
64 $em->persist($annotation);
67 $json = $this->get('jms_serializer')->serialize($annotation, 'json');
69 return JsonResponse
::fromJsonString($json);
76 * Updates an annotation.
78 * @see Wallabag\ApiBundle\Controller\WallabagRestController
80 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
82 * @param Annotation $annotation
83 * @param Request $request
85 * @return JsonResponse
87 public function putAnnotationAction(Annotation
$annotation, Request
$request)
89 $data = json_decode($request->getContent(), true);
91 $form = $this->get('form.factory')->createNamed('', EditAnnotationType
::class, $annotation, [
92 'csrf_protection' => false,
93 'allow_extra_fields' => true,
97 if ($form->isValid()) {
98 $em = $this->getDoctrine()->getManager();
99 $em->persist($annotation);
102 $json = $this->get('jms_serializer')->serialize($annotation, 'json');
104 return JsonResponse
::fromJsonString($json);
111 * Removes an annotation.
113 * @see Wallabag\ApiBundle\Controller\WallabagRestController
115 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
117 * @param Annotation $annotation
119 * @return JsonResponse
121 public function deleteAnnotationAction(Annotation
$annotation)
123 $em = $this->getDoctrine()->getManager();
124 $em->remove($annotation);
127 $json = $this->get('jms_serializer')->serialize($annotation, 'json');
129 return (new JsonResponse())->setJson($json);