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();