aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ApiBundle/Controller/EntryRestController.php
diff options
context:
space:
mode:
authorJeremy Benoist <j0k3r@users.noreply.github.com>2016-10-29 13:20:01 +0200
committerGitHub <noreply@github.com>2016-10-29 13:20:01 +0200
commit267087d969e68e2f5abcb18f526120d835d9a686 (patch)
tree49d4f5b01f168591bc19180506289da5f076fa87 /src/Wallabag/ApiBundle/Controller/EntryRestController.php
parenteca4d030bf90cdf6ca0b6a86b18bd8f66a36db08 (diff)
parentbc4564a709ccae949999af77aa456287a72f1591 (diff)
downloadwallabag-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/EntryRestController.php')
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php367
1 files changed, 367 insertions, 0 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
new file mode 100644
index 00000000..24fa7b3b
--- /dev/null
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -0,0 +1,367 @@
1<?php
2
3namespace Wallabag\ApiBundle\Controller;
4
5use Hateoas\Configuration\Route;
6use Hateoas\Representation\Factory\PagerfantaFactory;
7use Nelmio\ApiDocBundle\Annotation\ApiDoc;
8use Symfony\Component\HttpFoundation\Request;
9use Symfony\Component\HttpFoundation\JsonResponse;
10use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
11use Wallabag\CoreBundle\Entity\Entry;
12use Wallabag\CoreBundle\Entity\Tag;
13
14class EntryRestController extends WallabagRestController
15{
16 /**
17 * Check if an entry exist by url.
18 *
19 * @ApiDoc(
20 * parameters={
21 * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"},
22 * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="Urls (as an array) to check if it exists"}
23 * }
24 * )
25 *
26 * @return JsonResponse
27 */
28 public function getEntriesExistsAction(Request $request)
29 {
30 $this->validateAuthentication();
31
32 $urls = $request->query->get('urls', []);
33
34 // handle multiple urls first
35 if (!empty($urls)) {
36 $results = [];
37 foreach ($urls as $url) {
38 $res = $this->getDoctrine()
39 ->getRepository('WallabagCoreBundle:Entry')
40 ->findByUrlAndUserId($url, $this->getUser()->getId());
41
42 $results[$url] = false === $res ? false : true;
43 }
44
45 $json = $this->get('serializer')->serialize($results, 'json');
46
47 return (new JsonResponse())->setJson($json);
48 }
49
50 // let's see if it is a simple url?
51 $url = $request->query->get('url', '');
52
53 if (empty($url)) {
54 throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId());
55 }
56
57 $res = $this->getDoctrine()
58 ->getRepository('WallabagCoreBundle:Entry')
59 ->findByUrlAndUserId($url, $this->getUser()->getId());
60
61 $exists = false === $res ? false : true;
62
63 $json = $this->get('serializer')->serialize(['exists' => $exists], 'json');
64
65 return (new JsonResponse())->setJson($json);
66 }
67
68 /**
69 * Retrieve all entries. It could be filtered by many options.
70 *
71 * @ApiDoc(
72 * parameters={
73 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."},
74 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."},
75 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."},
76 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
77 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
78 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
79 * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
80 * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."},
81 * }
82 * )
83 *
84 * @return JsonResponse
85 */
86 public function getEntriesAction(Request $request)
87 {
88 $this->validateAuthentication();
89
90 $isArchived = (null === $request->query->get('archive')) ? null : (bool) $request->query->get('archive');
91 $isStarred = (null === $request->query->get('starred')) ? null : (bool) $request->query->get('starred');
92 $sort = $request->query->get('sort', 'created');
93 $order = $request->query->get('order', 'desc');
94 $page = (int) $request->query->get('page', 1);
95 $perPage = (int) $request->query->get('perPage', 30);
96 $tags = $request->query->get('tags', '');
97 $since = $request->query->get('since', 0);
98
99 $pager = $this->getDoctrine()
100 ->getRepository('WallabagCoreBundle:Entry')
101 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags);
102
103 $pager->setCurrentPage($page);
104 $pager->setMaxPerPage($perPage);
105
106 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
107 $paginatedCollection = $pagerfantaFactory->createRepresentation(
108 $pager,
109 new Route(
110 'api_get_entries',
111 [
112 'archive' => $isArchived,
113 'starred' => $isStarred,
114 'sort' => $sort,
115 'order' => $order,
116 'page' => $page,
117 'perPage' => $perPage,
118 'tags' => $tags,
119 'since' => $since,
120 ],
121 UrlGeneratorInterface::ABSOLUTE_URL
122 )
123 );
124
125 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
126
127 return (new JsonResponse())->setJson($json);
128 }
129
130 /**
131 * Retrieve a single entry.
132 *
133 * @ApiDoc(
134 * requirements={
135 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
136 * }
137 * )
138 *
139 * @return JsonResponse
140 */
141 public function getEntryAction(Entry $entry)
142 {
143 $this->validateAuthentication();
144 $this->validateUserAccess($entry->getUser()->getId());
145
146 $json = $this->get('serializer')->serialize($entry, 'json');
147
148 return (new JsonResponse())->setJson($json);
149 }
150
151 /**
152 * Create an entry.
153 *
154 * @ApiDoc(
155 * parameters={
156 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
157 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
158 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
159 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"},
160 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"},
161 * }
162 * )
163 *
164 * @return JsonResponse
165 */
166 public function postEntriesAction(Request $request)
167 {
168 $this->validateAuthentication();
169
170 $url = $request->request->get('url');
171 $title = $request->request->get('title');
172 $isArchived = $request->request->get('archive');
173 $isStarred = $request->request->get('starred');
174
175 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
176
177 if (false === $entry) {
178 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
179 new Entry($this->getUser()),
180 $url
181 );
182 }
183
184 if (!is_null($title)) {
185 $entry->setTitle($title);
186 }
187
188 $tags = $request->request->get('tags', '');
189 if (!empty($tags)) {
190 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
191 }
192
193 if (!is_null($isStarred)) {
194 $entry->setStarred((bool) $isStarred);
195 }
196
197 if (!is_null($isArchived)) {
198 $entry->setArchived((bool) $isArchived);
199 }
200
201 $em = $this->getDoctrine()->getManager();
202 $em->persist($entry);
203
204 $em->flush();
205
206 $json = $this->get('serializer')->serialize($entry, 'json');
207
208 return (new JsonResponse())->setJson($json);
209 }
210
211 /**
212 * Change several properties of an entry.
213 *
214 * @ApiDoc(
215 * requirements={
216 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
217 * },
218 * parameters={
219 * {"name"="title", "dataType"="string", "required"=false},
220 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
221 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
222 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
223 * }
224 * )
225 *
226 * @return JsonResponse
227 */
228 public function patchEntriesAction(Entry $entry, Request $request)
229 {
230 $this->validateAuthentication();
231 $this->validateUserAccess($entry->getUser()->getId());
232
233 $title = $request->request->get('title');
234 $isArchived = $request->request->get('archive');
235 $isStarred = $request->request->get('starred');
236
237 if (!is_null($title)) {
238 $entry->setTitle($title);
239 }
240
241 if (!is_null($isArchived)) {
242 $entry->setArchived((bool) $isArchived);
243 }
244
245 if (!is_null($isStarred)) {
246 $entry->setStarred((bool) $isStarred);
247 }
248
249 $tags = $request->request->get('tags', '');
250 if (!empty($tags)) {
251 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
252 }
253
254 $em = $this->getDoctrine()->getManager();
255 $em->flush();
256
257 $json = $this->get('serializer')->serialize($entry, 'json');
258
259 return (new JsonResponse())->setJson($json);
260 }
261
262 /**
263 * Delete **permanently** an entry.
264 *
265 * @ApiDoc(
266 * requirements={
267 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
268 * }
269 * )
270 *
271 * @return JsonResponse
272 */
273 public function deleteEntriesAction(Entry $entry)
274 {
275 $this->validateAuthentication();
276 $this->validateUserAccess($entry->getUser()->getId());
277
278 $em = $this->getDoctrine()->getManager();
279 $em->remove($entry);
280 $em->flush();
281
282 $json = $this->get('serializer')->serialize($entry, 'json');
283
284 return (new JsonResponse())->setJson($json);
285 }
286
287 /**
288 * Retrieve all tags for an entry.
289 *
290 * @ApiDoc(
291 * requirements={
292 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
293 * }
294 * )
295 *
296 * @return JsonResponse
297 */
298 public function getEntriesTagsAction(Entry $entry)
299 {
300 $this->validateAuthentication();
301 $this->validateUserAccess($entry->getUser()->getId());
302
303 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
304
305 return (new JsonResponse())->setJson($json);
306 }
307
308 /**
309 * Add one or more tags to an entry.
310 *
311 * @ApiDoc(
312 * requirements={
313 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
314 * },
315 * parameters={
316 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
317 * }
318 * )
319 *
320 * @return JsonResponse
321 */
322 public function postEntriesTagsAction(Request $request, Entry $entry)
323 {
324 $this->validateAuthentication();
325 $this->validateUserAccess($entry->getUser()->getId());
326
327 $tags = $request->request->get('tags', '');
328 if (!empty($tags)) {
329 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
330 }
331
332 $em = $this->getDoctrine()->getManager();
333 $em->persist($entry);
334 $em->flush();
335
336 $json = $this->get('serializer')->serialize($entry, 'json');
337
338 return (new JsonResponse())->setJson($json);
339 }
340
341 /**
342 * Permanently remove one tag for an entry.
343 *
344 * @ApiDoc(
345 * requirements={
346 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
347 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
348 * }
349 * )
350 *
351 * @return JsonResponse
352 */
353 public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
354 {
355 $this->validateAuthentication();
356 $this->validateUserAccess($entry->getUser()->getId());
357
358 $entry->removeTag($tag);
359 $em = $this->getDoctrine()->getManager();
360 $em->persist($entry);
361 $em->flush();
362
363 $json = $this->get('serializer')->serialize($entry, 'json');
364
365 return (new JsonResponse())->setJson($json);
366 }
367}