From d6fe2a62858a6404667e7838eb90ccf2bacf570c Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Wed, 6 Nov 2019 15:01:39 +0100 Subject: [PATCH] :zap: advance on posting a comment on a poll --- src/Controller/DefaultController.php | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 297a79d..8fd3a6f 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -82,8 +82,7 @@ class DefaultController extends AbstractController { $timeStamp = time() + ( 3600 * 24 * 90 ); // 90 days by default $newpoll->setExpiracyDate( ( new \DateTime() )->setTimestamp( $timeStamp ), new \DateTimeZone( 'Europe/Paris' ) ); - $data = json_decode( $data, true ); -// die( $data ); + $data = json_decode( $data, true ); $em = $this->getDoctrine()->getRepository( Owner::class ); $foundOwner = $em->findOneBy( [ 'email' => $data[ 'owner' ][ 'email' ] ] ); @@ -164,10 +163,26 @@ class DefaultController extends AbstractController { * @Post( * path = "/comment/new", * name = "new_comment", - * requirements = {"content"="\w+"} + * requirements = {"content"="\w+", "poll_id"="\d+"} * ) */ - public function newCommentAction() { + public function newCommentAction( Request $request ) { + $data = $request->getContent(); + + $serializer = SerializerBuilder::create()->build(); + $comment = $serializer->deserialize( $data, 'App\Entity\Comment', 'json' ); + $em = $this->getDoctrine()->getRepository( Poll::class ); + $foundPoll = $em->findOneBy( [ 'id' => $data[ 'poll_id' ] ] ); + $em = $this->getDoctrine()->getRepository( Owner::class ); + $foundOwner = $em->find( 10 ); + $comment->setOwner( $foundOwner ) + ->setPoll( $foundPoll ); + + $em = $this->getDoctrine()->getManager(); + $em->persist( $comment ); +// $em->persist( $foundOwner ); + $em->flush(); + return $this->json( [ 'message' => 'you created a comment', ] );