aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller/WallabagRestController.php')
-rw-r--r--src/Wallabag/ApiBundle/Controller/WallabagRestController.php140
1 files changed, 102 insertions, 38 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
index af24e498..fb7c6c1f 100644
--- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
@@ -7,7 +7,7 @@ use Hateoas\Configuration\Route;
7use Hateoas\Representation\Factory\PagerfantaFactory; 7use Hateoas\Representation\Factory\PagerfantaFactory;
8use Nelmio\ApiDocBundle\Annotation\ApiDoc; 8use Nelmio\ApiDocBundle\Annotation\ApiDoc;
9use Symfony\Component\HttpFoundation\Request; 9use Symfony\Component\HttpFoundation\Request;
10use Symfony\Component\HttpFoundation\Response; 10use Symfony\Component\HttpFoundation\JsonResponse;
11use Symfony\Component\Routing\Generator\UrlGeneratorInterface; 11use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
12use Symfony\Component\Security\Core\Exception\AccessDeniedException; 12use Symfony\Component\Security\Core\Exception\AccessDeniedException;
13use Wallabag\CoreBundle\Entity\Entry; 13use Wallabag\CoreBundle\Entity\Entry;
@@ -34,10 +34,11 @@ class WallabagRestController extends FOSRestController
34 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."}, 34 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
35 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."}, 35 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
36 * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."}, 36 * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
37 * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."},
37 * } 38 * }
38 * ) 39 * )
39 * 40 *
40 * @return Response 41 * @return JsonResponse
41 */ 42 */
42 public function getEntriesAction(Request $request) 43 public function getEntriesAction(Request $request)
43 { 44 {
@@ -49,10 +50,12 @@ class WallabagRestController extends FOSRestController
49 $order = $request->query->get('order', 'desc'); 50 $order = $request->query->get('order', 'desc');
50 $page = (int) $request->query->get('page', 1); 51 $page = (int) $request->query->get('page', 1);
51 $perPage = (int) $request->query->get('perPage', 30); 52 $perPage = (int) $request->query->get('perPage', 30);
53 $since = $request->query->get('since', 0);
54 $tags = $request->query->get('tags', '');
52 55
53 $pager = $this->getDoctrine() 56 $pager = $this->getDoctrine()
54 ->getRepository('WallabagCoreBundle:Entry') 57 ->getRepository('WallabagCoreBundle:Entry')
55 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order); 58 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags);
56 59
57 $pager->setCurrentPage($page); 60 $pager->setCurrentPage($page);
58 $pager->setMaxPerPage($perPage); 61 $pager->setMaxPerPage($perPage);
@@ -65,7 +68,7 @@ class WallabagRestController extends FOSRestController
65 68
66 $json = $this->get('serializer')->serialize($paginatedCollection, 'json'); 69 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
67 70
68 return $this->renderJsonResponse($json); 71 return (new JsonResponse())->setJson($json);
69 } 72 }
70 73
71 /** 74 /**
@@ -77,7 +80,7 @@ class WallabagRestController extends FOSRestController
77 * } 80 * }
78 * ) 81 * )
79 * 82 *
80 * @return Response 83 * @return JsonResponse
81 */ 84 */
82 public function getEntryAction(Entry $entry) 85 public function getEntryAction(Entry $entry)
83 { 86 {
@@ -86,7 +89,7 @@ class WallabagRestController extends FOSRestController
86 89
87 $json = $this->get('serializer')->serialize($entry, 'json'); 90 $json = $this->get('serializer')->serialize($entry, 'json');
88 91
89 return $this->renderJsonResponse($json); 92 return (new JsonResponse())->setJson($json);
90 } 93 }
91 94
92 /** 95 /**
@@ -102,7 +105,7 @@ class WallabagRestController extends FOSRestController
102 * } 105 * }
103 * ) 106 * )
104 * 107 *
105 * @return Response 108 * @return JsonResponse
106 */ 109 */
107 public function postEntriesAction(Request $request) 110 public function postEntriesAction(Request $request)
108 { 111 {
@@ -146,7 +149,7 @@ class WallabagRestController extends FOSRestController
146 149
147 $json = $this->get('serializer')->serialize($entry, 'json'); 150 $json = $this->get('serializer')->serialize($entry, 'json');
148 151
149 return $this->renderJsonResponse($json); 152 return (new JsonResponse())->setJson($json);
150 } 153 }
151 154
152 /** 155 /**
@@ -164,7 +167,7 @@ class WallabagRestController extends FOSRestController
164 * } 167 * }
165 * ) 168 * )
166 * 169 *
167 * @return Response 170 * @return JsonResponse
168 */ 171 */
169 public function patchEntriesAction(Entry $entry, Request $request) 172 public function patchEntriesAction(Entry $entry, Request $request)
170 { 173 {
@@ -197,7 +200,7 @@ class WallabagRestController extends FOSRestController
197 200
198 $json = $this->get('serializer')->serialize($entry, 'json'); 201 $json = $this->get('serializer')->serialize($entry, 'json');
199 202
200 return $this->renderJsonResponse($json); 203 return (new JsonResponse())->setJson($json);
201 } 204 }
202 205
203 /** 206 /**
@@ -209,7 +212,7 @@ class WallabagRestController extends FOSRestController
209 * } 212 * }
210 * ) 213 * )
211 * 214 *
212 * @return Response 215 * @return JsonResponse
213 */ 216 */
214 public function deleteEntriesAction(Entry $entry) 217 public function deleteEntriesAction(Entry $entry)
215 { 218 {
@@ -222,7 +225,7 @@ class WallabagRestController extends FOSRestController
222 225
223 $json = $this->get('serializer')->serialize($entry, 'json'); 226 $json = $this->get('serializer')->serialize($entry, 'json');
224 227
225 return $this->renderJsonResponse($json); 228 return (new JsonResponse())->setJson($json);
226 } 229 }
227 230
228 /** 231 /**
@@ -234,7 +237,7 @@ class WallabagRestController extends FOSRestController
234 * } 237 * }
235 * ) 238 * )
236 * 239 *
237 * @return Response 240 * @return JsonResponse
238 */ 241 */
239 public function getEntriesTagsAction(Entry $entry) 242 public function getEntriesTagsAction(Entry $entry)
240 { 243 {
@@ -243,7 +246,7 @@ class WallabagRestController extends FOSRestController
243 246
244 $json = $this->get('serializer')->serialize($entry->getTags(), 'json'); 247 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
245 248
246 return $this->renderJsonResponse($json); 249 return (new JsonResponse())->setJson($json);
247 } 250 }
248 251
249 /** 252 /**
@@ -258,7 +261,7 @@ class WallabagRestController extends FOSRestController
258 * } 261 * }
259 * ) 262 * )
260 * 263 *
261 * @return Response 264 * @return JsonResponse
262 */ 265 */
263 public function postEntriesTagsAction(Request $request, Entry $entry) 266 public function postEntriesTagsAction(Request $request, Entry $entry)
264 { 267 {
@@ -276,7 +279,7 @@ class WallabagRestController extends FOSRestController
276 279
277 $json = $this->get('serializer')->serialize($entry, 'json'); 280 $json = $this->get('serializer')->serialize($entry, 'json');
278 281
279 return $this->renderJsonResponse($json); 282 return (new JsonResponse())->setJson($json);
280 } 283 }
281 284
282 /** 285 /**
@@ -289,7 +292,7 @@ class WallabagRestController extends FOSRestController
289 * } 292 * }
290 * ) 293 * )
291 * 294 *
292 * @return Response 295 * @return JsonResponse
293 */ 296 */
294 public function deleteEntriesTagsAction(Entry $entry, Tag $tag) 297 public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
295 { 298 {
@@ -303,7 +306,7 @@ class WallabagRestController extends FOSRestController
303 306
304 $json = $this->get('serializer')->serialize($entry, 'json'); 307 $json = $this->get('serializer')->serialize($entry, 'json');
305 308
306 return $this->renderJsonResponse($json); 309 return (new JsonResponse())->setJson($json);
307 } 310 }
308 311
309 /** 312 /**
@@ -311,7 +314,7 @@ class WallabagRestController extends FOSRestController
311 * 314 *
312 * @ApiDoc() 315 * @ApiDoc()
313 * 316 *
314 * @return Response 317 * @return JsonResponse
315 */ 318 */
316 public function getTagsAction() 319 public function getTagsAction()
317 { 320 {
@@ -319,11 +322,84 @@ class WallabagRestController extends FOSRestController
319 322
320 $tags = $this->getDoctrine() 323 $tags = $this->getDoctrine()
321 ->getRepository('WallabagCoreBundle:Tag') 324 ->getRepository('WallabagCoreBundle:Tag')
322 ->findAllTags($this->getUser()->getId()); 325 ->findAllTags($this->getUser()->getId())
326 ->getQuery()
327 ->getResult();
323 328
324 $json = $this->get('serializer')->serialize($tags, 'json'); 329 $json = $this->get('serializer')->serialize($tags, 'json');
325 330
326 return $this->renderJsonResponse($json); 331 return (new JsonResponse())->setJson($json);
332 }
333
334 /**
335 * Permanently remove one tag from **every** entry.
336 *
337 * @ApiDoc(
338 * requirements={
339 * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"}
340 * }
341 * )
342 *
343 * @return JsonResponse
344 */
345 public function deleteTagLabelAction(Request $request)
346 {
347 $this->validateAuthentication();
348 $label = $request->request->get('tag', '');
349
350 $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label);
351
352 if (empty($tag)) {
353 throw $this->createNotFoundException('Tag not found');
354 }
355
356 $this->getDoctrine()
357 ->getRepository('WallabagCoreBundle:Entry')
358 ->removeTag($this->getUser()->getId(), $tag);
359
360 $json = $this->get('serializer')->serialize($tag, 'json');
361
362 return (new JsonResponse())->setJson($json);
363 }
364
365 /**
366 * Permanently remove some tags from **every** entry.
367 *
368 * @ApiDoc(
369 * requirements={
370 * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"}
371 * }
372 * )
373 *
374 * @return JsonResponse
375 */
376 public function deleteTagsLabelAction(Request $request)
377 {
378 $this->validateAuthentication();
379
380 $tagsLabels = $request->request->get('tags', '');
381
382 $tags = [];
383
384 foreach (explode(',', $tagsLabels) as $tagLabel) {
385 $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
386
387 if (!empty($tagEntity)) {
388 $tags[] = $tagEntity;
389 }
390 }
391
392 if (empty($tags)) {
393 throw $this->createNotFoundException('Tags not found');
394 }
395
396 $this->getDoctrine()
397 ->getRepository('WallabagCoreBundle:Entry')
398 ->removeTags($this->getUser()->getId(), $tags);
399
400 $json = $this->get('serializer')->serialize($tags, 'json');
401
402 return (new JsonResponse())->setJson($json);
327 } 403 }
328 404
329 /** 405 /**
@@ -335,7 +411,7 @@ class WallabagRestController extends FOSRestController
335 * } 411 * }
336 * ) 412 * )
337 * 413 *
338 * @return Response 414 * @return JsonResponse
339 */ 415 */
340 public function deleteTagAction(Tag $tag) 416 public function deleteTagAction(Tag $tag)
341 { 417 {
@@ -347,14 +423,15 @@ class WallabagRestController extends FOSRestController
347 423
348 $json = $this->get('serializer')->serialize($tag, 'json'); 424 $json = $this->get('serializer')->serialize($tag, 'json');
349 425
350 return $this->renderJsonResponse($json); 426 return (new JsonResponse())->setJson($json);
351 } 427 }
428
352 /** 429 /**
353 * Retrieve version number. 430 * Retrieve version number.
354 * 431 *
355 * @ApiDoc() 432 * @ApiDoc()
356 * 433 *
357 * @return Response 434 * @return JsonResponse
358 */ 435 */
359 public function getVersionAction() 436 public function getVersionAction()
360 { 437 {
@@ -362,7 +439,7 @@ class WallabagRestController extends FOSRestController
362 439
363 $json = $this->get('serializer')->serialize($version, 'json'); 440 $json = $this->get('serializer')->serialize($version, 'json');
364 441
365 return $this->renderJsonResponse($json); 442 return (new JsonResponse())->setJson($json);
366 } 443 }
367 444
368 /** 445 /**
@@ -378,17 +455,4 @@ class WallabagRestController extends FOSRestController
378 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$user->getId()); 455 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$user->getId());
379 } 456 }
380 } 457 }
381
382 /**
383 * Send a JSON Response.
384 * We don't use the Symfony JsonRespone, because it takes an array as parameter instead of a JSON string.
385 *
386 * @param string $json
387 *
388 * @return Response
389 */
390 private function renderJsonResponse($json)
391 {
392 return new Response($json, 200, ['application/json']);
393 }
394} 458}