]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php
Merge remote-tracking branch 'origin/master' into 2.2
[github/wallabag/wallabag.git] / src / Wallabag / AnnotationBundle / Controller / WallabagAnnotationController.php
CommitLineData
f38e03dc
TC
1<?php
2
4dc87223 3namespace Wallabag\AnnotationBundle\Controller;
f38e03dc
TC
4
5use FOS\RestBundle\Controller\FOSRestController;
1eea248b 6use Symfony\Component\HttpFoundation\JsonResponse;
f38e03dc 7use Symfony\Component\HttpFoundation\Request;
f38e03dc 8use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
4dc87223 9use Wallabag\AnnotationBundle\Entity\Annotation;
f38e03dc
TC
10use Wallabag\CoreBundle\Entity\Entry;
11
4dc87223 12class WallabagAnnotationController extends FOSRestController
f38e03dc
TC
13{
14 /**
4dc87223 15 * Retrieve annotations for an entry.
f38e03dc 16 *
351eb8d9
TC
17 * @param Entry $entry
18 *
19 * @see Wallabag\ApiBundle\Controller\WallabagRestController
f38e03dc 20 *
1eea248b 21 * @return JsonResponse
f38e03dc
TC
22 */
23 public function getAnnotationsAction(Entry $entry)
24 {
4dc87223 25 $annotationRows = $this
1eea248b
TC
26 ->getDoctrine()
27 ->getRepository('WallabagAnnotationBundle:Annotation')
28 ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId());
4dc87223 29 $total = count($annotationRows);
0c271b9e 30 $annotations = ['total' => $total, 'rows' => $annotationRows];
f38e03dc 31
4dc87223 32 $json = $this->get('serializer')->serialize($annotations, 'json');
f38e03dc 33
1eea248b 34 return (new JsonResponse())->setJson($json);
f38e03dc
TC
35 }
36
37 /**
4dc87223 38 * Creates a new annotation.
f38e03dc 39 *
1eea248b 40 * @param Request $request
b1e92f8c
TC
41 * @param Entry $entry
42 *
1eea248b 43 * @return JsonResponse
f38e03dc 44 *
b1e92f8c 45 * @see Wallabag\ApiBundle\Controller\WallabagRestController
f38e03dc
TC
46 */
47 public function postAnnotationAction(Request $request, Entry $entry)
48 {
49 $data = json_decode($request->getContent(), true);
50
51 $em = $this->getDoctrine()->getManager();
52
4dc87223 53 $annotation = new Annotation($this->getUser());
f38e03dc 54
4dc87223 55 $annotation->setText($data['text']);
f38e03dc 56 if (array_key_exists('quote', $data)) {
4dc87223 57 $annotation->setQuote($data['quote']);
f38e03dc
TC
58 }
59 if (array_key_exists('ranges', $data)) {
4dc87223 60 $annotation->setRanges($data['ranges']);
f38e03dc
TC
61 }
62
4dc87223 63 $annotation->setEntry($entry);
f38e03dc 64
4dc87223 65 $em->persist($annotation);
f38e03dc
TC
66 $em->flush();
67
4dc87223 68 $json = $this->get('serializer')->serialize($annotation, 'json');
f38e03dc 69
1eea248b 70 return (new JsonResponse())->setJson($json);
f38e03dc
TC
71 }
72
73 /**
4dc87223 74 * Updates an annotation.
f38e03dc 75 *
351eb8d9 76 * @see Wallabag\ApiBundle\Controller\WallabagRestController
f38e03dc 77 *
4dc87223 78 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
f38e03dc 79 *
1eea248b 80 * @param Annotation $annotation
b1e92f8c
TC
81 * @param Request $request
82 *
1eea248b 83 * @return JsonResponse
f38e03dc 84 */
4dc87223 85 public function putAnnotationAction(Annotation $annotation, Request $request)
f38e03dc
TC
86 {
87 $data = json_decode($request->getContent(), true);
88
89 if (!is_null($data['text'])) {
4dc87223 90 $annotation->setText($data['text']);
f38e03dc
TC
91 }
92
93 $em = $this->getDoctrine()->getManager();
94 $em->flush();
95
4dc87223 96 $json = $this->get('serializer')->serialize($annotation, 'json');
f38e03dc 97
1eea248b 98 return (new JsonResponse())->setJson($json);
f38e03dc
TC
99 }
100
101 /**
4dc87223 102 * Removes an annotation.
f38e03dc 103 *
351eb8d9 104 * @see Wallabag\ApiBundle\Controller\WallabagRestController
f38e03dc 105 *
4dc87223 106 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
f38e03dc 107 *
1eea248b 108 * @param Annotation $annotation
b1e92f8c 109 *
1eea248b 110 * @return JsonResponse
f38e03dc 111 */
4dc87223 112 public function deleteAnnotationAction(Annotation $annotation)
f38e03dc
TC
113 {
114 $em = $this->getDoctrine()->getManager();
4dc87223 115 $em->remove($annotation);
f38e03dc
TC
116 $em->flush();
117
4dc87223 118 $json = $this->get('serializer')->serialize($annotation, 'json');
f38e03dc 119
1eea248b 120 return (new JsonResponse())->setJson($json);
f38e03dc
TC
121 }
122}