X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FAnnotationBundle%2FController%2FWallabagAnnotationController.php;h=8d7b6ee9eae786988f4e8f83c5975adb995a8f64;hb=f808b01692a835673f328d7221ba8c212caa9b61;hp=c13a034ffe58f27c2f7fab0a388ef0e686fed4b6;hpb=20da238413d1c6cc360d58a13df33eb199fa5f05;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php b/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php index c13a034f..8d7b6ee9 100644 --- a/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php +++ b/src/Wallabag/AnnotationBundle/Controller/WallabagAnnotationController.php @@ -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 @@ -49,25 +51,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('serializer')->serialize($annotation, 'json'); - $json = $this->get('serializer')->serialize($annotation, 'json'); + return JsonResponse::fromJsonString($json); + } - return (new JsonResponse())->setJson($json); + return $form; } /** @@ -86,16 +88,23 @@ class WallabagAnnotationController extends FOSRestController { $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('serializer')->serialize($annotation, 'json'); - return (new JsonResponse())->setJson($json); + return JsonResponse::fromJsonString($json); + } + + return $form; } /**