🐛 fix vote stack adding

This commit is contained in:
Baptiste Lemoine 2019-11-28 17:00:17 +01:00
parent fceb2be35d
commit 087fbafd6a
2 changed files with 16 additions and 12 deletions

View File

@ -329,10 +329,16 @@ class DefaultController extends AbstractController {
$stack
->setPseudo( $data[ 'pseudo' ] )
->setPoll( $poll );
foreach ( $data[ 'votes' ] as $voteInfo ) {
$vote = new Vote();
$foundChoice = $poll->findChoiceById( $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' ] );
@ -350,20 +356,13 @@ class DefaultController extends AbstractController {
$em->flush();
return $this->json( [
'message' => 'you created a vote',
'message' => 'you created a vote stack',
'vote_stack' => $stack,
'json_you_sent' => $data,
],
201 );
}
function newVoteAction( Poll $poll ) {
return $this->json( [
'message' => 'you voted on the poll',
],
201 );
}
/**
* @Delete(

View File

@ -143,6 +143,7 @@ class Poll {
* @var int
*/
public $defaultExpiracyDaysFromNow = 60;
private $maxChoicesLimit = 25;
public function __construct() {
$this->votes = new ArrayCollection();
@ -203,17 +204,21 @@ class Poll {
public function findChoiceById( int $id ) {
error_reporting( E_ALL ^ E_NOTICE );
$choices = $this->getChoices();
var_dump( count( $choices ) );
$counter = 0;
// there must be something cleaner than this in Doctrine ArrayCollection
foreach ( $choices as $choice ) {
$counter ++;
if ( $counter > $this->maxChoicesLimit ) {
throw new \ErrorException( "max number of choices reached for this poll" );
}
if ( $choice && $choice->getId() == $id ) {
return $choice;
}
return null;
}
return null;
}
public function addDaysToDate( \DateTime $date, int $days ) {