diff options
author | Thomas Citharel <tcit@tcit.fr> | 2016-10-05 19:16:57 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-10-22 09:06:07 +0200 |
commit | 1eea248bb0ebf49772878557211817905a4d6952 (patch) | |
tree | 987bf28959b70a469f6df95303dc462da14bd372 | |
parent | c7935f32d2cd7fd29dbc7b07c8f206f6e8b64fb4 (diff) | |
download | wallabag-1eea248bb0ebf49772878557211817905a4d6952.tar.gz wallabag-1eea248bb0ebf49772878557211817905a4d6952.tar.zst wallabag-1eea248bb0ebf49772878557211817905a4d6952.zip |
move code
-rw-r--r-- | src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php | 43 | ||||
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/WallabagRestController.php | 87 |
2 files changed, 48 insertions, 82 deletions
diff --git a/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php b/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php index 4143c1b6..b04c0bc2 100644 --- a/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php +++ b/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php | |||
@@ -3,6 +3,7 @@ | |||
3 | namespace Wallabag\AnnotationBundle\Controller; | 3 | namespace Wallabag\AnnotationBundle\Controller; |
4 | 4 | ||
5 | use FOS\RestBundle\Controller\FOSRestController; | 5 | use FOS\RestBundle\Controller\FOSRestController; |
6 | use Symfony\Component\HttpFoundation\JsonResponse; | ||
6 | use Symfony\Component\HttpFoundation\Request; | 7 | use Symfony\Component\HttpFoundation\Request; |
7 | use Symfony\Component\HttpFoundation\Response; | 8 | use Symfony\Component\HttpFoundation\Response; |
8 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; | 9 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
@@ -18,30 +19,30 @@ class WallabagAnnotationController extends FOSRestController | |||
18 | * | 19 | * |
19 | * @see Wallabag\ApiBundle\Controller\WallabagRestController | 20 | * @see Wallabag\ApiBundle\Controller\WallabagRestController |
20 | * | 21 | * |
21 | * @return Response | 22 | * @return JsonResponse |
22 | */ | 23 | */ |
23 | public function getAnnotationsAction(Entry $entry) | 24 | public function getAnnotationsAction(Entry $entry) |
24 | { | 25 | { |
25 | $annotationRows = $this | 26 | $annotationRows = $this |
26 | ->getDoctrine() | 27 | ->getDoctrine() |
27 | ->getRepository('WallabagAnnotationBundle:Annotation') | 28 | ->getRepository('WallabagAnnotationBundle:Annotation') |
28 | ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId()); | 29 | ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId()); |
29 | $total = count($annotationRows); | 30 | $total = count($annotationRows); |
30 | $annotations = ['total' => $total, 'rows' => $annotationRows]; | 31 | $annotations = array('total' => $total, 'rows' => $annotationRows); |
31 | 32 | ||
32 | $json = $this->get('serializer')->serialize($annotations, 'json'); | 33 | $json = $this->get('serializer')->serialize($annotations, 'json'); |
33 | 34 | ||
34 | return $this->renderJsonResponse($json); | 35 | return (new JsonResponse())->setJson($json); |
35 | } | 36 | } |
36 | 37 | ||
37 | /** | 38 | /** |
38 | * Creates a new annotation. | 39 | * Creates a new annotation. |
39 | * | 40 | * |
41 | * @param Request $request | ||
40 | * @param Entry $entry | 42 | * @param Entry $entry |
41 | * | 43 | * @return JsonResponse |
42 | * @see Wallabag\ApiBundle\Controller\WallabagRestController | 44 | * @see Wallabag\ApiBundle\Controller\WallabagRestController |
43 | * | 45 | * |
44 | * @return Response | ||
45 | */ | 46 | */ |
46 | public function postAnnotationAction(Request $request, Entry $entry) | 47 | public function postAnnotationAction(Request $request, Entry $entry) |
47 | { | 48 | { |
@@ -66,7 +67,7 @@ class WallabagAnnotationController extends FOSRestController | |||
66 | 67 | ||
67 | $json = $this->get('serializer')->serialize($annotation, 'json'); | 68 | $json = $this->get('serializer')->serialize($annotation, 'json'); |
68 | 69 | ||
69 | return $this->renderJsonResponse($json); | 70 | return (new JsonResponse())->setJson($json); |
70 | } | 71 | } |
71 | 72 | ||
72 | /** | 73 | /** |
@@ -76,7 +77,9 @@ class WallabagAnnotationController extends FOSRestController | |||
76 | * | 77 | * |
77 | * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") | 78 | * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") |
78 | * | 79 | * |
79 | * @return Response | 80 | * @param Annotation $annotation |
81 | * @param Request $request | ||
82 | * @return JsonResponse | ||
80 | */ | 83 | */ |
81 | public function putAnnotationAction(Annotation $annotation, Request $request) | 84 | public function putAnnotationAction(Annotation $annotation, Request $request) |
82 | { | 85 | { |
@@ -91,7 +94,7 @@ class WallabagAnnotationController extends FOSRestController | |||
91 | 94 | ||
92 | $json = $this->get('serializer')->serialize($annotation, 'json'); | 95 | $json = $this->get('serializer')->serialize($annotation, 'json'); |
93 | 96 | ||
94 | return $this->renderJsonResponse($json); | 97 | return (new JsonResponse())->setJson($json); |
95 | } | 98 | } |
96 | 99 | ||
97 | /** | 100 | /** |
@@ -101,7 +104,8 @@ class WallabagAnnotationController extends FOSRestController | |||
101 | * | 104 | * |
102 | * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") | 105 | * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") |
103 | * | 106 | * |
104 | * @return Response | 107 | * @param Annotation $annotation |
108 | * @return JsonResponse | ||
105 | */ | 109 | */ |
106 | public function deleteAnnotationAction(Annotation $annotation) | 110 | public function deleteAnnotationAction(Annotation $annotation) |
107 | { | 111 | { |
@@ -111,19 +115,6 @@ class WallabagAnnotationController extends FOSRestController | |||
111 | 115 | ||
112 | $json = $this->get('serializer')->serialize($annotation, 'json'); | 116 | $json = $this->get('serializer')->serialize($annotation, 'json'); |
113 | 117 | ||
114 | return $this->renderJsonResponse($json); | 118 | return (new JsonResponse())->setJson($json); |
115 | } | ||
116 | |||
117 | /** | ||
118 | * Send a JSON Response. | ||
119 | * We don't use the Symfony JsonRespone, because it takes an array as parameter instead of a JSON string. | ||
120 | * | ||
121 | * @param string $json | ||
122 | * | ||
123 | * @return Response | ||
124 | */ | ||
125 | private function renderJsonResponse($json, $code = 200) | ||
126 | { | ||
127 | return new Response($json, $code, ['application/json']); | ||
128 | } | 119 | } |
129 | } | 120 | } |
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index 07d0e1e9..e2e4924b 100644 --- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php | |||
@@ -528,29 +528,26 @@ class WallabagRestController extends FOSRestController | |||
528 | * } | 528 | * } |
529 | * ) | 529 | * ) |
530 | * | 530 | * |
531 | * @return Response | 531 | * @param Entry $entry |
532 | * @return JsonResponse | ||
532 | */ | 533 | */ |
533 | public function getAnnotationsAction(Entry $entry) | 534 | public function getAnnotationsAction(Entry $entry) |
534 | { | 535 | { |
535 | $this->validateAuthentication(); | 536 | $this->validateAuthentication(); |
536 | 537 | ||
537 | $annotationRows = $this | 538 | $response = $this->forward('WallabagApiBundle:WallabagRest:getAnnotations', |
538 | ->getDoctrine() | 539 | [ |
539 | ->getRepository('WallabagAnnotationBundle:Annotation') | 540 | 'entry' => $entry |
540 | ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId()); | 541 | ]); |
541 | $total = count($annotationRows); | 542 | return $response; |
542 | $annotations = array('total' => $total, 'rows' => $annotationRows); | ||
543 | |||
544 | $json = $this->get('serializer')->serialize($annotations, 'json'); | ||
545 | |||
546 | return $this->renderJsonResponse($json); | ||
547 | } | 543 | } |
548 | 544 | ||
549 | /** | 545 | /** |
550 | * Creates a new annotation. | 546 | * Creates a new annotation. |
551 | * | 547 | * |
548 | * @param Request $request | ||
552 | * @param Entry $entry | 549 | * @param Entry $entry |
553 | * | 550 | * @return JsonResponse |
554 | * @ApiDoc( | 551 | * @ApiDoc( |
555 | * requirements={ | 552 | * requirements={ |
556 | * {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"}, | 553 | * {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"}, |
@@ -559,34 +556,17 @@ class WallabagRestController extends FOSRestController | |||
559 | * } | 556 | * } |
560 | * ) | 557 | * ) |
561 | * | 558 | * |
562 | * @return Response | ||
563 | */ | 559 | */ |
564 | public function postAnnotationAction(Request $request, Entry $entry) | 560 | public function postAnnotationAction(Request $request, Entry $entry) |
565 | { | 561 | { |
566 | $this->validateAuthentication(); | 562 | $this->validateAuthentication(); |
567 | 563 | ||
568 | $data = json_decode($request->getContent(), true); | 564 | $response = $this->forward('WallabagApiBundle:WallabagRest:postAnnotation', |
569 | 565 | [ | |
570 | $em = $this->getDoctrine()->getManager(); | 566 | 'request' => $request, |
571 | 567 | 'entry' => $entry | |
572 | $annotation = new Annotation($this->getUser()); | 568 | ]); |
573 | 569 | return $response; | |
574 | $annotation->setText($data['text']); | ||
575 | if (array_key_exists('quote', $data)) { | ||
576 | $annotation->setQuote($data['quote']); | ||
577 | } | ||
578 | if (array_key_exists('ranges', $data)) { | ||
579 | $annotation->setRanges($data['ranges']); | ||
580 | } | ||
581 | |||
582 | $annotation->setEntry($entry); | ||
583 | |||
584 | $em->persist($annotation); | ||
585 | $em->flush(); | ||
586 | |||
587 | $json = $this->get('serializer')->serialize($annotation, 'json'); | ||
588 | |||
589 | return $this->renderJsonResponse($json); | ||
590 | } | 570 | } |
591 | 571 | ||
592 | /** | 572 | /** |
@@ -600,24 +580,20 @@ class WallabagRestController extends FOSRestController | |||
600 | * | 580 | * |
601 | * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") | 581 | * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") |
602 | * | 582 | * |
603 | * @return Response | 583 | * @param Annotation $annotation |
584 | * @param Request $request | ||
585 | * @return JsonResponse | ||
604 | */ | 586 | */ |
605 | public function putAnnotationAction(Annotation $annotation, Request $request) | 587 | public function putAnnotationAction(Annotation $annotation, Request $request) |
606 | { | 588 | { |
607 | $this->validateAuthentication(); | 589 | $this->validateAuthentication(); |
608 | 590 | ||
609 | $data = json_decode($request->getContent(), true); | 591 | $response = $this->forward('WallabagApiBundle:WallabagRest:putAnnotation', |
610 | 592 | [ | |
611 | if (!is_null($data['text'])) { | 593 | 'annotation' => $annotation, |
612 | $annotation->setText($data['text']); | 594 | 'request' => $request |
613 | } | 595 | ]); |
614 | 596 | return $response; | |
615 | $em = $this->getDoctrine()->getManager(); | ||
616 | $em->flush(); | ||
617 | |||
618 | $json = $this->get('serializer')->serialize($annotation, 'json'); | ||
619 | |||
620 | return $this->renderJsonResponse($json); | ||
621 | } | 597 | } |
622 | 598 | ||
623 | /** | 599 | /** |
@@ -631,19 +607,18 @@ class WallabagRestController extends FOSRestController | |||
631 | * | 607 | * |
632 | * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") | 608 | * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") |
633 | * | 609 | * |
634 | * @return Response | 610 | * @param Annotation $annotation |
611 | * @return JsonResponse | ||
635 | */ | 612 | */ |
636 | public function deleteAnnotationAction(Annotation $annotation) | 613 | public function deleteAnnotationAction(Annotation $annotation) |
637 | { | 614 | { |
638 | $this->validateAuthentication(); | 615 | $this->validateAuthentication(); |
639 | 616 | ||
640 | $em = $this->getDoctrine()->getManager(); | 617 | $response = $this->forward('WallabagApiBundle:WallabagRest:deleteAnnotation', |
641 | $em->remove($annotation); | 618 | [ |
642 | $em->flush(); | 619 | 'annotation' => $annotation, |
643 | 620 | ]); | |
644 | $json = $this->get('serializer')->serialize($annotation, 'json'); | 621 | return $response; |
645 | |||
646 | return $this->renderJsonResponse($json); | ||
647 | } | 622 | } |
648 | 623 | ||
649 | /** | 624 | /** |