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.php170
1 files changed, 132 insertions, 38 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
index af24e498..104720a9 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;
@@ -23,6 +23,38 @@ class WallabagRestController extends FOSRestController
23 } 23 }
24 24
25 /** 25 /**
26 * Check if an entry exist by url.
27 *
28 * @ApiDoc(
29 * parameters={
30 * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"}
31 * }
32 * )
33 *
34 * @return JsonResponse
35 */
36 public function getEntriesExistsAction(Request $request)
37 {
38 $this->validateAuthentication();
39
40 $url = $request->query->get('url', '');
41
42 if (empty($url)) {
43 throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$user->getId());
44 }
45
46 $res = $this->getDoctrine()
47 ->getRepository('WallabagCoreBundle:Entry')
48 ->findByUrlAndUserId($url, $this->getUser()->getId());
49
50 $exists = false === $res ? false : true;
51
52 $json = $this->get('serializer')->serialize(['exists' => $exists], 'json');
53
54 return (new JsonResponse())->setJson($json);
55 }
56
57 /**
26 * Retrieve all entries. It could be filtered by many options. 58 * Retrieve all entries. It could be filtered by many options.
27 * 59 *
28 * @ApiDoc( 60 * @ApiDoc(
@@ -34,10 +66,11 @@ class WallabagRestController extends FOSRestController
34 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."}, 66 * {"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."}, 67 * {"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."}, 68 * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
69 * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."},
37 * } 70 * }
38 * ) 71 * )
39 * 72 *
40 * @return Response 73 * @return JsonResponse
41 */ 74 */
42 public function getEntriesAction(Request $request) 75 public function getEntriesAction(Request $request)
43 { 76 {
@@ -49,10 +82,12 @@ class WallabagRestController extends FOSRestController
49 $order = $request->query->get('order', 'desc'); 82 $order = $request->query->get('order', 'desc');
50 $page = (int) $request->query->get('page', 1); 83 $page = (int) $request->query->get('page', 1);
51 $perPage = (int) $request->query->get('perPage', 30); 84 $perPage = (int) $request->query->get('perPage', 30);
85 $since = $request->query->get('since', 0);
86 $tags = $request->query->get('tags', '');
52 87
53 $pager = $this->getDoctrine() 88 $pager = $this->getDoctrine()
54 ->getRepository('WallabagCoreBundle:Entry') 89 ->getRepository('WallabagCoreBundle:Entry')
55 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order); 90 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags);
56 91
57 $pager->setCurrentPage($page); 92 $pager->setCurrentPage($page);
58 $pager->setMaxPerPage($perPage); 93 $pager->setMaxPerPage($perPage);
@@ -65,7 +100,7 @@ class WallabagRestController extends FOSRestController
65 100
66 $json = $this->get('serializer')->serialize($paginatedCollection, 'json'); 101 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
67 102
68 return $this->renderJsonResponse($json); 103 return (new JsonResponse())->setJson($json);
69 } 104 }
70 105
71 /** 106 /**
@@ -77,7 +112,7 @@ class WallabagRestController extends FOSRestController
77 * } 112 * }
78 * ) 113 * )
79 * 114 *
80 * @return Response 115 * @return JsonResponse
81 */ 116 */
82 public function getEntryAction(Entry $entry) 117 public function getEntryAction(Entry $entry)
83 { 118 {
@@ -86,7 +121,7 @@ class WallabagRestController extends FOSRestController
86 121
87 $json = $this->get('serializer')->serialize($entry, 'json'); 122 $json = $this->get('serializer')->serialize($entry, 'json');
88 123
89 return $this->renderJsonResponse($json); 124 return (new JsonResponse())->setJson($json);
90 } 125 }
91 126
92 /** 127 /**
@@ -102,7 +137,7 @@ class WallabagRestController extends FOSRestController
102 * } 137 * }
103 * ) 138 * )
104 * 139 *
105 * @return Response 140 * @return JsonResponse
106 */ 141 */
107 public function postEntriesAction(Request $request) 142 public function postEntriesAction(Request $request)
108 { 143 {
@@ -146,7 +181,7 @@ class WallabagRestController extends FOSRestController
146 181
147 $json = $this->get('serializer')->serialize($entry, 'json'); 182 $json = $this->get('serializer')->serialize($entry, 'json');
148 183
149 return $this->renderJsonResponse($json); 184 return (new JsonResponse())->setJson($json);
150 } 185 }
151 186
152 /** 187 /**
@@ -164,7 +199,7 @@ class WallabagRestController extends FOSRestController
164 * } 199 * }
165 * ) 200 * )
166 * 201 *
167 * @return Response 202 * @return JsonResponse
168 */ 203 */
169 public function patchEntriesAction(Entry $entry, Request $request) 204 public function patchEntriesAction(Entry $entry, Request $request)
170 { 205 {
@@ -197,7 +232,7 @@ class WallabagRestController extends FOSRestController
197 232
198 $json = $this->get('serializer')->serialize($entry, 'json'); 233 $json = $this->get('serializer')->serialize($entry, 'json');
199 234
200 return $this->renderJsonResponse($json); 235 return (new JsonResponse())->setJson($json);
201 } 236 }
202 237
203 /** 238 /**
@@ -209,7 +244,7 @@ class WallabagRestController extends FOSRestController
209 * } 244 * }
210 * ) 245 * )
211 * 246 *
212 * @return Response 247 * @return JsonResponse
213 */ 248 */
214 public function deleteEntriesAction(Entry $entry) 249 public function deleteEntriesAction(Entry $entry)
215 { 250 {
@@ -222,7 +257,7 @@ class WallabagRestController extends FOSRestController
222 257
223 $json = $this->get('serializer')->serialize($entry, 'json'); 258 $json = $this->get('serializer')->serialize($entry, 'json');
224 259
225 return $this->renderJsonResponse($json); 260 return (new JsonResponse())->setJson($json);
226 } 261 }
227 262
228 /** 263 /**
@@ -234,7 +269,7 @@ class WallabagRestController extends FOSRestController
234 * } 269 * }
235 * ) 270 * )
236 * 271 *
237 * @return Response 272 * @return JsonResponse
238 */ 273 */
239 public function getEntriesTagsAction(Entry $entry) 274 public function getEntriesTagsAction(Entry $entry)
240 { 275 {
@@ -243,7 +278,7 @@ class WallabagRestController extends FOSRestController
243 278
244 $json = $this->get('serializer')->serialize($entry->getTags(), 'json'); 279 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
245 280
246 return $this->renderJsonResponse($json); 281 return (new JsonResponse())->setJson($json);
247 } 282 }
248 283
249 /** 284 /**
@@ -258,7 +293,7 @@ class WallabagRestController extends FOSRestController
258 * } 293 * }
259 * ) 294 * )
260 * 295 *
261 * @return Response 296 * @return JsonResponse
262 */ 297 */
263 public function postEntriesTagsAction(Request $request, Entry $entry) 298 public function postEntriesTagsAction(Request $request, Entry $entry)
264 { 299 {
@@ -276,7 +311,7 @@ class WallabagRestController extends FOSRestController
276 311
277 $json = $this->get('serializer')->serialize($entry, 'json'); 312 $json = $this->get('serializer')->serialize($entry, 'json');
278 313
279 return $this->renderJsonResponse($json); 314 return (new JsonResponse())->setJson($json);
280 } 315 }
281 316
282 /** 317 /**
@@ -289,7 +324,7 @@ class WallabagRestController extends FOSRestController
289 * } 324 * }
290 * ) 325 * )
291 * 326 *
292 * @return Response 327 * @return JsonResponse
293 */ 328 */
294 public function deleteEntriesTagsAction(Entry $entry, Tag $tag) 329 public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
295 { 330 {
@@ -303,7 +338,7 @@ class WallabagRestController extends FOSRestController
303 338
304 $json = $this->get('serializer')->serialize($entry, 'json'); 339 $json = $this->get('serializer')->serialize($entry, 'json');
305 340
306 return $this->renderJsonResponse($json); 341 return (new JsonResponse())->setJson($json);
307 } 342 }
308 343
309 /** 344 /**
@@ -311,7 +346,7 @@ class WallabagRestController extends FOSRestController
311 * 346 *
312 * @ApiDoc() 347 * @ApiDoc()
313 * 348 *
314 * @return Response 349 * @return JsonResponse
315 */ 350 */
316 public function getTagsAction() 351 public function getTagsAction()
317 { 352 {
@@ -319,11 +354,82 @@ class WallabagRestController extends FOSRestController
319 354
320 $tags = $this->getDoctrine() 355 $tags = $this->getDoctrine()
321 ->getRepository('WallabagCoreBundle:Tag') 356 ->getRepository('WallabagCoreBundle:Tag')
322 ->findAllTags($this->getUser()->getId()); 357 ->findAllTagsWithEntries($this->getUser()->getId());
358
359 $json = $this->get('serializer')->serialize($tags, 'json');
360
361 return (new JsonResponse())->setJson($json);
362 }
363
364 /**
365 * Permanently remove one tag from **every** entry.
366 *
367 * @ApiDoc(
368 * requirements={
369 * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"}
370 * }
371 * )
372 *
373 * @return JsonResponse
374 */
375 public function deleteTagLabelAction(Request $request)
376 {
377 $this->validateAuthentication();
378 $label = $request->request->get('tag', '');
379
380 $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label);
381
382 if (empty($tag)) {
383 throw $this->createNotFoundException('Tag not found');
384 }
385
386 $this->getDoctrine()
387 ->getRepository('WallabagCoreBundle:Entry')
388 ->removeTag($this->getUser()->getId(), $tag);
389
390 $json = $this->get('serializer')->serialize($tag, 'json');
391
392 return (new JsonResponse())->setJson($json);
393 }
394
395 /**
396 * Permanently remove some tags from **every** entry.
397 *
398 * @ApiDoc(
399 * requirements={
400 * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"}
401 * }
402 * )
403 *
404 * @return JsonResponse
405 */
406 public function deleteTagsLabelAction(Request $request)
407 {
408 $this->validateAuthentication();
409
410 $tagsLabels = $request->request->get('tags', '');
411
412 $tags = [];
413
414 foreach (explode(',', $tagsLabels) as $tagLabel) {
415 $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
416
417 if (!empty($tagEntity)) {
418 $tags[] = $tagEntity;
419 }
420 }
421
422 if (empty($tags)) {
423 throw $this->createNotFoundException('Tags not found');
424 }
425
426 $this->getDoctrine()
427 ->getRepository('WallabagCoreBundle:Entry')
428 ->removeTags($this->getUser()->getId(), $tags);
323 429
324 $json = $this->get('serializer')->serialize($tags, 'json'); 430 $json = $this->get('serializer')->serialize($tags, 'json');
325 431
326 return $this->renderJsonResponse($json); 432 return (new JsonResponse())->setJson($json);
327 } 433 }
328 434
329 /** 435 /**
@@ -335,7 +441,7 @@ class WallabagRestController extends FOSRestController
335 * } 441 * }
336 * ) 442 * )
337 * 443 *
338 * @return Response 444 * @return JsonResponse
339 */ 445 */
340 public function deleteTagAction(Tag $tag) 446 public function deleteTagAction(Tag $tag)
341 { 447 {
@@ -347,14 +453,15 @@ class WallabagRestController extends FOSRestController
347 453
348 $json = $this->get('serializer')->serialize($tag, 'json'); 454 $json = $this->get('serializer')->serialize($tag, 'json');
349 455
350 return $this->renderJsonResponse($json); 456 return (new JsonResponse())->setJson($json);
351 } 457 }
458
352 /** 459 /**
353 * Retrieve version number. 460 * Retrieve version number.
354 * 461 *
355 * @ApiDoc() 462 * @ApiDoc()
356 * 463 *
357 * @return Response 464 * @return JsonResponse
358 */ 465 */
359 public function getVersionAction() 466 public function getVersionAction()
360 { 467 {
@@ -362,7 +469,7 @@ class WallabagRestController extends FOSRestController
362 469
363 $json = $this->get('serializer')->serialize($version, 'json'); 470 $json = $this->get('serializer')->serialize($version, 'json');
364 471
365 return $this->renderJsonResponse($json); 472 return (new JsonResponse())->setJson($json);
366 } 473 }
367 474
368 /** 475 /**
@@ -378,17 +485,4 @@ class WallabagRestController extends FOSRestController
378 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$user->getId()); 485 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$user->getId());
379 } 486 }
380 } 487 }
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} 488}