move code

This commit is contained in:
Thomas Citharel 2016-10-05 19:16:57 +02:00 committed by Jeremy Benoist
parent c7935f32d2
commit 1eea248bb0
No known key found for this signature in database
GPG Key ID: BCA73962457ACC3C
2 changed files with 48 additions and 82 deletions

View File

@ -3,6 +3,7 @@
namespace Wallabag\AnnotationBundle\Controller; namespace Wallabag\AnnotationBundle\Controller;
use FOS\RestBundle\Controller\FOSRestController; use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
@ -18,30 +19,30 @@ class WallabagAnnotationController extends FOSRestController
* *
* @see Wallabag\ApiBundle\Controller\WallabagRestController * @see Wallabag\ApiBundle\Controller\WallabagRestController
* *
* @return Response * @return JsonResponse
*/ */
public function getAnnotationsAction(Entry $entry) public function getAnnotationsAction(Entry $entry)
{ {
$annotationRows = $this $annotationRows = $this
->getDoctrine() ->getDoctrine()
->getRepository('WallabagAnnotationBundle:Annotation') ->getRepository('WallabagAnnotationBundle:Annotation')
->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId()); ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId());
$total = count($annotationRows); $total = count($annotationRows);
$annotations = ['total' => $total, 'rows' => $annotationRows]; $annotations = array('total' => $total, 'rows' => $annotationRows);
$json = $this->get('serializer')->serialize($annotations, 'json'); $json = $this->get('serializer')->serialize($annotations, 'json');
return $this->renderJsonResponse($json); return (new JsonResponse())->setJson($json);
} }
/** /**
* Creates a new annotation. * Creates a new annotation.
* *
* @param Request $request
* @param Entry $entry * @param Entry $entry
* * @return JsonResponse
* @see Wallabag\ApiBundle\Controller\WallabagRestController * @see Wallabag\ApiBundle\Controller\WallabagRestController
* *
* @return Response
*/ */
public function postAnnotationAction(Request $request, Entry $entry) public function postAnnotationAction(Request $request, Entry $entry)
{ {
@ -66,7 +67,7 @@ class WallabagAnnotationController extends FOSRestController
$json = $this->get('serializer')->serialize($annotation, 'json'); $json = $this->get('serializer')->serialize($annotation, 'json');
return $this->renderJsonResponse($json); return (new JsonResponse())->setJson($json);
} }
/** /**
@ -76,7 +77,9 @@ class WallabagAnnotationController extends FOSRestController
* *
* @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
* *
* @return Response * @param Annotation $annotation
* @param Request $request
* @return JsonResponse
*/ */
public function putAnnotationAction(Annotation $annotation, Request $request) public function putAnnotationAction(Annotation $annotation, Request $request)
{ {
@ -91,7 +94,7 @@ class WallabagAnnotationController extends FOSRestController
$json = $this->get('serializer')->serialize($annotation, 'json'); $json = $this->get('serializer')->serialize($annotation, 'json');
return $this->renderJsonResponse($json); return (new JsonResponse())->setJson($json);
} }
/** /**
@ -101,7 +104,8 @@ class WallabagAnnotationController extends FOSRestController
* *
* @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
* *
* @return Response * @param Annotation $annotation
* @return JsonResponse
*/ */
public function deleteAnnotationAction(Annotation $annotation) public function deleteAnnotationAction(Annotation $annotation)
{ {
@ -111,19 +115,6 @@ class WallabagAnnotationController extends FOSRestController
$json = $this->get('serializer')->serialize($annotation, 'json'); $json = $this->get('serializer')->serialize($annotation, 'json');
return $this->renderJsonResponse($json); return (new JsonResponse())->setJson($json);
}
/**
* Send a JSON Response.
* We don't use the Symfony JsonRespone, because it takes an array as parameter instead of a JSON string.
*
* @param string $json
*
* @return Response
*/
private function renderJsonResponse($json, $code = 200)
{
return new Response($json, $code, ['application/json']);
} }
} }

View File

@ -528,29 +528,26 @@ class WallabagRestController extends FOSRestController
* } * }
* ) * )
* *
* @return Response * @param Entry $entry
* @return JsonResponse
*/ */
public function getAnnotationsAction(Entry $entry) public function getAnnotationsAction(Entry $entry)
{ {
$this->validateAuthentication(); $this->validateAuthentication();
$annotationRows = $this $response = $this->forward('WallabagApiBundle:WallabagRest:getAnnotations',
->getDoctrine() [
->getRepository('WallabagAnnotationBundle:Annotation') 'entry' => $entry
->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId()); ]);
$total = count($annotationRows); return $response;
$annotations = array('total' => $total, 'rows' => $annotationRows);
$json = $this->get('serializer')->serialize($annotations, 'json');
return $this->renderJsonResponse($json);
} }
/** /**
* Creates a new annotation. * Creates a new annotation.
* *
* @param Request $request
* @param Entry $entry * @param Entry $entry
* * @return JsonResponse
* @ApiDoc( * @ApiDoc(
* requirements={ * requirements={
* {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"}, * {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"},
@ -559,34 +556,17 @@ class WallabagRestController extends FOSRestController
* } * }
* ) * )
* *
* @return Response
*/ */
public function postAnnotationAction(Request $request, Entry $entry) public function postAnnotationAction(Request $request, Entry $entry)
{ {
$this->validateAuthentication(); $this->validateAuthentication();
$data = json_decode($request->getContent(), true); $response = $this->forward('WallabagApiBundle:WallabagRest:postAnnotation',
[
$em = $this->getDoctrine()->getManager(); 'request' => $request,
'entry' => $entry
$annotation = new Annotation($this->getUser()); ]);
return $response;
$annotation->setText($data['text']);
if (array_key_exists('quote', $data)) {
$annotation->setQuote($data['quote']);
}
if (array_key_exists('ranges', $data)) {
$annotation->setRanges($data['ranges']);
}
$annotation->setEntry($entry);
$em->persist($annotation);
$em->flush();
$json = $this->get('serializer')->serialize($annotation, 'json');
return $this->renderJsonResponse($json);
} }
/** /**
@ -600,24 +580,20 @@ class WallabagRestController extends FOSRestController
* *
* @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
* *
* @return Response * @param Annotation $annotation
* @param Request $request
* @return JsonResponse
*/ */
public function putAnnotationAction(Annotation $annotation, Request $request) public function putAnnotationAction(Annotation $annotation, Request $request)
{ {
$this->validateAuthentication(); $this->validateAuthentication();
$data = json_decode($request->getContent(), true); $response = $this->forward('WallabagApiBundle:WallabagRest:putAnnotation',
[
if (!is_null($data['text'])) { 'annotation' => $annotation,
$annotation->setText($data['text']); 'request' => $request
} ]);
return $response;
$em = $this->getDoctrine()->getManager();
$em->flush();
$json = $this->get('serializer')->serialize($annotation, 'json');
return $this->renderJsonResponse($json);
} }
/** /**
@ -631,19 +607,18 @@ class WallabagRestController extends FOSRestController
* *
* @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation") * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
* *
* @return Response * @param Annotation $annotation
* @return JsonResponse
*/ */
public function deleteAnnotationAction(Annotation $annotation) public function deleteAnnotationAction(Annotation $annotation)
{ {
$this->validateAuthentication(); $this->validateAuthentication();
$em = $this->getDoctrine()->getManager(); $response = $this->forward('WallabagApiBundle:WallabagRest:deleteAnnotation',
$em->remove($annotation); [
$em->flush(); 'annotation' => $annotation,
]);
$json = $this->get('serializer')->serialize($annotation, 'json'); return $response;
return $this->renderJsonResponse($json);
} }
/** /**