1
0
mirror of https://framagit.org/tykayn/date-poll-api synced 2023-08-25 08:23:11 +02:00

add comment action with a new owner

This commit is contained in:
Baptiste Lemoine 2019-11-12 11:52:09 +01:00
parent f3d7b0773d
commit 130cd313c0

View File

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