]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php
Merge pull request #1 from wallabag/master
[github/wallabag/wallabag.git] / src / Wallabag / AnnotationBundle / Controller / WallabagAnnotationController.php
index c13a034ffe58f27c2f7fab0a388ef0e686fed4b6..883ce4a89a3c039f7f91fec3405376bf54d963c7 100644 (file)
@@ -3,10 +3,12 @@
 namespace Wallabag\AnnotationBundle\Controller;
 
 use FOS\RestBundle\Controller\FOSRestController;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
 use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\HttpFoundation\Request;
-use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
 use Wallabag\AnnotationBundle\Entity\Annotation;
+use Wallabag\AnnotationBundle\Form\EditAnnotationType;
+use Wallabag\AnnotationBundle\Form\NewAnnotationType;
 use Wallabag\CoreBundle\Entity\Entry;
 
 class WallabagAnnotationController extends FOSRestController
@@ -14,8 +16,6 @@ class WallabagAnnotationController extends FOSRestController
     /**
      * Retrieve annotations for an entry.
      *
-     * @param Entry $entry
-     *
      * @see Wallabag\ApiBundle\Controller\WallabagRestController
      *
      * @return JsonResponse
@@ -26,10 +26,10 @@ class WallabagAnnotationController extends FOSRestController
             ->getDoctrine()
             ->getRepository('WallabagAnnotationBundle:Annotation')
             ->findAnnotationsByPageId($entry->getId(), $this->getUser()->getId());
-        $total = count($annotationRows);
+        $total = \count($annotationRows);
         $annotations = ['total' => $total, 'rows' => $annotationRows];
 
-        $json = $this->get('serializer')->serialize($annotations, 'json');
+        $json = $this->get('jms_serializer')->serialize($annotations, 'json');
 
         return (new JsonResponse())->setJson($json);
     }
@@ -37,9 +37,6 @@ class WallabagAnnotationController extends FOSRestController
     /**
      * Creates a new annotation.
      *
-     * @param Request $request
-     * @param Entry   $entry
-     *
      * @return JsonResponse
      *
      * @see Wallabag\ApiBundle\Controller\WallabagRestController
@@ -49,25 +46,25 @@ class WallabagAnnotationController extends FOSRestController
         $data = json_decode($request->getContent(), true);
 
         $em = $this->getDoctrine()->getManager();
-
         $annotation = new Annotation($this->getUser());
+        $annotation->setEntry($entry);
 
-        $annotation->setText($data['text']);
-        if (array_key_exists('quote', $data)) {
-            $annotation->setQuote($data['quote']);
-        }
-        if (array_key_exists('ranges', $data)) {
-            $annotation->setRanges($data['ranges']);
-        }
+        $form = $this->get('form.factory')->createNamed('', NewAnnotationType::class, $annotation, [
+            'csrf_protection' => false,
+            'allow_extra_fields' => true,
+        ]);
+        $form->submit($data);
 
-        $annotation->setEntry($entry);
+        if ($form->isValid()) {
+            $em->persist($annotation);
+            $em->flush();
 
-        $em->persist($annotation);
-        $em->flush();
+            $json = $this->get('jms_serializer')->serialize($annotation, 'json');
 
-        $json = $this->get('serializer')->serialize($annotation, 'json');
+            return JsonResponse::fromJsonString($json);
+        }
 
-        return (new JsonResponse())->setJson($json);
+        return $form;
     }
 
     /**
@@ -77,25 +74,29 @@ class WallabagAnnotationController extends FOSRestController
      *
      * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
      *
-     * @param Annotation $annotation
-     * @param Request    $request
-     *
      * @return JsonResponse
      */
     public function putAnnotationAction(Annotation $annotation, Request $request)
     {
         $data = json_decode($request->getContent(), true);
 
-        if (!is_null($data['text'])) {
-            $annotation->setText($data['text']);
-        }
+        $form = $this->get('form.factory')->createNamed('', EditAnnotationType::class, $annotation, [
+            'csrf_protection' => false,
+            'allow_extra_fields' => true,
+        ]);
+        $form->submit($data);
 
-        $em = $this->getDoctrine()->getManager();
-        $em->flush();
+        if ($form->isValid()) {
+            $em = $this->getDoctrine()->getManager();
+            $em->persist($annotation);
+            $em->flush();
 
-        $json = $this->get('serializer')->serialize($annotation, 'json');
+            $json = $this->get('jms_serializer')->serialize($annotation, 'json');
 
-        return (new JsonResponse())->setJson($json);
+            return JsonResponse::fromJsonString($json);
+        }
+
+        return $form;
     }
 
     /**
@@ -105,8 +106,6 @@ class WallabagAnnotationController extends FOSRestController
      *
      * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
      *
-     * @param Annotation $annotation
-     *
      * @return JsonResponse
      */
     public function deleteAnnotationAction(Annotation $annotation)
@@ -115,7 +114,7 @@ class WallabagAnnotationController extends FOSRestController
         $em->remove($annotation);
         $em->flush();
 
-        $json = $this->get('serializer')->serialize($annotation, 'json');
+        $json = $this->get('jms_serializer')->serialize($annotation, 'json');
 
         return (new JsonResponse())->setJson($json);
     }