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

update doctrine managerregistry

This commit is contained in:
tykayn 2021-04-28 18:40:04 +02:00 committed by Baptiste Lemoine
parent d63929eb93
commit 272588d000
6 changed files with 250 additions and 240 deletions

View File

@ -1,35 +1,36 @@
<?php <?php
namespace App\Controller\api; namespace App\Controller\api;
use App\Controller\EmailsController; use App\Controller\EmailsController;
use App\Entity\Choice; use App\Entity\Choice;
use App\Entity\Owner; use App\Entity\Owner;
use App\Entity\Poll; use App\Entity\Poll;
use App\Entity\StackOfVotes; use App\Entity\StackOfVotes;
use App\Entity\Vote; use App\Entity\Vote;
use FOS\RestBundle\Controller\Annotations\Delete; use App\Repository\ChoiceRepository;
use FOS\RestBundle\Controller\Annotations\Patch; use FOS\RestBundle\Controller\Annotations\Delete;
use FOS\RestBundle\Controller\Annotations\Post; use FOS\RestBundle\Controller\Annotations\Patch;
use FOS\RestBundle\Controller\Annotations\Route; use FOS\RestBundle\Controller\Annotations\Post;
use JMS\Serializer\SerializerInterface; use FOS\RestBundle\Controller\Annotations\Route;
use Symfony\Component\HttpFoundation\JsonResponse; use JMS\Serializer\SerializerInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/** /**
* Class DefaultController * Class DefaultController
* @package App\Controller * @package App\Controller
* @Route("/api/v1",name="api_") * @Route("/api/v1",name="api_")
*/ */
class VoteController extends EmailsController { class VoteController extends EmailsController {
/** /**
* add a vote stack on a poll * add a vote stack on a poll
* @Post( * @Post(
* path = "/poll/{id}/answer", * path = "/poll/{custom_url}/answer",
* name = "new_vote_stack", * name = "new_vote_stack",
* requirements = {"content"="\w+", "poll_id"="\d+"}
* ) * )
* *
* @param SerializerInterface $serializer * @param SerializerInterface $serializer
@ -41,99 +42,136 @@ class VoteController extends EmailsController {
public function newVoteStackAction( public function newVoteStackAction(
SerializerInterface $serializer, SerializerInterface $serializer,
string $custom_url, string $custom_url,
Request $request Request $request,
ChoiceRepository $choice_repository,
) { ) {
$entityManager = $this->getDoctrine()->getManager();
$poll = $entityManager->findOneByCustomUrl(Poll::class, $custom_url);
if ( ! $poll ) {
return $this->json( [ 'message' => 'poll "'.$custom_url.'" not found' ], 404 );
}
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$emPol = $em->getRepository( Poll::class );
$poll = $emPol->findOneByCustomUrl( $custom_url );
if ( ! $poll ) {
return $this->json( [ 'message' => 'poll "' . $custom_url . '" not found' ], 404 );
}
$data = $request->getContent(); $data = $request->getContent();
$data = json_decode( $data, true ); $data = json_decode( $data, true );
// $data = $data['data'];
$newStack = new StackOfVotes();
$emOwner = $this->getDoctrine()->getRepository( Owner::class ); $newStack
$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' ] ) ->setPseudo( $data[ 'pseudo' ] )
->setPoll( $custom_url ); ->setOwner( new Owner() );
foreach ( $data[ 'votes' ] as $voteInfo ) {
if ( ! isset( $voteInfo[ 'value' ] ) ) { // TODO manage new comment
continue; $emChoice = $choice_repository;
}
$allowedValuesToAnswer = [ 'yes', 'maybe', 'no' ];
if ( ! in_array( $voteInfo[ 'value' ], $allowedValuesToAnswer ) ) { foreach ( $data[ 'votes' ] as $vote ) {
return $this->json( [ $newVote = new Vote();
'message' => 'answer ' . $voteInfo[ 'value' ] . ' is not allowed. should be yes, maybe, or no.', $newStack->addVote( $newVote );
'vote_stack' => $stack, $choiceFound = $emChoice->find( $vote[ 'choice_id' ] );
], if ( $choiceFound ) {
404 );
} $choiceFound->addVote( $newVote );
$vote = new Vote(); $newVote->setStacksOfVotes( $newStack )
$foundChoice = $emChoice->find( $voteInfo[ 'choice_id' ] ); ->setChoice( $choiceFound )
if ( ! $foundChoice ) { ->setValue( $vote[ 'value' ] );
return $this->json( [ $em->persist( $choiceFound );
'message' => 'choice ' . $voteInfo[ 'choice_id' ] . ' was not found', } else {
'vote_stack' => $stack, throw new NotFoundHttpException( 'no choice of id' . $vote[ 'choice_id' ] );
],
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 $em->persist( $newVote );
$custom_url->addStackOfVote( $stack ); }
$em->persist( $stack ); $newStack
$em->persist( $custom_url ); ->setPoll( $poll );
$em->persist( $newStack );
$em->persist( $poll );
$em->flush(); $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; return $this->json( [
'data' => $data,
$jsonResponse = $serializer->serialize( $returnedVoteStack, 'json' ); ] );
// $emOwner = $em->getRepository( Owner::class );
$response = new Response( $jsonResponse ); // $emChoice = $em->getRepository( Choice::class );
$response->headers->set( 'Content-Type', 'application/json' ); // $existingOwner = false;
$response->setStatusCode( 200 ); // $foundOwner = $emOwner->findOneByEmail( trim( $data[ 'email' ] ) );
// manage existing or new Owner
return $response; // 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;
} }
@ -223,4 +261,4 @@ class VoteController extends EmailsController {
403 ); 403 );
} }
} }
} }

View File

@ -4,7 +4,7 @@ namespace App\Repository;
use App\Entity\Choice; use App\Entity\Choice;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
/** /**
* @method Choice|null find( $id, $lockMode = null, $lockVersion = null ) * @method Choice|null find( $id, $lockMode = null, $lockVersion = null )
@ -17,32 +17,4 @@ class ChoiceRepository extends ServiceEntityRepository {
parent::__construct( $registry, Choice::class ); 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()
;
}
*/
} }

View File

@ -4,7 +4,7 @@ namespace App\Repository;
use App\Entity\Comment; use App\Entity\Comment;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
/** /**
* @method Comment|null find( $id, $lockMode = null, $lockVersion = null ) * @method Comment|null find( $id, $lockMode = null, $lockVersion = null )

View File

@ -4,7 +4,7 @@ namespace App\Repository;
use App\Entity\Owner; use App\Entity\Owner;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
/** /**
* @method Owner|null find( $id, $lockMode = null, $lockVersion = null ) * @method Owner|null find( $id, $lockMode = null, $lockVersion = null )

View File

@ -4,7 +4,7 @@ namespace App\Repository;
use App\Entity\StackOfVotes; use App\Entity\StackOfVotes;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
/** /**
* @method StackOfVotes|null find( $id, $lockMode = null, $lockVersion = null ) * @method StackOfVotes|null find( $id, $lockMode = null, $lockVersion = null )

View File

@ -4,7 +4,7 @@ namespace App\Repository;
use App\Entity\Vote; use App\Entity\Vote;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
/** /**
* @method Vote|null find( $id, $lockMode = null, $lockVersion = null ) * @method Vote|null find( $id, $lockMode = null, $lockVersion = null )