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

limiter l'ajout de vote stack si la limite par poll a été atteinte

#30
This commit is contained in:
Tykayn 2021-05-18 23:46:05 +02:00 committed by tykayn
parent 5716642c09
commit f2edaa49f0

View File

@ -44,17 +44,25 @@ class VoteController extends EmailsController {
string $custom_url, string $custom_url,
Request $request, Request $request,
ChoiceRepository $choice_repository ChoiceRepository $choice_repository
) { ) {
/***
* checks before persisting
*/
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$emPol = $em->getRepository( Poll::class ); $emPol = $em->getRepository( Poll::class );
$poll = $emPol->findOneByCustomUrl( $custom_url ); $poll = $emPol->findOneByCustomUrl( $custom_url );
// check : existence of poll
if ( ! $poll ) { if ( ! $poll ) {
return $this->json( [ 'message' => 'poll "' . $custom_url . '" not found' ], 404 ); return $this->json( [ 'message' => 'poll "' . $custom_url . '" not found' ], 404 );
} }
// check : limit of number of participation max
if ( count( $poll->getStacksOfVotes() ) == $poll->getVotesMax() ) {
return $this->json( [ 'message' => 'poll "' . $custom_url . '" not allowed to have more stack of votes than ' . $poll->getVotesMax() ],
403 );
}
$data = $request->getContent(); $data = $request->getContent();
$data = json_decode( $data, true ); $data = json_decode( $data, true );
@ -63,32 +71,30 @@ class VoteController extends EmailsController {
$owner = new Owner(); $owner = new Owner();
$owner $owner
->addPoll( $poll ) ->addPoll( $poll );
;
$newStack = new StackOfVotes(); $newStack = new StackOfVotes();
$newStack $newStack
->setPoll($poll) ->setPoll( $poll )
->setIp( $_SERVER[ 'REMOTE_ADDR' ] ) ->setIp( $_SERVER[ 'REMOTE_ADDR' ] )
->setPseudo( $data[ "pseudo" ] ) ->setPseudo( $data[ "pseudo" ] )
->setOwner( $owner ); ->setOwner( $owner );
$owner $owner
->setPseudo($data['owner'][ "pseudo" ]) ->setPseudo( $data[ 'owner' ][ "pseudo" ] )
->setPseudo($data['owner'][ "email" ]) ->setPseudo( $data[ 'owner' ][ "email" ] )
->addStackOfVote($newStack); ->addStackOfVote( $newStack );
// TODO manage new comment // TODO manage new comment
$emChoice = $choice_repository; $emChoice = $choice_repository;
$newComment = new Comment(); $newComment = new Comment();
$newComment->setPseudo($data ['pseudo']) $newComment->setPseudo( $data [ 'pseudo' ] )
->setPoll($poll) ->setPoll( $poll )
->setText($data['comment']); ->setText( $data[ 'comment' ] );
$owner->addComment($newComment); $owner->addComment( $newComment );
$em->persist($newComment); $em->persist( $newComment );
foreach ( $data[ 'votes' ] as $vote ) { foreach ( $data[ 'votes' ] as $vote ) {
if(!$vote[ 'value' ]){ if ( ! $vote[ 'value' ] ) {
continue; continue;
} }
$newVote = new Vote(); $newVote = new Vote();