]>
Commit | Line | Data |
---|---|---|
f38e03dc TC |
1 | <?php |
2 | ||
4dc87223 | 3 | namespace Wallabag\AnnotationBundle\Controller; |
f38e03dc TC |
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; | |
4dc87223 | 10 | use Wallabag\AnnotationBundle\Entity\Annotation; |
f38e03dc TC |
11 | use Wallabag\CoreBundle\Entity\Entry; |
12 | ||
4dc87223 | 13 | class 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 TC |
21 | * |
22 | * @return Response | |
23 | */ | |
24 | public function getAnnotationsAction(Entry $entry) | |
25 | { | |
4dc87223 | 26 | $annotationRows = $this |
f38e03dc | 27 | ->getDoctrine() |
4dc87223 NL |
28 | ->getRepository('WallabagAnnotationBundle:Annotation') |
29 | ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId()); | |
30 | $total = count($annotationRows); | |
4094ea47 | 31 | $annotations = ['total' => $total, 'rows' => $annotationRows]; |
f38e03dc | 32 | |
4dc87223 | 33 | $json = $this->get('serializer')->serialize($annotations, 'json'); |
f38e03dc TC |
34 | |
35 | return $this->renderJsonResponse($json); | |
36 | } | |
37 | ||
38 | /** | |
4dc87223 | 39 | * Creates a new annotation. |
f38e03dc TC |
40 | * |
41 | * @param Entry $entry | |
42 | * | |
351eb8d9 | 43 | * @see Wallabag\ApiBundle\Controller\WallabagRestController |
f38e03dc TC |
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 | ||
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 TC |
69 | |
70 | return $this->renderJsonResponse($json); | |
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 TC |
79 | * |
80 | * @return Response | |
81 | */ | |
4dc87223 | 82 | public function putAnnotationAction(Annotation $annotation, Request $request) |
f38e03dc TC |
83 | { |
84 | $data = json_decode($request->getContent(), true); | |
85 | ||
86 | if (!is_null($data['text'])) { | |
4dc87223 | 87 | $annotation->setText($data['text']); |
f38e03dc TC |
88 | } |
89 | ||
90 | $em = $this->getDoctrine()->getManager(); | |
91 | $em->flush(); | |
92 | ||
4dc87223 | 93 | $json = $this->get('serializer')->serialize($annotation, 'json'); |
f38e03dc TC |
94 | |
95 | return $this->renderJsonResponse($json); | |
96 | } | |
97 | ||
98 | /** | |
4dc87223 | 99 | * Removes an annotation. |
f38e03dc | 100 | * |
351eb8d9 | 101 | * @see Wallabag\ApiBundle\Controller\WallabagRestController |
f38e03dc | 102 | * |
4dc87223 | 103 | * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") |
f38e03dc TC |
104 | * |
105 | * @return Response | |
106 | */ | |
4dc87223 | 107 | public function deleteAnnotationAction(Annotation $annotation) |
f38e03dc TC |
108 | { |
109 | $em = $this->getDoctrine()->getManager(); | |
4dc87223 | 110 | $em->remove($annotation); |
f38e03dc TC |
111 | $em->flush(); |
112 | ||
4dc87223 | 113 | $json = $this->get('serializer')->serialize($annotation, 'json'); |
f38e03dc TC |
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 | { | |
4094ea47 | 128 | return new Response($json, $code, ['application/json']); |
f38e03dc TC |
129 | } |
130 | } |