]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php
move code
[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
TC
7use Symfony\Component\HttpFoundation\Request;
8use Symfony\Component\HttpFoundation\Response;
9use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
4dc87223 10use Wallabag\AnnotationBundle\Entity\Annotation;
f38e03dc
TC
11use Wallabag\CoreBundle\Entity\Entry;
12
4dc87223 13class WallabagAnnotationController extends FOSRestController
f38e03dc
TC
14{
15 /**
4dc87223 16 * Retrieve annotations for an entry.
f38e03dc 17 *
351eb8d9
TC
18 * @param Entry $entry
19 *
20 * @see Wallabag\ApiBundle\Controller\WallabagRestController
f38e03dc 21 *
1eea248b 22 * @return JsonResponse
f38e03dc
TC
23 */
24 public function getAnnotationsAction(Entry $entry)
25 {
4dc87223 26 $annotationRows = $this
1eea248b
TC
27 ->getDoctrine()
28 ->getRepository('WallabagAnnotationBundle:Annotation')
29 ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId());
4dc87223 30 $total = count($annotationRows);
1eea248b 31 $annotations = array('total' => $total, 'rows' => $annotationRows);
f38e03dc 32
4dc87223 33 $json = $this->get('serializer')->serialize($annotations, 'json');
f38e03dc 34
1eea248b 35 return (new JsonResponse())->setJson($json);
f38e03dc
TC
36 }
37
38 /**
4dc87223 39 * Creates a new annotation.
f38e03dc 40 *
1eea248b 41 * @param Request $request
f38e03dc 42 * @param Entry $entry
1eea248b 43 * @return JsonResponse
351eb8d9 44 * @see Wallabag\ApiBundle\Controller\WallabagRestController
f38e03dc 45 *
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
TC
80 * @param Annotation $annotation
81 * @param Request $request
82 * @return JsonResponse
f38e03dc 83 */
4dc87223 84 public function putAnnotationAction(Annotation $annotation, Request $request)
f38e03dc
TC
85 {
86 $data = json_decode($request->getContent(), true);
87
88 if (!is_null($data['text'])) {
4dc87223 89 $annotation->setText($data['text']);
f38e03dc
TC
90 }
91
92 $em = $this->getDoctrine()->getManager();
93 $em->flush();
94
4dc87223 95 $json = $this->get('serializer')->serialize($annotation, 'json');
f38e03dc 96
1eea248b 97 return (new JsonResponse())->setJson($json);
f38e03dc
TC
98 }
99
100 /**
4dc87223 101 * Removes an annotation.
f38e03dc 102 *
351eb8d9 103 * @see Wallabag\ApiBundle\Controller\WallabagRestController
f38e03dc 104 *
4dc87223 105 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
f38e03dc 106 *
1eea248b
TC
107 * @param Annotation $annotation
108 * @return JsonResponse
f38e03dc 109 */
4dc87223 110 public function deleteAnnotationAction(Annotation $annotation)
f38e03dc
TC
111 {
112 $em = $this->getDoctrine()->getManager();
4dc87223 113 $em->remove($annotation);
f38e03dc
TC
114 $em->flush();
115
4dc87223 116 $json = $this->get('serializer')->serialize($annotation, 'json');
f38e03dc 117
1eea248b 118 return (new JsonResponse())->setJson($json);
f38e03dc
TC
119 }
120}