From d63929eb93f6292cb8aa88b496d1d1c3fa1c6952 Mon Sep 17 00:00:00 2001 From: tykayn Date: Wed, 28 Apr 2021 16:20:40 +0200 Subject: [PATCH] find a poll by custom url in vote stack add action --- src/Controller/api/VoteController.php | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Controller/api/VoteController.php b/src/Controller/api/VoteController.php index 3ff2a01..770b555 100644 --- a/src/Controller/api/VoteController.php +++ b/src/Controller/api/VoteController.php @@ -33,18 +33,21 @@ class VoteController extends EmailsController { * ) * * @param SerializerInterface $serializer - * @param Poll $poll + * @param string $custom_url * @param Request $request * * @return JsonResponse|Response */ public function newVoteStackAction( SerializerInterface $serializer, - Poll $poll, + string $custom_url, Request $request ) { + $entityManager = $this->getDoctrine()->getManager(); + $poll = $entityManager->findOneByCustomUrl(Poll::class, $custom_url); + if ( ! $poll ) { - return $this->json( [ 'message' => 'poll not found' ], 404 ); + return $this->json( [ 'message' => 'poll "'.$custom_url.'" not found' ], 404 ); } $em = $this->getDoctrine()->getManager(); @@ -67,13 +70,13 @@ class VoteController extends EmailsController { } // TODO anti flood $foundOwner - ->setModifierToken( $poll->generateRandomKey() ); + ->setModifierToken( $custom_url->generateRandomKey() ); $stack = new StackOfVotes(); $stack ->setOwner( $foundOwner ) ->setIp( $_SERVER[ 'REMOTE_ADDR' ] ) ->setPseudo( $data[ 'pseudo' ] ) - ->setPoll( $poll ); + ->setPoll( $custom_url ); foreach ( $data[ 'votes' ] as $voteInfo ) { if ( ! isset( $voteInfo[ 'value' ] ) ) { @@ -97,28 +100,28 @@ class VoteController extends EmailsController { ], 404 ); } - $vote->setPoll( $poll ) + $vote->setPoll( $custom_url ) ->setChoice( $foundChoice ) ->setValue( $voteInfo[ 'value' ] ); - $vote->setPoll( $poll ); + $vote->setPoll( $custom_url ); $stack->addVote( $vote ); - $poll->addVote( $vote ); + $custom_url->addVote( $vote ); $em->persist( $vote ); $em->persist( $foundChoice ); } // find poll from choices - $poll->addStackOfVote( $stack ); + $custom_url->addStackOfVote( $stack ); $em->persist( $stack ); - $em->persist( $poll ); + $em->persist( $custom_url ); $em->flush(); $precision = ''; if ( $existingOwner ) { $precision = ' from an existing owner : ' . $foundOwner->getEmail(); } - $stacks = $poll->getStacksOfVotes(); + $stacks = $custom_url->getStacksOfVotes(); - if ( $poll->getMailOnVote() ) { + if ( $custom_url->getMailOnVote() ) { $this->sendVoteNotificationAction( $stack->getOwner(), $stack ); }