]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php
bring annotations to API
[github/wallabag/wallabag.git] / src / Wallabag / AnnotationBundle / Controller / WallabagAnnotationController.php
1 <?php
2
3 namespace Wallabag\AnnotationBundle\Controller;
4
5 use FOS\RestBundle\Controller\FOSRestController;
6 use Nelmio\ApiDocBundle\Annotation\ApiDoc;
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;
12
13 class WallabagAnnotationController extends FOSRestController
14 {
15 /**
16 * Retrieve annotations for an entry.
17 *
18 * @param Entry $entry
19 *
20 * @see Wallabag\ApiBundle\Controller\WallabagRestController
21 *
22 * @return Response
23 */
24 public function getAnnotationsAction(Entry $entry)
25 {
26 $annotationRows = $this
27 ->getDoctrine()
28 ->getRepository('WallabagAnnotationBundle:Annotation')
29 ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId());
30 $total = count($annotationRows);
31 $annotations = ['total' => $total, 'rows' => $annotationRows];
32
33 $json = $this->get('serializer')->serialize($annotations, 'json');
34
35 return $this->renderJsonResponse($json);
36 }
37
38 /**
39 * Creates a new annotation.
40 *
41 * @param Entry $entry
42 *
43 * @see Wallabag\ApiBundle\Controller\WallabagRestController
44 *
45 * @return Response
46 */
47 public function postAnnotationAction(Request $request, Entry $entry)
48 {
49 $data = json_decode($request->getContent(), true);
50
51 $em = $this->getDoctrine()->getManager();
52
53 $annotation = new Annotation($this->getUser());
54
55 $annotation->setText($data['text']);
56 if (array_key_exists('quote', $data)) {
57 $annotation->setQuote($data['quote']);
58 }
59 if (array_key_exists('ranges', $data)) {
60 $annotation->setRanges($data['ranges']);
61 }
62
63 $annotation->setEntry($entry);
64
65 $em->persist($annotation);
66 $em->flush();
67
68 $json = $this->get('serializer')->serialize($annotation, 'json');
69
70 return $this->renderJsonResponse($json);
71 }
72
73 /**
74 * Updates an annotation.
75 *
76 * @see Wallabag\ApiBundle\Controller\WallabagRestController
77 *
78 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
79 *
80 * @return Response
81 */
82 public function putAnnotationAction(Annotation $annotation, Request $request)
83 {
84 $data = json_decode($request->getContent(), true);
85
86 if (!is_null($data['text'])) {
87 $annotation->setText($data['text']);
88 }
89
90 $em = $this->getDoctrine()->getManager();
91 $em->flush();
92
93 $json = $this->get('serializer')->serialize($annotation, 'json');
94
95 return $this->renderJsonResponse($json);
96 }
97
98 /**
99 * Removes an annotation.
100 *
101 * @see Wallabag\ApiBundle\Controller\WallabagRestController
102 *
103 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
104 *
105 * @return Response
106 */
107 public function deleteAnnotationAction(Annotation $annotation)
108 {
109 $em = $this->getDoctrine()->getManager();
110 $em->remove($annotation);
111 $em->flush();
112
113 $json = $this->get('serializer')->serialize($annotation, 'json');
114
115 return $this->renderJsonResponse($json);
116 }
117
118 /**
119 * Send a JSON Response.
120 * We don't use the Symfony JsonRespone, because it takes an array as parameter instead of a JSON string.
121 *
122 * @param string $json
123 *
124 * @return Response
125 */
126 private function renderJsonResponse($json, $code = 200)
127 {
128 return new Response($json, $code, ['application/json']);
129 }
130 }