From 130cd313c07fc566aee5e51a33730c836e500d33 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Tue, 12 Nov 2019 11:52:09 +0100 Subject: [PATCH] add comment action with a new owner --- src/Controller/DefaultController.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 2582301..c3c253f 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -171,6 +171,7 @@ class DefaultController extends AbstractController { } /** + * add a comment on a poll * @Post( * path = "poll/{id}/comment", * name = "new_comment", @@ -186,12 +187,24 @@ class DefaultController extends AbstractController { $serializer = SerializerBuilder::create()->build(); $comment = $serializer->deserialize( $data, 'App\Entity\Comment', 'json' ); - $em = $this->getDoctrine()->getRepository( Owner::class ); - $foundOwner = $em->find( 10 ); + $em = $this->getDoctrine()->getRepository( Owner::class ); + + $data = json_decode( $data, true ); + + $foundOwner = $em->findByEmail( $data[ 'owner' ][ 'email' ] ); + // manage existing or new Owner + if ( ! $foundOwner ) { + $foundOwner = new Owner(); + $foundOwner->setPseudo( $data[ 'owner' ][ 'email' ] ) + ->setEmail( $data[ 'owner' ][ 'email' ] ) + ->setModifierToken( uniqid() ); + } $comment->setOwner( $foundOwner ) ->setPoll( $poll ); + $foundOwner->addComment( $comment ); $em = $this->getDoctrine()->getManager(); + $em->persist( $foundOwner ); $em->persist( $comment ); $em->flush();