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

vote stack with pseudo

This commit is contained in:
Baptiste Lemoine 2019-11-27 17:07:11 +01:00
parent d6cbd23d3f
commit 5d74fb9780
3 changed files with 63 additions and 31 deletions

View File

@ -6,6 +6,7 @@ use App\Entity\Choice;
use App\Entity\Comment;
use App\Entity\Owner;
use App\Entity\Poll;
use App\Entity\StackOfVotes;
use App\Entity\Vote;
use DateTime;
use Doctrine\Bundle\FixturesBundle\Fixture;
@ -43,15 +44,50 @@ class AppPollFixtures extends Fixture {
$poll
->addChoice( $choiceA )
->addChoice( $choiceB );
$voteA = new Vote();
$voteA->setPseudo( 'chuck norris' )
->setChoice( $choiceA );
$voteA = new Vote();
$voteA->setPseudo( 'Néo' )
->setChoice( $choiceB );
$manager->persist( $poll );
$manager->persist( $owner );
$manager->persist( $voter );
$manager->flush();
$stack1 = new StackOfVotes();
$stack1
->setPoll( $poll )
->setOwner( $voter );
$voteA = new Vote();
$voteA
->setPoll( $poll )
->setStackOfVotes( $stack1 )
->setValue( [ "yes" ] )
->setChoice( $choiceA );
$voteB = new Vote();
$voteB
->setPoll( $poll )
->setStackOfVotes( $stack1 )
->setValue( [ "yes" ] )
->setChoice( $choiceB );
$stack1->setPseudo( 'chuck norris' );
$manager->persist( $stack1 );
// voter guy votes again with an other pseudo
$stack2 = new StackOfVotes();
$stack2->setPseudo( 'Jean indécis' );
$stack2
->setPoll( $poll )
->setOwner( $voter );
$voteA = new Vote();
$voteA
->setPoll( $poll )
->setStackOfVotes( $stack2 )
->setValue( [ "maybe" ] )
->setChoice( $choiceA );
$manager->persist( $stack2 );
$poll = new Poll();

View File

@ -5,6 +5,7 @@ namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
/**
* contains the votes for one answer to a poll
@ -17,7 +18,12 @@ class StackOfVotes {
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Serializer\Type("string")
* @Serializer\Expose()
*/
public $pseudo;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="stacksOfVotes", cascade={"persist","remove"})
*/
@ -28,7 +34,7 @@ class StackOfVotes {
private $poll;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Owner", inversedBy="stackOfVotes")
* @ORM\ManyToOne(targetEntity="App\Entity\Owner", inversedBy="stackOfVotes", cascade={"persist"})
*/
private $owner;
@ -49,6 +55,8 @@ class StackOfVotes {
public function addVote( Vote $vote ): self {
if ( ! $this->votes->contains( $vote ) ) {
$vote->setPoll( $this->getPoll() );
$this->votes[] = $vote;
$vote->setStackOfVotes( $this );
}

View File

@ -10,20 +10,15 @@ use JMS\Serializer\Annotation as Serializer;
* @ORM\Entity(repositoryClass="App\Repository\VoteRepository")
*/
class Vote {
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Serializer\Type("string")
*/
public $pseudo;
/**
* for a text kind of choice: could be "yes" "no" "maybe" and emptu.
* for a date kind, the choice linked is equivalent to the value selected
* @ORM\Column(type="string", length=255, nullable=true)
* @Serializer\Type("string")
* @ORM\Column(type="array", length=255, nullable=true)
* @Serializer\Type("array")
*/
public $value;
public $value = [];
/**
* @ORM\Column(type="datetime")
* @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"})
* @Serializer\Type("datetime")
*/
public $creationDate;
@ -47,7 +42,7 @@ class Vote {
*/
private $poll;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\StackOfVotes", inversedBy="votes")
* @ORM\ManyToOne(targetEntity="App\Entity\StackOfVotes", inversedBy="votes", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
* @Serializer\Type("App\Entity\StackOfVotes")
*/
@ -67,16 +62,9 @@ class Vote {
public function setPoll( ?Poll $poll ): self {
$this->poll = $poll;
return $this;
}
public function getPseudo(): ?string {
return $this->pseudo;
}
public function setPseudo( ?string $pseudo ): self {
$this->pseudo = $pseudo;
if ( $poll ) {
$poll->addVote( $this );
}
return $this;
}
@ -95,7 +83,7 @@ class Vote {
return $this->value;
}
public function setValue( ?string $value ): self {
public function setValue( ?array $value ): self {
$this->value = $value;
return $this;