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