diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2016-02-26 18:17:37 +0100 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2016-02-26 18:17:37 +0100 |
commit | 162954763e6f68e0cf4002822007683bde62fc25 (patch) | |
tree | a85f850eddb30b3435d1bb5f45d16d9ab465bbf9 /src/Wallabag/AnnotationBundle/Controller | |
parent | d3f1a9dc1ac181033ec54f9149d2807199fa8f17 (diff) | |
parent | b07c7dfe787313b75e0996265e13cccc78da41be (diff) | |
download | wallabag-162954763e6f68e0cf4002822007683bde62fc25.tar.gz wallabag-162954763e6f68e0cf4002822007683bde62fc25.tar.zst wallabag-162954763e6f68e0cf4002822007683bde62fc25.zip |
Merge pull request #1653 from wallabag/v2-annotator-comments
V2 annotator comments
Diffstat (limited to 'src/Wallabag/AnnotationBundle/Controller')
-rw-r--r-- | src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php b/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php new file mode 100644 index 00000000..5f981eb5 --- /dev/null +++ b/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php | |||
@@ -0,0 +1,146 @@ | |||
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 | * @ApiDoc( | ||
19 | * requirements={ | ||
20 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | ||
21 | * } | ||
22 | * ) | ||
23 | * | ||
24 | * @return Response | ||
25 | */ | ||
26 | public function getAnnotationsAction(Entry $entry) | ||
27 | { | ||
28 | $annotationRows = $this | ||
29 | ->getDoctrine() | ||
30 | ->getRepository('WallabagAnnotationBundle:Annotation') | ||
31 | ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId()); | ||
32 | $total = count($annotationRows); | ||
33 | $annotations = array('total' => $total, 'rows' => $annotationRows); | ||
34 | |||
35 | $json = $this->get('serializer')->serialize($annotations, 'json'); | ||
36 | |||
37 | return $this->renderJsonResponse($json); | ||
38 | } | ||
39 | |||
40 | /** | ||
41 | * Creates a new annotation. | ||
42 | * | ||
43 | * @param Entry $entry | ||
44 | * | ||
45 | * @ApiDoc( | ||
46 | * requirements={ | ||
47 | * {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"}, | ||
48 | * {"name"="quote", "dataType"="string", "required"=false, "description"="Optional, quote for the annotation"}, | ||
49 | * {"name"="text", "dataType"="string", "required"=true, "description"=""}, | ||
50 | * } | ||
51 | * ) | ||
52 | * | ||
53 | * @return Response | ||
54 | */ | ||
55 | public function postAnnotationAction(Request $request, Entry $entry) | ||
56 | { | ||
57 | $data = json_decode($request->getContent(), true); | ||
58 | |||
59 | $em = $this->getDoctrine()->getManager(); | ||
60 | |||
61 | $annotation = new Annotation($this->getUser()); | ||
62 | |||
63 | $annotation->setText($data['text']); | ||
64 | if (array_key_exists('quote', $data)) { | ||
65 | $annotation->setQuote($data['quote']); | ||
66 | } | ||
67 | if (array_key_exists('ranges', $data)) { | ||
68 | $annotation->setRanges($data['ranges']); | ||
69 | } | ||
70 | |||
71 | $annotation->setEntry($entry); | ||
72 | |||
73 | $em->persist($annotation); | ||
74 | $em->flush(); | ||
75 | |||
76 | $json = $this->get('serializer')->serialize($annotation, 'json'); | ||
77 | |||
78 | return $this->renderJsonResponse($json); | ||
79 | } | ||
80 | |||
81 | /** | ||
82 | * Updates an annotation. | ||
83 | * | ||
84 | * @ApiDoc( | ||
85 | * requirements={ | ||
86 | * {"name"="annotation", "dataType"="string", "requirement"="\w+", "description"="The annotation ID"} | ||
87 | * } | ||
88 | * ) | ||
89 | * | ||
90 | * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") | ||
91 | * | ||
92 | * @return Response | ||
93 | */ | ||
94 | public function putAnnotationAction(Annotation $annotation, Request $request) | ||
95 | { | ||
96 | $data = json_decode($request->getContent(), true); | ||
97 | |||
98 | if (!is_null($data['text'])) { | ||
99 | $annotation->setText($data['text']); | ||
100 | } | ||
101 | |||
102 | $em = $this->getDoctrine()->getManager(); | ||
103 | $em->flush(); | ||
104 | |||
105 | $json = $this->get('serializer')->serialize($annotation, 'json'); | ||
106 | |||
107 | return $this->renderJsonResponse($json); | ||
108 | } | ||
109 | |||
110 | /** | ||
111 | * Removes an annotation. | ||
112 | * | ||
113 | * @ApiDoc( | ||
114 | * requirements={ | ||
115 | * {"name"="annotation", "dataType"="string", "requirement"="\w+", "description"="The annotation ID"} | ||
116 | * } | ||
117 | * ) | ||
118 | * | ||
119 | * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") | ||
120 | * | ||
121 | * @return Response | ||
122 | */ | ||
123 | public function deleteAnnotationAction(Annotation $annotation) | ||
124 | { | ||
125 | $em = $this->getDoctrine()->getManager(); | ||
126 | $em->remove($annotation); | ||
127 | $em->flush(); | ||
128 | |||
129 | $json = $this->get('serializer')->serialize($annotation, 'json'); | ||
130 | |||
131 | return $this->renderJsonResponse($json); | ||
132 | } | ||
133 | |||
134 | /** | ||
135 | * Send a JSON Response. | ||
136 | * We don't use the Symfony JsonRespone, because it takes an array as parameter instead of a JSON string. | ||
137 | * | ||
138 | * @param string $json | ||
139 | * | ||
140 | * @return Response | ||
141 | */ | ||
142 | private function renderJsonResponse($json, $code = 200) | ||
143 | { | ||
144 | return new Response($json, $code, array('application/json')); | ||
145 | } | ||
146 | } | ||