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