From 9123faf3f1bb0eab677aa7691ede0c2560beab1f Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Wed, 6 Nov 2019 14:35:07 +0100 Subject: [PATCH] :zap: add choices and votes links in DB --- src/Controller/DefaultController.php | 2 +- src/Entity/Choice.php | 131 +++++++++++++++++++++++++++ src/Entity/Owner.php | 4 +- src/Entity/Poll.php | 41 ++++++++- src/Entity/Vote.php | 61 ++++++++----- src/Repository/ChoiceRepository.php | 50 ++++++++++ 6 files changed, 264 insertions(+), 25 deletions(-) create mode 100644 src/Entity/Choice.php create mode 100644 src/Repository/ChoiceRepository.php diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index c4ad051..f03d819 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -126,7 +126,7 @@ class DefaultController extends AbstractController { return $this->json( [ 'message' => 'you created a poll ' . $precision, -// 'data' => $jsonContent, + 'data' => $newpoll, ], 203 ); diff --git a/src/Entity/Choice.php b/src/Entity/Choice.php new file mode 100644 index 0000000..d19747f --- /dev/null +++ b/src/Entity/Choice.php @@ -0,0 +1,131 @@ +poll = new ArrayCollection(); + $this->votes = new ArrayCollection(); + } + + + public function getId(): ?int { + return $this->id; + } + + public function getName(): ?string { + return $this->name; + } + + public function setName( string $name ): self { + $this->name = $name; + + return $this; + } + + public function getDateTime(): ?\DateTimeInterface { + return $this->dateTime; + } + + public function setDateTime( ?\DateTimeInterface $dateTime ): self { + $this->dateTime = $dateTime; + + return $this; + } + + /** + * @return Collection|Poll[] + */ + public function getPoll(): Collection + { + return $this->poll; + } + + public function addPoll(Poll $poll): self + { + if (!$this->poll->contains($poll)) { + $this->poll[] = $poll; + $poll->setChoices($this); + } + + return $this; + } + + public function removePoll(Poll $poll): self + { + if ($this->poll->contains($poll)) { + $this->poll->removeElement($poll); + // set the owning side to null (unless already changed) + if ($poll->getChoices() === $this) { + $poll->setChoices(null); + } + } + + return $this; + } + + /** + * @return Collection|Choice[] + */ + public function getVotes(): Collection + { + return $this->votes; + } + + public function addVote(Choice $vote): self + { + if (!$this->votes->contains($vote)) { + $this->votes[] = $vote; + $vote->setChoice($this); + } + + return $this; + } + + public function removeVote(Choice $vote): self + { + if ($this->votes->contains($vote)) { + $this->votes->removeElement($vote); + // set the owning side to null (unless already changed) + if ($vote->getChoice() === $this) { + $vote->setChoice(null); + } + } + + return $this; + } +} diff --git a/src/Entity/Owner.php b/src/Entity/Owner.php index f669c44..6418f69 100644 --- a/src/Entity/Owner.php +++ b/src/Entity/Owner.php @@ -21,14 +21,16 @@ class Owner { /** * @ORM\Column(type="string", length=255) * @Serializer\Type("string") + * @Serializer\Expose() */ private $email; /** * @ORM\Column(type="string", length=255) * @Serializer\Type("string") + * @Serializer\Expose() */ - private $pseudo; + public $pseudo; /** * @ORM\OneToMany(targetEntity="App\Entity\Poll", mappedBy="owner",cascade={"persist"}) diff --git a/src/Entity/Poll.php b/src/Entity/Poll.php index 7e281d4..028ca98 100644 --- a/src/Entity/Poll.php +++ b/src/Entity/Poll.php @@ -83,26 +83,31 @@ class Poll { * self - one can only modify its own vote * nobody - no one can modify the votes (excepted admin), pray to have it right at first * @ORM\Column(type="string", length=255) + * @Serializer\Type("string") */ public $modificationPolicy; /** * send a mail on a new comment * @ORM\Column(type="boolean", nullable=true) + * @Serializer\Type("boolean") */ private $mailOnComment; /** * send a mail on a new vote * @ORM\Column(type="boolean", nullable=true) + * @Serializer\Type("boolean") */ private $mailOnVote; /** * hide publicly results * @ORM\Column(type="boolean", nullable=true) + * @Serializer\Type("boolean") */ private $hideResults; /** * show publicly results even if there is a password to access the vote * @ORM\Column(type="boolean", nullable=true) + * @Serializer\Type("boolean") */ private $showResultEvenIfPasswords; @@ -111,9 +116,15 @@ class Poll { * @Serializer\Type("App\Entity\Vote") */ private $votes; + /** + * @ORM\OneToMany(targetEntity="App\Entity\Choice", mappedBy="poll", orphanRemoval=true,cascade={"persist"}) + * @Serializer\Type("App\Entity\Choice") + */ + private $choices; public function __construct() { - $this->votes = new ArrayCollection(); + $this->votes = new ArrayCollection(); + $this->choices = new ArrayCollection(); } public function getId(): ?int { @@ -287,4 +298,32 @@ class Poll { return $this; } + + /** + * @return Collection|Choice[] + */ + public function getChoices(): Collection { + return $this->choices; + } + + public function addChoice( Choice $choice ): self { + if ( ! $this->choices->contains( $choice ) ) { + $this->choices[] = $choice; + $choice->setPoll( $this ); + } + + return $this; + } + + public function removeChoice( Choice $choice ): self { + if ( $this->choices->contains( $choice ) ) { + $this->choices->removeElement( $choice ); + // set the owning side to null (unless already changed) + if ( $choice->getPoll() === $this ) { + $choice->setPoll( null ); + } + } + + return $this; + } } diff --git a/src/Entity/Vote.php b/src/Entity/Vote.php index 83bfe00..5f7a72f 100644 --- a/src/Entity/Vote.php +++ b/src/Entity/Vote.php @@ -39,50 +39,55 @@ class Vote { * @ORM\JoinColumn(nullable=false) */ private $poll; + /** + * @ORM\ManyToOne(targetEntity="App\Entity\Choice", inversedBy="votes") + * @ORM\JoinColumn(nullable=false) + */ + private $choice; public function getId(): ?int { - return $this->id; - } + return $this->id; + } public function getAnswerTxt(): ?string { - return $this->answerTxt; - } + return $this->answerTxt; + } public function setAnswerTxt( ?string $answerTxt ): self { - $this->answerTxt = $answerTxt; + $this->answerTxt = $answerTxt; - return $this; - } + return $this; + } public function getAnswerDate(): ?\DateTimeInterface { - return $this->answerDate; - } + return $this->answerDate; + } public function setAnswerDate( ?\DateTimeInterface $answerDate ): self { - $this->answerDate = $answerDate; + $this->answerDate = $answerDate; - return $this; - } + return $this; + } public function getCreationDate(): ?\DateTimeInterface { - return $this->creationDate; - } + return $this->creationDate; + } public function setCreationDate( \DateTimeInterface $creationDate ): self { - $this->creationDate = $creationDate; + $this->creationDate = $creationDate; - return $this; - } + return $this; + } public function getPoll(): ?Poll { - return $this->poll; - } + return $this->poll; + } public function setPoll( ?Poll $poll ): self { - $this->poll = $poll; + $this->poll = $poll; - return $this; - } + return $this; + } public function getPseudo(): ?string { @@ -95,4 +100,16 @@ class Vote { return $this; } + + public function getChoice(): ?Choice + { + return $this->choice; + } + + public function setChoice(?Choice $choice): self + { + $this->choice = $choice; + + return $this; + } } diff --git a/src/Repository/ChoiceRepository.php b/src/Repository/ChoiceRepository.php new file mode 100644 index 0000000..0172fe7 --- /dev/null +++ b/src/Repository/ChoiceRepository.php @@ -0,0 +1,50 @@ +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() + ; + } + */ +}