From 272588d00013ccee2858743460aec165d7524ae2 Mon Sep 17 00:00:00 2001 From: tykayn Date: Wed, 28 Apr 2021 18:40:04 +0200 Subject: [PATCH] update doctrine managerregistry --- src/Controller/api/VoteController.php | 452 ++++++++++++---------- src/Repository/ChoiceRepository.php | 30 +- src/Repository/CommentRepository.php | 2 +- src/Repository/OwnerRepository.php | 2 +- src/Repository/StackOfVotesRepository.php | 2 +- src/Repository/VoteRepository.php | 2 +- 6 files changed, 250 insertions(+), 240 deletions(-) diff --git a/src/Controller/api/VoteController.php b/src/Controller/api/VoteController.php index 770b555..dfdb04c 100644 --- a/src/Controller/api/VoteController.php +++ b/src/Controller/api/VoteController.php @@ -1,226 +1,264 @@ getDoctrine()->getManager(); - $poll = $entityManager->findOneByCustomUrl(Poll::class, $custom_url); + class VoteController extends EmailsController { - if ( ! $poll ) { - return $this->json( [ 'message' => 'poll "'.$custom_url.'" not found' ], 404 ); - } - - $em = $this->getDoctrine()->getManager(); - $data = $request->getContent(); - $data = json_decode( $data, true ); + /** + * add a vote stack on a poll + * @Post( + * path = "/poll/{custom_url}/answer", + * name = "new_vote_stack", + * ) + * + * @param SerializerInterface $serializer + * @param string $custom_url + * @param Request $request + * + * @return JsonResponse|Response + */ + public function newVoteStackAction( + SerializerInterface $serializer, + string $custom_url, + Request $request, + ChoiceRepository $choice_repository, + ) { - $emOwner = $this->getDoctrine()->getRepository( Owner::class ); - $emChoice = $this->getDoctrine()->getRepository( Choice::class ); - $existingOwner = false; - $foundOwner = $emOwner->findOneByEmail( trim( $data[ 'email' ] ) ); - // manage existing or new Owner - if ( ! $foundOwner ) { - $foundOwner = new Owner(); - $foundOwner - ->setEmail( $data[ 'email' ] ) - ->setPseudo( $data[ 'pseudo' ] ); - } else { - $existingOwner = true; - } - // TODO anti flood - $foundOwner - ->setModifierToken( $custom_url->generateRandomKey() ); - $stack = new StackOfVotes(); - $stack - ->setOwner( $foundOwner ) - ->setIp( $_SERVER[ 'REMOTE_ADDR' ] ) - ->setPseudo( $data[ 'pseudo' ] ) - ->setPoll( $custom_url ); - foreach ( $data[ 'votes' ] as $voteInfo ) { + $em = $this->getDoctrine()->getManager(); + $emPol = $em->getRepository( Poll::class ); + $poll = $emPol->findOneByCustomUrl( $custom_url ); - if ( ! isset( $voteInfo[ 'value' ] ) ) { - continue; + if ( ! $poll ) { + return $this->json( [ 'message' => 'poll "' . $custom_url . '" not found' ], 404 ); } - $allowedValuesToAnswer = [ 'yes', 'maybe', 'no' ]; - if ( ! in_array( $voteInfo[ 'value' ], $allowedValuesToAnswer ) ) { - return $this->json( [ - 'message' => 'answer ' . $voteInfo[ 'value' ] . ' is not allowed. should be yes, maybe, or no.', - 'vote_stack' => $stack, - ], - 404 ); + $data = $request->getContent(); + $data = json_decode( $data, true ); +// $data = $data['data']; + + $newStack = new StackOfVotes(); + $newStack + ->setPseudo( $data[ 'pseudo' ] ) + ->setOwner( new Owner() ); + + // TODO manage new comment + $emChoice = $choice_repository; + + foreach ( $data[ 'votes' ] as $vote ) { + $newVote = new Vote(); + $newStack->addVote( $newVote ); + $choiceFound = $emChoice->find( $vote[ 'choice_id' ] ); + if ( $choiceFound ) { + + $choiceFound->addVote( $newVote ); + $newVote->setStacksOfVotes( $newStack ) + ->setChoice( $choiceFound ) + ->setValue( $vote[ 'value' ] ); + $em->persist( $choiceFound ); + } else { + throw new NotFoundHttpException( 'no choice of id' . $vote[ 'choice_id' ] ); + } + + $em->persist( $newVote ); } - $vote = new Vote(); - $foundChoice = $emChoice->find( $voteInfo[ 'choice_id' ] ); - if ( ! $foundChoice ) { - return $this->json( [ - 'message' => 'choice ' . $voteInfo[ 'choice_id' ] . ' was not found', - 'vote_stack' => $stack, - ], - 404 ); - } - $vote->setPoll( $custom_url ) - ->setChoice( $foundChoice ) - ->setValue( $voteInfo[ 'value' ] ); - $vote->setPoll( $custom_url ); - $stack->addVote( $vote ); - $custom_url->addVote( $vote ); - $em->persist( $vote ); - $em->persist( $foundChoice ); - } - - // find poll from choices - $custom_url->addStackOfVote( $stack ); - $em->persist( $stack ); - $em->persist( $custom_url ); - $em->flush(); - $precision = ''; - if ( $existingOwner ) { - $precision = ' from an existing owner : ' . $foundOwner->getEmail(); - } - $stacks = $custom_url->getStacksOfVotes(); - - if ( $custom_url->getMailOnVote() ) { - $this->sendVoteNotificationAction( $stack->getOwner(), $stack ); - } - - $returnedVoteStack = $stack; - - $jsonResponse = $serializer->serialize( $returnedVoteStack, 'json' ); - - $response = new Response( $jsonResponse ); - $response->headers->set( 'Content-Type', 'application/json' ); - $response->setStatusCode( 200 ); - - return $response; - - } - - /** - * update vote stack - * @Patch( - * path = "/vote-stack/{id}/token/{modifierToken}", - * name = "update_vote_stack", - * requirements = { "id"="\d+"} - * ) - * - * @param SerializerInterface $serializer - * @param StackOfVotes $id - * @param $modifierToken - * @param Request $request - * - * @return JsonResponse|Response - */ - public - function updateVoteStackAction( - SerializerInterface $serializer, - StackOfVotes $id, - $modifierToken, - Request $request - ) { - $voteStack = $id; - if ( ! $voteStack ) { - return $this->json( [ 'message' => 'vote stack not found' ], 404 ); - } - $poll = $voteStack->getPoll(); - - // if only self users are allowed to modify a vote, check it - if ( ! $modifierToken || $voteStack->getOwner()->getModifierToken() !== $modifierToken ) { - return $this->json( [ 'message' => 'your token does not allow you to modify this vote ' ], - 403 ); - } - // everything is ok, we can update all the votes of the vote stack - //TODO - // match votes and choices - // update answers - // save evrything - - - $jsonResponse = $serializer->serialize( [ - 'message' => 'ok', - 'modifier_token' => $voteStack->getOwner()->getModifierToken(), - 'vote_stack' => $voteStack, - ], - 'json' ); - - $response = new Response( $jsonResponse ); - $response->headers->set( 'Content-Type', 'application/json' ); - $response->setStatusCode( 200 ); - - return $response; - - - } - - /** - * @Delete( - * path = "/poll/{id}/votes/{accessToken}", - * name = "poll_votes_delete", - * requirements = {"accessToken"="\w+", "poll_id"="\d+"} - * ) - * @return JsonResponse - */ - public - function deletePollVotesAction( - Poll $poll, - $accessToken - ) { - if ( $accessToken == $poll->getAdminKey() ) { - $em = $this->getDoctrine()->getManager(); - $length = count( $poll->getVotes() ); - $em->remove( $poll->getVotes() ); + $newStack + ->setPoll( $poll ); + $em->persist( $newStack ); + $em->persist( $poll ); $em->flush(); + return $this->json( [ - 'message' => 'boom! les ' . $length . ' votes du sondage ont été supprimés', + 'data' => $data, + ] ); +// $emOwner = $em->getRepository( Owner::class ); +// $emChoice = $em->getRepository( Choice::class ); +// $existingOwner = false; +// $foundOwner = $emOwner->findOneByEmail( trim( $data[ 'email' ] ) ); + // manage existing or new Owner +// if ( ! $foundOwner ) { +// $foundOwner = new Owner(); +// $foundOwner +// ->setEmail( $data[ 'email' ] ) +// ->setPseudo( $data[ 'pseudo' ] ); +// } else { +// $existingOwner = true; +// } + // TODO anti flood +// $foundOwner +// ->setModifierToken( $poll->generateRandomKey() ); +// $stack = new StackOfVotes(); +// $stack +// ->setOwner( $foundOwner ) +// ->setIp( $_SERVER[ 'REMOTE_ADDR' ] ) +// ->setPseudo( $data[ 'pseudo' ] ) +// ->setPoll( $custom_url ); +// foreach ( $data[ 'votes' ] as $voteInfo ) { +// +// if ( ! isset( $voteInfo[ 'value' ] ) ) { +// continue; +// } +// $allowedValuesToAnswer = [ 'yes', 'maybe', 'no' ]; +// +// if ( ! in_array( $voteInfo[ 'value' ], $allowedValuesToAnswer ) ) { +// return $this->json( [ +// 'message' => 'answer ' . $voteInfo[ 'value' ] . ' is not allowed. should be yes, maybe, or no.', +// 'vote_stack' => $stack, +// ], +// 404 ); +// } +// $vote = new Vote(); +// $foundChoice = $emChoice->find( $voteInfo[ 'choice_id' ] ); +// if ( ! $foundChoice ) { +// return $this->json( [ +// 'message' => 'choice ' . $voteInfo[ 'choice_id' ] . ' was not found', +// 'vote_stack' => $stack, +// ], +// 404 ); +// } +// $vote->setPoll( $poll ) +// ->setChoice( $foundChoice ) +// ->setValue( $voteInfo[ 'value' ] ); +// $vote->setPoll( $poll ); +// $stack->addVote( $vote ); +// $poll->addVote( $vote ); +// $em->persist( $vote ); +// $em->persist( $foundChoice ); +// } +// +// // find poll from choices +// $poll->addStackOfVote( $stack ); +// $em->persist( $stack ); +// $em->persist( $poll ); +// $em->flush(); +// $precision = ''; +// if ( $existingOwner ) { +// $precision = ' from an existing owner : ' . $foundOwner->getEmail(); +// } +// +// if ( $poll->getMailOnVote() ) { +// $this->sendVoteNotificationAction( $stack->getOwner(), $stack ); +// } +// +// $returnedVoteStack = $stack; +// +// $jsonResponse = $serializer->serialize( $returnedVoteStack, 'json' ); +// +// $response = new Response( $jsonResponse ); +// $response->headers->set( 'Content-Type', 'application/json' ); +// $response->setStatusCode( 200 ); +// +// return $response; + + } + + /** + * update vote stack + * @Patch( + * path = "/vote-stack/{id}/token/{modifierToken}", + * name = "update_vote_stack", + * requirements = { "id"="\d+"} + * ) + * + * @param SerializerInterface $serializer + * @param StackOfVotes $id + * @param $modifierToken + * @param Request $request + * + * @return JsonResponse|Response + */ + public + function updateVoteStackAction( + SerializerInterface $serializer, + StackOfVotes $id, + $modifierToken, + Request $request + ) { + $voteStack = $id; + if ( ! $voteStack ) { + return $this->json( [ 'message' => 'vote stack not found' ], 404 ); + } + $poll = $voteStack->getPoll(); + + // if only self users are allowed to modify a vote, check it + if ( ! $modifierToken || $voteStack->getOwner()->getModifierToken() !== $modifierToken ) { + return $this->json( [ 'message' => 'your token does not allow you to modify this vote ' ], + 403 ); + } + // everything is ok, we can update all the votes of the vote stack + //TODO + // match votes and choices + // update answers + // save evrything + + + $jsonResponse = $serializer->serialize( [ + 'message' => 'ok', + 'modifier_token' => $voteStack->getOwner()->getModifierToken(), + 'vote_stack' => $voteStack, ], - 200 ); - } else { - return $this->json( [ - 'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage', - ], - 403 ); + 'json' ); + + $response = new Response( $jsonResponse ); + $response->headers->set( 'Content-Type', 'application/json' ); + $response->setStatusCode( 200 ); + + return $response; + + + } + + /** + * @Delete( + * path = "/poll/{id}/votes/{accessToken}", + * name = "poll_votes_delete", + * requirements = {"accessToken"="\w+", "poll_id"="\d+"} + * ) + * @return JsonResponse + */ + public + function deletePollVotesAction( + Poll $poll, + $accessToken + ) { + if ( $accessToken == $poll->getAdminKey() ) { + $em = $this->getDoctrine()->getManager(); + $length = count( $poll->getVotes() ); + $em->remove( $poll->getVotes() ); + $em->flush(); + + return $this->json( [ + 'message' => 'boom! les ' . $length . ' votes du sondage ont été supprimés', + ], + 200 ); + } else { + return $this->json( [ + 'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage', + ], + 403 ); + } } } -} diff --git a/src/Repository/ChoiceRepository.php b/src/Repository/ChoiceRepository.php index fcc6bac..24a220f 100755 --- a/src/Repository/ChoiceRepository.php +++ b/src/Repository/ChoiceRepository.php @@ -4,7 +4,7 @@ namespace App\Repository; use App\Entity\Choice; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; -use Doctrine\Common\Persistence\ManagerRegistry; +use Doctrine\Persistence\ManagerRegistry; /** * @method Choice|null find( $id, $lockMode = null, $lockVersion = null ) @@ -17,32 +17,4 @@ class ChoiceRepository extends ServiceEntityRepository { parent::__construct( $registry, Choice::class ); } - // /** - // * @return Choice[] Returns an array of Choice objects - // */ - /* - public function findByExampleField($value) - { - return $this->createQueryBuilder('c') - ->andWhere('c.exampleField = :val') - ->setParameter('val', $value) - ->orderBy('c.id', 'ASC') - ->setMaxResults(10) - ->getQuery() - ->getResult() - ; - } - */ - - /* - public function findOneBySomeField($value): ?Choice - { - return $this->createQueryBuilder('c') - ->andWhere('c.exampleField = :val') - ->setParameter('val', $value) - ->getQuery() - ->getOneOrNullResult() - ; - } - */ } diff --git a/src/Repository/CommentRepository.php b/src/Repository/CommentRepository.php index d8a2f2d..4fd6992 100755 --- a/src/Repository/CommentRepository.php +++ b/src/Repository/CommentRepository.php @@ -4,7 +4,7 @@ namespace App\Repository; use App\Entity\Comment; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; -use Doctrine\Common\Persistence\ManagerRegistry; +use Doctrine\Persistence\ManagerRegistry; /** * @method Comment|null find( $id, $lockMode = null, $lockVersion = null ) diff --git a/src/Repository/OwnerRepository.php b/src/Repository/OwnerRepository.php index 64ebfa7..d858d2d 100755 --- a/src/Repository/OwnerRepository.php +++ b/src/Repository/OwnerRepository.php @@ -4,7 +4,7 @@ namespace App\Repository; use App\Entity\Owner; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; -use Doctrine\Common\Persistence\ManagerRegistry; +use Doctrine\Persistence\ManagerRegistry; /** * @method Owner|null find( $id, $lockMode = null, $lockVersion = null ) diff --git a/src/Repository/StackOfVotesRepository.php b/src/Repository/StackOfVotesRepository.php index 1626eed..bedb659 100755 --- a/src/Repository/StackOfVotesRepository.php +++ b/src/Repository/StackOfVotesRepository.php @@ -4,7 +4,7 @@ namespace App\Repository; use App\Entity\StackOfVotes; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; -use Doctrine\Common\Persistence\ManagerRegistry; +use Doctrine\Persistence\ManagerRegistry; /** * @method StackOfVotes|null find( $id, $lockMode = null, $lockVersion = null ) diff --git a/src/Repository/VoteRepository.php b/src/Repository/VoteRepository.php index 358ef53..9691359 100755 --- a/src/Repository/VoteRepository.php +++ b/src/Repository/VoteRepository.php @@ -4,7 +4,7 @@ namespace App\Repository; use App\Entity\Vote; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; -use Doctrine\Common\Persistence\ManagerRegistry; +use Doctrine\Persistence\ManagerRegistry; /** * @method Vote|null find( $id, $lockMode = null, $lockVersion = null )