diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-07-24 10:55:49 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-08-23 07:26:58 +0200 |
commit | a0e1eafc35e1007056555651ace7221d09cd8270 (patch) | |
tree | f565b8d53fbf89e0a2221467fee213fdc3153050 /src/Wallabag/ApiBundle/Controller/WallabagRestController.php | |
parent | 9bf83f1fb8d588b871a5d12289179de087756d02 (diff) | |
download | wallabag-a0e1eafc35e1007056555651ace7221d09cd8270.tar.gz wallabag-a0e1eafc35e1007056555651ace7221d09cd8270.tar.zst wallabag-a0e1eafc35e1007056555651ace7221d09cd8270.zip |
Add some tests
Also, retrieve tag from the request instead of the query (which will be the same but it's more easy to test).
Moved down `deleteTagAction` because it conflicted with the new action:
api_delete_tag => /api/tags/{tag}.{_format}
api_delete_tags_label => /api/tags/label.{_format}
And finally, throw exception when a tag is not found before removing it.
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller/WallabagRestController.php')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/WallabagRestController.php | 58 |
1 files changed, 35 insertions, 23 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index 43225149..869fdc56 100644 --- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php | |||
@@ -334,15 +334,22 @@ class WallabagRestController extends FOSRestController | |||
334 | * | 334 | * |
335 | * @ApiDoc( | 335 | * @ApiDoc( |
336 | * requirements={ | 336 | * requirements={ |
337 | * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"} | 337 | * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"} |
338 | * } | 338 | * } |
339 | * ) | 339 | * ) |
340 | * | 340 | * |
341 | * @return Response | 341 | * @return Response |
342 | */ | 342 | */ |
343 | public function deleteTagAction(Tag $tag) | 343 | public function deleteTagLabelAction(Request $request) |
344 | { | 344 | { |
345 | $this->validateAuthentication(); | 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 | } | ||
346 | 353 | ||
347 | $this->getDoctrine() | 354 | $this->getDoctrine() |
348 | ->getRepository('WallabagCoreBundle:Entry') | 355 | ->getRepository('WallabagCoreBundle:Entry') |
@@ -354,60 +361,65 @@ class WallabagRestController extends FOSRestController | |||
354 | } | 361 | } |
355 | 362 | ||
356 | /** | 363 | /** |
357 | * Permanently remove one tag from **every** entry. | 364 | * Permanently remove some tags from **every** entry. |
358 | * | 365 | * |
359 | * @ApiDoc( | 366 | * @ApiDoc( |
360 | * requirements={ | 367 | * requirements={ |
361 | * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag as a string"} | 368 | * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"} |
362 | * } | 369 | * } |
363 | * ) | 370 | * ) |
364 | * | 371 | * |
365 | * @return Response | 372 | * @return Response |
366 | */ | 373 | */ |
367 | public function deleteTagLabelAction(Request $request) | 374 | public function deleteTagsLabelAction(Request $request) |
368 | { | 375 | { |
369 | $this->validateAuthentication(); | 376 | $this->validateAuthentication(); |
370 | $label = $request->query->get('tag', ''); | ||
371 | 377 | ||
372 | $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label); | 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 | |||
373 | $this->getDoctrine() | 394 | $this->getDoctrine() |
374 | ->getRepository('WallabagCoreBundle:Entry') | 395 | ->getRepository('WallabagCoreBundle:Entry') |
375 | ->removeTag($this->getUser()->getId(), $tag); | 396 | ->removeTags($this->getUser()->getId(), $tags); |
376 | 397 | ||
377 | $json = $this->get('serializer')->serialize($tag, 'json'); | 398 | $json = $this->get('serializer')->serialize($tags, 'json'); |
378 | 399 | ||
379 | return $this->renderJsonResponse($json); | 400 | return $this->renderJsonResponse($json); |
380 | } | 401 | } |
381 | 402 | ||
382 | /** | 403 | /** |
383 | * Permanently remove some tags from **every** entry. | 404 | * Permanently remove one tag from **every** entry. |
384 | * | 405 | * |
385 | * @ApiDoc( | 406 | * @ApiDoc( |
386 | * requirements={ | 407 | * requirements={ |
387 | * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="The tags as strings"} | 408 | * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"} |
388 | * } | 409 | * } |
389 | * ) | 410 | * ) |
390 | * | 411 | * |
391 | * @return Response | 412 | * @return Response |
392 | */ | 413 | */ |
393 | public function deleteTagsLabelAction(Request $request) | 414 | public function deleteTagAction(Tag $tag) |
394 | { | 415 | { |
395 | $this->validateAuthentication(); | 416 | $this->validateAuthentication(); |
396 | 417 | ||
397 | $tagsLabels = $request->query->get('tags', ''); | ||
398 | |||
399 | $tags = array(); | ||
400 | |||
401 | foreach (explode(',', $tagsLabels) as $tagLabel) { | ||
402 | $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel); | ||
403 | $tags[] = $tagEntity; | ||
404 | } | ||
405 | |||
406 | $this->getDoctrine() | 418 | $this->getDoctrine() |
407 | ->getRepository('WallabagCoreBundle:Entry') | 419 | ->getRepository('WallabagCoreBundle:Entry') |
408 | ->removeTags($this->getUser()->getId(), $tags); | 420 | ->removeTag($this->getUser()->getId(), $tag); |
409 | 421 | ||
410 | $json = $this->get('serializer')->serialize($tags, 'json'); | 422 | $json = $this->get('serializer')->serialize($tag, 'json'); |
411 | 423 | ||
412 | return $this->renderJsonResponse($json); | 424 | return $this->renderJsonResponse($json); |
413 | } | 425 | } |