]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php
move code
[github/wallabag/wallabag.git] / src / Wallabag / AnnotationBundle / Controller / WallabagAnnotationController.php
1 <?php
2
3 namespace Wallabag\AnnotationBundle\Controller;
4
5 use FOS\RestBundle\Controller\FOSRestController;
6 use Symfony\Component\HttpFoundation\JsonResponse;
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 * @param Entry $entry
19 *
20 * @see Wallabag\ApiBundle\Controller\WallabagRestController
21 *
22 * @return JsonResponse
23 */
24 public function getAnnotationsAction(Entry $entry)
25 {
26 $annotationRows = $this
27 ->getDoctrine()
28 ->getRepository('WallabagAnnotationBundle:Annotation')
29 ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId());
30 $total = count($annotationRows);
31 $annotations = array('total' => $total, 'rows' => $annotationRows);
32
33 $json = $this->get('serializer')->serialize($annotations, 'json');
34
35 return (new JsonResponse())->setJson($json);
36 }
37
38 /**
39 * Creates a new annotation.
40 *
41 * @param Request $request
42 * @param Entry $entry
43 * @return JsonResponse
44 * @see Wallabag\ApiBundle\Controller\WallabagRestController
45 *
46 */
47 public function postAnnotationAction(Request $request, Entry $entry)
48 {
49 $data = json_decode($request->getContent(), true);
50
51 $em = $this->getDoctrine()->getManager();
52
53 $annotation = new Annotation($this->getUser());
54
55 $annotation->setText($data['text']);
56 if (array_key_exists('quote', $data)) {
57 $annotation->setQuote($data['quote']);
58 }
59 if (array_key_exists('ranges', $data)) {
60 $annotation->setRanges($data['ranges']);
61 }
62
63 $annotation->setEntry($entry);
64
65 $em->persist($annotation);
66 $em->flush();
67
68 $json = $this->get('serializer')->serialize($annotation, 'json');
69
70 return (new JsonResponse())->setJson($json);
71 }
72
73 /**
74 * Updates an annotation.
75 *
76 * @see Wallabag\ApiBundle\Controller\WallabagRestController
77 *
78 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
79 *
80 * @param Annotation $annotation
81 * @param Request $request
82 * @return JsonResponse
83 */
84 public function putAnnotationAction(Annotation $annotation, Request $request)
85 {
86 $data = json_decode($request->getContent(), true);
87
88 if (!is_null($data['text'])) {
89 $annotation->setText($data['text']);
90 }
91
92 $em = $this->getDoctrine()->getManager();
93 $em->flush();
94
95 $json = $this->get('serializer')->serialize($annotation, 'json');
96
97 return (new JsonResponse())->setJson($json);
98 }
99
100 /**
101 * Removes an annotation.
102 *
103 * @see Wallabag\ApiBundle\Controller\WallabagRestController
104 *
105 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
106 *
107 * @param Annotation $annotation
108 * @return JsonResponse
109 */
110 public function deleteAnnotationAction(Annotation $annotation)
111 {
112 $em = $this->getDoctrine()->getManager();
113 $em->remove($annotation);
114 $em->flush();
115
116 $json = $this->get('serializer')->serialize($annotation, 'json');
117
118 return (new JsonResponse())->setJson($json);
119 }
120 }