From 7e79048b59069cce44a1bbd39131cc878067cf0f Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Wed, 6 Nov 2019 14:54:04 +0100 Subject: [PATCH] :zap: expose config for poll --- src/Controller/DefaultController.php | 28 +++--- src/Entity/Choice.php | 140 +++++++++++++-------------- src/Entity/Comment.php | 81 ++++++++++++++++ src/Entity/Owner.php | 36 ++++++- src/Entity/Poll.php | 61 +++++++++--- src/Entity/Vote.php | 100 ++++++++++--------- src/Repository/CommentRepository.php | 50 ++++++++++ 7 files changed, 349 insertions(+), 147 deletions(-) create mode 100644 src/Entity/Comment.php create mode 100644 src/Repository/CommentRepository.php diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index f03d819..297a79d 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -42,19 +42,6 @@ class DefaultController extends AbstractController { ] ); } - /** - * @Get( - * path = "/poll/{id}/comments", - * name = "get_poll_comment", - * requirements = {"id"="\d+"} - * ) - */ - public function getPollCommentsAction() { - return $this->json( [ - 'message' => 'here are your comments of the poll', - ] ); - } - /** * @Get( * path = "/poll/all", @@ -109,7 +96,6 @@ class DefaultController extends AbstractController { $owner->setEmail( $data[ 'owner' ][ 'email' ] ); $foundOwner = $owner; } else { -// die( $foundOwner->getPseudo() ); $userWasFound = true; } // link the owner and the poll @@ -133,6 +119,20 @@ class DefaultController extends AbstractController { } + /** + * @Get( + * path = "/poll/{id}/comments", + * name = "get_poll_comment", + * requirements = {"id"="\d+"} + * ) + */ + public function getPollCommentsAction( Poll $poll ) { + return $this->json( [ + 'message' => 'here are your comments of the poll', + 'data' => $poll->getComments(), + ] ); + } + /** * @Get( * path = "/poll/{id}", diff --git a/src/Entity/Choice.php b/src/Entity/Choice.php index d19747f..4641d08 100644 --- a/src/Entity/Choice.php +++ b/src/Entity/Choice.php @@ -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; /** * @ORM\Entity(repositoryClass="App\Repository\ChoiceRepository") @@ -19,113 +20,110 @@ class Choice { /** * @ORM\Column(type="string", length=255, nullable=true) + * @Serializer\Type("string") */ private $name; /** * @ORM\Column(type="datetime", nullable=true) + * @Serializer\Type("datetime") */ private $dateTime; /** * @ORM\OneToMany(targetEntity="App\Entity\Poll", mappedBy="choices") + * @Serializer\Type("App\Entity\Poll") */ private $poll; /** * @ORM\OneToMany(targetEntity="App\Entity\Choice", mappedBy="choice") + * @Serializer\Type("App\Entity\Choice") */ private $votes; - public function __construct() - { - $this->poll = new ArrayCollection(); - $this->votes = new ArrayCollection(); - } + public function __construct() { + $this->poll = new ArrayCollection(); + $this->votes = new ArrayCollection(); + } public function getId(): ?int { - return $this->id; - } + return $this->id; + } public function getName(): ?string { - return $this->name; - } + return $this->name; + } public function setName( string $name ): self { - $this->name = $name; - - return $this; - } + $this->name = $name; + + return $this; + } public function getDateTime(): ?\DateTimeInterface { - return $this->dateTime; - } + return $this->dateTime; + } public function setDateTime( ?\DateTimeInterface $dateTime ): self { - $this->dateTime = $dateTime; - - return $this; - } + $this->dateTime = $dateTime; - /** - * @return Collection|Poll[] - */ - public function getPoll(): Collection - { - return $this->poll; - } + return $this; + } - public function addPoll(Poll $poll): self - { - if (!$this->poll->contains($poll)) { - $this->poll[] = $poll; - $poll->setChoices($this); - } + /** + * @return Collection|Poll[] + */ + public function getPoll(): Collection { + return $this->poll; + } - return $this; - } + public function addPoll( Poll $poll ): self { + if ( ! $this->poll->contains( $poll ) ) { + $this->poll[] = $poll; + $poll->setChoices( $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 $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 Collection|Choice[] - */ - public function getVotes(): Collection - { - return $this->votes; - } + return $this; + } - public function addVote(Choice $vote): self - { - if (!$this->votes->contains($vote)) { - $this->votes[] = $vote; - $vote->setChoice($this); - } + /** + * @return Collection|Choice[] + */ + public function getVotes(): Collection { + return $this->votes; + } - return $this; - } + public function addVote( Choice $vote ): self { + if ( ! $this->votes->contains( $vote ) ) { + $this->votes[] = $vote; + $vote->setChoice( $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; + } - 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/Comment.php b/src/Entity/Comment.php new file mode 100644 index 0000000..56e3076 --- /dev/null +++ b/src/Entity/Comment.php @@ -0,0 +1,81 @@ +id; + } + + public function getOwner(): ?Owner { + return $this->owner; + } + + public function setOwner( ?Owner $owner ): self { + $this->owner = $owner; + + return $this; + } + + public function getText(): ?string { + return $this->text; + } + + public function setText( string $text ): self { + $this->text = $text; + + return $this; + } + + public function getCreatedAt(): ?\DateTimeInterface { + return $this->createdAt; + } + + public function setCreatedAt( \DateTimeInterface $createdAt ): self { + $this->createdAt = $createdAt; + + return $this; + } + + public function getPoll(): ?Poll { + return $this->poll; + } + + public function setPoll( ?Poll $poll ): self { + $this->poll = $poll; + + return $this; + } +} diff --git a/src/Entity/Owner.php b/src/Entity/Owner.php index 6418f69..5f9ae83 100644 --- a/src/Entity/Owner.php +++ b/src/Entity/Owner.php @@ -38,8 +38,14 @@ class Owner { */ private $polls; + /** + * @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="owner") + */ + private $comments; + public function __construct() { - $this->polls = new ArrayCollection(); + $this->polls = new ArrayCollection(); + $this->comments = new ArrayCollection(); } public function getId(): ?int { @@ -93,4 +99,32 @@ class Owner { return $this; } + + /** + * @return Collection|Comment[] + */ + public function getComments(): Collection { + return $this->comments; + } + + public function addText( Comment $text ): self { + if ( ! $this->comments->contains( $text ) ) { + $this->comments[] = $text; + $text->setOwner( $this ); + } + + return $this; + } + + public function removeText( Comment $text ): self { + if ( $this->comments->contains( $text ) ) { + $this->comments->removeElement( $text ); + // set the owning side to null (unless already changed) + if ( $text->getOwner() === $this ) { + $text->setOwner( null ); + } + } + + return $this; + } } diff --git a/src/Entity/Poll.php b/src/Entity/Poll.php index 028ca98..9e3f7aa 100644 --- a/src/Entity/Poll.php +++ b/src/Entity/Poll.php @@ -31,7 +31,7 @@ class Poll { * @Serializer\Expose() * @Serializer\Type("string") */ - private $customUrl; + public $customUrl; /** * vote restricted by a password in md5 format @@ -44,25 +44,25 @@ class Poll { * @Serializer\Expose() * @Serializer\Type("string") */ - private $description; + public $description; /** * @ORM\Column(type="datetime") * @Serializer\Expose() */ - private $creationDate; + public $creationDate; /** * @ORM\Column(type="datetime") */ - private $expiracyDate; + public $expiracyDate; /** * @ORM\ManyToOne(targetEntity="App\Entity\Owner", inversedBy="polls",cascade={"persist"}) * @ORM\JoinColumn(nullable=false) * @Serializer\Type("App\Entity\Owner") */ - private $owner; + public $owner; /** * used to allow administration @@ -91,40 +91,47 @@ class Poll { * @ORM\Column(type="boolean", nullable=true) * @Serializer\Type("boolean") */ - private $mailOnComment; + public $mailOnComment; /** * send a mail on a new vote * @ORM\Column(type="boolean", nullable=true) * @Serializer\Type("boolean") */ - private $mailOnVote; + public $mailOnVote; /** * hide publicly results * @ORM\Column(type="boolean", nullable=true) * @Serializer\Type("boolean") */ - private $hideResults; + public $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; + public $showResultEvenIfPasswords; /** * @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="poll", orphanRemoval=true,cascade={"persist"}) * @Serializer\Type("App\Entity\Vote") */ - private $votes; + public $votes; /** * @ORM\OneToMany(targetEntity="App\Entity\Choice", mappedBy="poll", orphanRemoval=true,cascade={"persist"}) * @Serializer\Type("App\Entity\Choice") */ - private $choices; + public $choices; + + /** + * @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="poll") + * @Serializer\Type("App\Entity\Comment") + */ + public $comments; public function __construct() { - $this->votes = new ArrayCollection(); - $this->choices = new ArrayCollection(); + $this->votes = new ArrayCollection(); + $this->choices = new ArrayCollection(); + $this->comments = new ArrayCollection(); } public function getId(): ?int { @@ -326,4 +333,32 @@ class Poll { return $this; } + + /** + * @return Collection|Comment[] + */ + public function getComments(): Collection { + return $this->comments; + } + + public function addComment( Comment $comment ): self { + if ( ! $this->comments->contains( $comment ) ) { + $this->comments[] = $comment; + $comment->setPoll( $this ); + } + + return $this; + } + + public function removeComment( Comment $comment ): self { + if ( $this->comments->contains( $comment ) ) { + $this->comments->removeElement( $comment ); + // set the owning side to null (unless already changed) + if ( $comment->getPoll() === $this ) { + $comment->setPoll( null ); + } + } + + return $this; + } } diff --git a/src/Entity/Vote.php b/src/Entity/Vote.php index 5f7a72f..8f0eb1d 100644 --- a/src/Entity/Vote.php +++ b/src/Entity/Vote.php @@ -3,6 +3,7 @@ namespace App\Entity; use Doctrine\ORM\Mapping as ORM; +use JMS\Serializer\Annotation as Serializer; /** * @ORM\Entity(repositoryClass="App\Repository\VoteRepository") @@ -12,104 +13,107 @@ class Vote { * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") + * @Serializer\Type("integer") */ private $id; /** * @ORM\Column(type="string", length=255, nullable=true) + * @Serializer\Type("string") */ - private $pseudo; + public $pseudo; /** * @ORM\Column(type="string", length=255, nullable=true) + * @Serializer\Type("string") */ - private $answerTxt; + public $answerTxt; /** * @ORM\Column(type="datetime", nullable=true) + * @Serializer\Type("datetime") */ private $answerDate; /** * @ORM\Column(type="datetime") + * @Serializer\Type("datetime") */ - private $creationDate; + public $creationDate; /** * @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="votes") * @ORM\JoinColumn(nullable=false) + * @Serializer\Type("App\Entity\Poll") */ private $poll; /** * @ORM\ManyToOne(targetEntity="App\Entity\Choice", inversedBy="votes") * @ORM\JoinColumn(nullable=false) + * @Serializer\Type("App\Entity\choice") */ - private $choice; + public $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; - - return $this; - } + $this->answerTxt = $answerTxt; + + return $this; + } public function getAnswerDate(): ?\DateTimeInterface { - return $this->answerDate; - } + return $this->answerDate; + } public function setAnswerDate( ?\DateTimeInterface $answerDate ): self { - $this->answerDate = $answerDate; - - return $this; - } + $this->answerDate = $answerDate; + + return $this; + } public function getCreationDate(): ?\DateTimeInterface { - return $this->creationDate; - } + return $this->creationDate; + } public function setCreationDate( \DateTimeInterface $creationDate ): self { - $this->creationDate = $creationDate; - - return $this; - } + $this->creationDate = $creationDate; + + return $this; + } public function getPoll(): ?Poll { - return $this->poll; - } + return $this->poll; + } public function setPoll( ?Poll $poll ): self { - $this->poll = $poll; - - return $this; - } + $this->poll = $poll; - public function getPseudo(): ?string - { - return $this->pseudo; - } + return $this; + } - public function setPseudo(?string $pseudo): self - { - $this->pseudo = $pseudo; + public function getPseudo(): ?string { + return $this->pseudo; + } - return $this; - } + public function setPseudo( ?string $pseudo ): self { + $this->pseudo = $pseudo; - public function getChoice(): ?Choice - { - return $this->choice; - } + return $this; + } - public function setChoice(?Choice $choice): self - { - $this->choice = $choice; + public function getChoice(): ?Choice { + return $this->choice; + } - return $this; - } + public function setChoice( ?Choice $choice ): self { + $this->choice = $choice; + + return $this; + } } diff --git a/src/Repository/CommentRepository.php b/src/Repository/CommentRepository.php new file mode 100644 index 0000000..cddf55d --- /dev/null +++ b/src/Repository/CommentRepository.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): ?Comment + { + return $this->createQueryBuilder('c') + ->andWhere('c.exampleField = :val') + ->setParameter('val', $value) + ->getQuery() + ->getOneOrNullResult() + ; + } + */ +}