diff options
author | Jeremy Benoist <j0k3r@users.noreply.github.com> | 2016-10-29 13:20:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-29 13:20:01 +0200 |
commit | 267087d969e68e2f5abcb18f526120d835d9a686 (patch) | |
tree | 49d4f5b01f168591bc19180506289da5f076fa87 /src/Wallabag/ApiBundle/Controller/TagRestController.php | |
parent | eca4d030bf90cdf6ca0b6a86b18bd8f66a36db08 (diff) | |
parent | bc4564a709ccae949999af77aa456287a72f1591 (diff) | |
download | wallabag-267087d969e68e2f5abcb18f526120d835d9a686.tar.gz wallabag-267087d969e68e2f5abcb18f526120d835d9a686.tar.zst wallabag-267087d969e68e2f5abcb18f526120d835d9a686.zip |
Merge pull request #2509 from wallabag/explode-api-controller
Exploded WallabagRestController into many controllers
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller/TagRestController.php')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/TagRestController.php | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/TagRestController.php b/src/Wallabag/ApiBundle/Controller/TagRestController.php new file mode 100644 index 00000000..4e7ddc66 --- /dev/null +++ b/src/Wallabag/ApiBundle/Controller/TagRestController.php | |||
@@ -0,0 +1,171 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\ApiBundle\Controller; | ||
4 | |||
5 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; | ||
6 | use Symfony\Component\HttpFoundation\Request; | ||
7 | use Symfony\Component\HttpFoundation\JsonResponse; | ||
8 | use Wallabag\CoreBundle\Entity\Entry; | ||
9 | use Wallabag\CoreBundle\Entity\Tag; | ||
10 | |||
11 | class TagRestController extends WallabagRestController | ||
12 | { | ||
13 | /** | ||
14 | * Retrieve all tags. | ||
15 | * | ||
16 | * @ApiDoc() | ||
17 | * | ||
18 | * @return JsonResponse | ||
19 | */ | ||
20 | public function getTagsAction() | ||
21 | { | ||
22 | $this->validateAuthentication(); | ||
23 | |||
24 | $tags = $this->getDoctrine() | ||
25 | ->getRepository('WallabagCoreBundle:Tag') | ||
26 | ->findAllTags($this->getUser()->getId()); | ||
27 | |||
28 | $json = $this->get('serializer')->serialize($tags, 'json'); | ||
29 | |||
30 | return (new JsonResponse())->setJson($json); | ||
31 | } | ||
32 | |||
33 | /** | ||
34 | * Permanently remove one tag from **every** entry. | ||
35 | * | ||
36 | * @ApiDoc( | ||
37 | * requirements={ | ||
38 | * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"} | ||
39 | * } | ||
40 | * ) | ||
41 | * | ||
42 | * @return JsonResponse | ||
43 | */ | ||
44 | public function deleteTagLabelAction(Request $request) | ||
45 | { | ||
46 | $this->validateAuthentication(); | ||
47 | $label = $request->request->get('tag', ''); | ||
48 | |||
49 | $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label); | ||
50 | |||
51 | if (empty($tag)) { | ||
52 | throw $this->createNotFoundException('Tag not found'); | ||
53 | } | ||
54 | |||
55 | $this->getDoctrine() | ||
56 | ->getRepository('WallabagCoreBundle:Entry') | ||
57 | ->removeTag($this->getUser()->getId(), $tag); | ||
58 | |||
59 | $this->cleanOrphanTag($tag); | ||
60 | |||
61 | $json = $this->get('serializer')->serialize($tag, 'json'); | ||
62 | |||
63 | return (new JsonResponse())->setJson($json); | ||
64 | } | ||
65 | |||
66 | /** | ||
67 | * Permanently remove some tags from **every** entry. | ||
68 | * | ||
69 | * @ApiDoc( | ||
70 | * requirements={ | ||
71 | * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"} | ||
72 | * } | ||
73 | * ) | ||
74 | * | ||
75 | * @return JsonResponse | ||
76 | */ | ||
77 | public function deleteTagsLabelAction(Request $request) | ||
78 | { | ||
79 | $this->validateAuthentication(); | ||
80 | |||
81 | $tagsLabels = $request->request->get('tags', ''); | ||
82 | |||
83 | $tags = []; | ||
84 | |||
85 | foreach (explode(',', $tagsLabels) as $tagLabel) { | ||
86 | $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel); | ||
87 | |||
88 | if (!empty($tagEntity)) { | ||
89 | $tags[] = $tagEntity; | ||
90 | } | ||
91 | } | ||
92 | |||
93 | if (empty($tags)) { | ||
94 | throw $this->createNotFoundException('Tags not found'); | ||
95 | } | ||
96 | |||
97 | $this->getDoctrine() | ||
98 | ->getRepository('WallabagCoreBundle:Entry') | ||
99 | ->removeTags($this->getUser()->getId(), $tags); | ||
100 | |||
101 | $this->cleanOrphanTag($tags); | ||
102 | |||
103 | $json = $this->get('serializer')->serialize($tags, 'json'); | ||
104 | |||
105 | return (new JsonResponse())->setJson($json); | ||
106 | } | ||
107 | |||
108 | /** | ||
109 | * Permanently remove one tag from **every** entry. | ||
110 | * | ||
111 | * @ApiDoc( | ||
112 | * requirements={ | ||
113 | * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"} | ||
114 | * } | ||
115 | * ) | ||
116 | * | ||
117 | * @return JsonResponse | ||
118 | */ | ||
119 | public function deleteTagAction(Tag $tag) | ||
120 | { | ||
121 | $this->validateAuthentication(); | ||
122 | |||
123 | $this->getDoctrine() | ||
124 | ->getRepository('WallabagCoreBundle:Entry') | ||
125 | ->removeTag($this->getUser()->getId(), $tag); | ||
126 | |||
127 | $this->cleanOrphanTag($tag); | ||
128 | |||
129 | $json = $this->get('serializer')->serialize($tag, 'json'); | ||
130 | |||
131 | return (new JsonResponse())->setJson($json); | ||
132 | } | ||
133 | |||
134 | /** | ||
135 | * Retrieve version number. | ||
136 | * | ||
137 | * @ApiDoc() | ||
138 | * | ||
139 | * @return JsonResponse | ||
140 | */ | ||
141 | public function getVersionAction() | ||
142 | { | ||
143 | $version = $this->container->getParameter('wallabag_core.version'); | ||
144 | |||
145 | $json = $this->get('serializer')->serialize($version, 'json'); | ||
146 | |||
147 | return (new JsonResponse())->setJson($json); | ||
148 | } | ||
149 | |||
150 | /** | ||
151 | * Remove orphan tag in case no entries are associated to it. | ||
152 | * | ||
153 | * @param Tag|array $tags | ||
154 | */ | ||
155 | private function cleanOrphanTag($tags) | ||
156 | { | ||
157 | if (!is_array($tags)) { | ||
158 | $tags = [$tags]; | ||
159 | } | ||
160 | |||
161 | $em = $this->getDoctrine()->getManager(); | ||
162 | |||
163 | foreach ($tags as $tag) { | ||
164 | if (count($tag->getEntries()) === 0) { | ||
165 | $em->remove($tag); | ||
166 | } | ||
167 | } | ||
168 | |||
169 | $em->flush(); | ||
170 | } | ||
171 | } | ||