aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CommentBundle/Controller/WallabagCommentController.php
diff options
context:
space:
mode:
authorThomas Citharel <tcit@tcit.fr>2016-02-07 16:52:59 +0100
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-02-26 18:13:17 +0100
commitf38e03dc02c96344677fd2720912605b21c90b5d (patch)
tree2028cecf5d0930e1569ddb977e245ea44bedc38a /src/Wallabag/CommentBundle/Controller/WallabagCommentController.php
parentd3f1a9dc1ac181033ec54f9149d2807199fa8f17 (diff)
downloadwallabag-f38e03dc02c96344677fd2720912605b21c90b5d.tar.gz
wallabag-f38e03dc02c96344677fd2720912605b21c90b5d.tar.zst
wallabag-f38e03dc02c96344677fd2720912605b21c90b5d.zip
Comment work with annotator v2
- add missing annotator.js file and fix typo - edit & delete routes, started tests - basic tests
Diffstat (limited to 'src/Wallabag/CommentBundle/Controller/WallabagCommentController.php')
-rw-r--r--src/Wallabag/CommentBundle/Controller/WallabagCommentController.php146
1 files changed, 146 insertions, 0 deletions
diff --git a/src/Wallabag/CommentBundle/Controller/WallabagCommentController.php b/src/Wallabag/CommentBundle/Controller/WallabagCommentController.php
new file mode 100644
index 00000000..be11f226
--- /dev/null
+++ b/src/Wallabag/CommentBundle/Controller/WallabagCommentController.php
@@ -0,0 +1,146 @@
1<?php
2
3namespace Wallabag\CommentBundle\Controller;
4
5use FOS\RestBundle\Controller\FOSRestController;
6use Nelmio\ApiDocBundle\Annotation\ApiDoc;
7use Symfony\Component\HttpFoundation\Request;
8use Symfony\Component\HttpFoundation\Response;
9use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
10use Wallabag\CommentBundle\Entity\Comment;
11use Wallabag\CoreBundle\Entity\Entry;
12
13class WallabagCommentController extends FOSRestController
14{
15 /**
16 * Retrieve comments 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 $commentRows = $this
29 ->getDoctrine()
30 ->getRepository('WallabagCommentBundle:Comment')
31 ->findCommentsByPageId($entry->getId(), $this->getUser()->getId());
32 $total = count($commentRows);
33 $comments = array('total' => $total, 'rows' => $commentRows);
34
35 $json = $this->get('serializer')->serialize($comments, 'json');
36
37 return $this->renderJsonResponse($json);
38 }
39
40 /**
41 * Creates a new comment.
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 comment"},
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 $comment = new Comment($this->getUser());
62
63 $comment->setText($data['text']);
64 if (array_key_exists('quote', $data)) {
65 $comment->setQuote($data['quote']);
66 }
67 if (array_key_exists('ranges', $data)) {
68 $comment->setRanges($data['ranges']);
69 }
70
71 $comment->setEntry($entry);
72
73 $em->persist($comment);
74 $em->flush();
75
76 $json = $this->get('serializer')->serialize($comment, 'json');
77
78 return $this->renderJsonResponse($json);
79 }
80
81 /**
82 * Updates a comment.
83 *
84 * @ApiDoc(
85 * requirements={
86 * {"name"="comment", "dataType"="string", "requirement"="\w+", "description"="The comment ID"}
87 * }
88 * )
89 *
90 * @ParamConverter("comment", class="WallabagCommentBundle:Comment")
91 *
92 * @return Response
93 */
94 public function putAnnotationAction(Comment $comment, Request $request)
95 {
96 $data = json_decode($request->getContent(), true);
97
98 if (!is_null($data['text'])) {
99 $comment->setText($data['text']);
100 }
101
102 $em = $this->getDoctrine()->getManager();
103 $em->flush();
104
105 $json = $this->get('serializer')->serialize($comment, 'json');
106
107 return $this->renderJsonResponse($json);
108 }
109
110 /**
111 * Removes a comment.
112 *
113 * @ApiDoc(
114 * requirements={
115 * {"name"="comment", "dataType"="string", "requirement"="\w+", "description"="The comment ID"}
116 * }
117 * )
118 *
119 * @ParamConverter("comment", class="WallabagCommentBundle:Comment")
120 *
121 * @return Response
122 */
123 public function deleteAnnotationAction(Comment $comment)
124 {
125 $em = $this->getDoctrine()->getManager();
126 $em->remove($comment);
127 $em->flush();
128
129 $json = $this->get('serializer')->serialize($comment, '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}