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.php138
1 files changed, 100 insertions, 38 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
index af24e498..dd17ef97 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,82 @@ 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 ->findAllTagsWithEntries($this->getUser()->getId());
323 326
324 $json = $this->get('serializer')->serialize($tags, 'json'); 327 $json = $this->get('serializer')->serialize($tags, 'json');
325 328
326 return $this->renderJsonResponse($json); 329 return (new JsonResponse())->setJson($json);
330 }
331
332 /**
333 * Permanently remove one tag from **every** entry.
334 *
335 * @ApiDoc(
336 * requirements={
337 * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"}
338 * }
339 * )
340 *
341 * @return JsonResponse
342 */
343 public function deleteTagLabelAction(Request $request)
344 {
345 $this->validateAuthentication();
346 $label = $request->request->get('tag', '');
347
348 $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label);
349
350 if (empty($tag)) {
351 throw $this->createNotFoundException('Tag not found');
352 }
353
354 $this->getDoctrine()
355 ->getRepository('WallabagCoreBundle:Entry')
356 ->removeTag($this->getUser()->getId(), $tag);
357
358 $json = $this->get('serializer')->serialize($tag, 'json');
359
360 return (new JsonResponse())->setJson($json);
361 }
362
363 /**
364 * Permanently remove some tags from **every** entry.
365 *
366 * @ApiDoc(
367 * requirements={
368 * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"}
369 * }
370 * )
371 *
372 * @return JsonResponse
373 */
374 public function deleteTagsLabelAction(Request $request)
375 {
376 $this->validateAuthentication();
377
378 $tagsLabels = $request->request->get('tags', '');
379
380 $tags = [];
381
382 foreach (explode(',', $tagsLabels) as $tagLabel) {
383 $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
384
385 if (!empty($tagEntity)) {
386 $tags[] = $tagEntity;
387 }
388 }
389
390 if (empty($tags)) {
391 throw $this->createNotFoundException('Tags not found');
392 }
393
394 $this->getDoctrine()
395 ->getRepository('WallabagCoreBundle:Entry')
396 ->removeTags($this->getUser()->getId(), $tags);
397
398 $json = $this->get('serializer')->serialize($tags, 'json');
399
400 return (new JsonResponse())->setJson($json);
327 } 401 }
328 402
329 /** 403 /**
@@ -335,7 +409,7 @@ class WallabagRestController extends FOSRestController
335 * } 409 * }
336 * ) 410 * )
337 * 411 *
338 * @return Response 412 * @return JsonResponse
339 */ 413 */
340 public function deleteTagAction(Tag $tag) 414 public function deleteTagAction(Tag $tag)
341 { 415 {
@@ -347,14 +421,15 @@ class WallabagRestController extends FOSRestController
347 421
348 $json = $this->get('serializer')->serialize($tag, 'json'); 422 $json = $this->get('serializer')->serialize($tag, 'json');
349 423
350 return $this->renderJsonResponse($json); 424 return (new JsonResponse())->setJson($json);
351 } 425 }
426
352 /** 427 /**
353 * Retrieve version number. 428 * Retrieve version number.
354 * 429 *
355 * @ApiDoc() 430 * @ApiDoc()
356 * 431 *
357 * @return Response 432 * @return JsonResponse
358 */ 433 */
359 public function getVersionAction() 434 public function getVersionAction()
360 { 435 {
@@ -362,7 +437,7 @@ class WallabagRestController extends FOSRestController
362 437
363 $json = $this->get('serializer')->serialize($version, 'json'); 438 $json = $this->get('serializer')->serialize($version, 'json');
364 439
365 return $this->renderJsonResponse($json); 440 return (new JsonResponse())->setJson($json);
366 } 441 }
367 442
368 /** 443 /**
@@ -378,17 +453,4 @@ class WallabagRestController extends FOSRestController
378 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$user->getId()); 453 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$user->getId());
379 } 454 }
380 } 455 }
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} 456}