3 namespace Wallabag\CommentBundle\Controller
;
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\CommentBundle\Entity\Comment
;
11 use Wallabag\CoreBundle\Entity\Entry
;
13 class WallabagCommentController
extends FOSRestController
16 * Retrieve comments for an entry.
20 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
26 public function getAnnotationsAction(Entry
$entry)
30 ->getRepository('WallabagCommentBundle:Comment')
31 ->findCommentsByPageId($entry->getId(), $this->getUser()->getId());
32 $total = count($commentRows);
33 $comments = array('total' => $total, 'rows' => $commentRows);
35 $json = $this->get('serializer')->serialize($comments, 'json');
37 return $this->renderJsonResponse($json);
41 * Creates a new comment.
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"=""},
55 public function postAnnotationAction(Request
$request, Entry
$entry)
57 $data = json_decode($request->getContent(), true);
59 $em = $this->getDoctrine()->getManager();
61 $comment = new Comment($this->getUser());
63 $comment->setText($data['text']);
64 if (array_key_exists('quote', $data)) {
65 $comment->setQuote($data['quote']);
67 if (array_key_exists('ranges', $data)) {
68 $comment->setRanges($data['ranges']);
71 $comment->setEntry($entry);
73 $em->persist($comment);
76 $json = $this->get('serializer')->serialize($comment, 'json');
78 return $this->renderJsonResponse($json);
86 * {"name"="comment", "dataType"="string", "requirement"="\w+", "description"="The comment ID"}
90 * @ParamConverter("comment", class="WallabagCommentBundle:Comment")
94 public function putAnnotationAction(Comment
$comment, Request
$request)
96 $data = json_decode($request->getContent(), true);
98 if (!is_null($data['text'])) {
99 $comment->setText($data['text']);
102 $em = $this->getDoctrine()->getManager();
105 $json = $this->get('serializer')->serialize($comment, 'json');
107 return $this->renderJsonResponse($json);
115 * {"name"="comment", "dataType"="string", "requirement"="\w+", "description"="The comment ID"}
119 * @ParamConverter("comment", class="WallabagCommentBundle:Comment")
123 public function deleteAnnotationAction(Comment
$comment)
125 $em = $this->getDoctrine()->getManager();
126 $em->remove($comment);
129 $json = $this->get('serializer')->serialize($comment, 'json');
131 return $this->renderJsonResponse($json);
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.
138 * @param string $json
142 private function renderJsonResponse($json, $code = 200)
144 return new Response($json, $code, array('application/json'));