]>
Commit | Line | Data |
---|---|---|
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 Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; | |
9 | use Wallabag\AnnotationBundle\Entity\Annotation; | |
10 | use Wallabag\CoreBundle\Entity\Entry; | |
11 | ||
12 | class WallabagAnnotationController extends FOSRestController | |
13 | { | |
14 | /** | |
15 | * Retrieve annotations for an entry. | |
16 | * | |
17 | * @param Entry $entry | |
18 | * | |
19 | * @see Wallabag\ApiBundle\Controller\WallabagRestController | |
20 | * | |
21 | * @return JsonResponse | |
22 | */ | |
23 | public function getAnnotationsAction(Entry $entry) | |
24 | { | |
25 | $annotationRows = $this | |
26 | ->getDoctrine() | |
27 | ->getRepository('WallabagAnnotationBundle:Annotation') | |
28 | ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId()); | |
29 | $total = count($annotationRows); | |
30 | $annotations = ['total' => $total, 'rows' => $annotationRows]; | |
31 | ||
32 | $json = $this->get('serializer')->serialize($annotations, 'json'); | |
33 | ||
34 | return (new JsonResponse())->setJson($json); | |
35 | } | |
36 | ||
37 | /** | |
38 | * Creates a new annotation. | |
39 | * | |
40 | * @param Request $request | |
41 | * @param Entry $entry | |
42 | * | |
43 | * @return JsonResponse | |
44 | * | |
45 | * @see Wallabag\ApiBundle\Controller\WallabagRestController | |
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 | * | |
83 | * @return JsonResponse | |
84 | */ | |
85 | public function putAnnotationAction(Annotation $annotation, Request $request) | |
86 | { | |
87 | $data = json_decode($request->getContent(), true); | |
88 | ||
89 | if (!is_null($data['text'])) { | |
90 | $annotation->setText($data['text']); | |
91 | } | |
92 | ||
93 | $em = $this->getDoctrine()->getManager(); | |
94 | $em->flush(); | |
95 | ||
96 | $json = $this->get('serializer')->serialize($annotation, 'json'); | |
97 | ||
98 | return (new JsonResponse())->setJson($json); | |
99 | } | |
100 | ||
101 | /** | |
102 | * Removes an annotation. | |
103 | * | |
104 | * @see Wallabag\ApiBundle\Controller\WallabagRestController | |
105 | * | |
106 | * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") | |
107 | * | |
108 | * @param Annotation $annotation | |
109 | * | |
110 | * @return JsonResponse | |
111 | */ | |
112 | public function deleteAnnotationAction(Annotation $annotation) | |
113 | { | |
114 | $em = $this->getDoctrine()->getManager(); | |
115 | $em->remove($annotation); | |
116 | $em->flush(); | |
117 | ||
118 | $json = $this->get('serializer')->serialize($annotation, 'json'); | |
119 | ||
120 | return (new JsonResponse())->setJson($json); | |
121 | } | |
122 | } |