From fc40302aa42bb5ac4b4a17d0889c3717b4b450b6 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Thu, 28 Nov 2019 14:16:56 +0100 Subject: [PATCH] :zap: advance on choice create --- README.md | 6 +- examples.md | 20 +- src/Controller/DefaultController.php | 19 +- src/Entity/Choice.php | 111 +++---- src/Entity/Poll.php | 469 +++++++++++++-------------- 5 files changed, 324 insertions(+), 301 deletions(-) diff --git a/README.md b/README.md index c1699bf..d1ea236 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,11 @@ there are examples of request to make it all work. ```bash composer install ``` -### initiate the database -```bash +### initiate the database with fixtures +```bash +php bin/console doctrine:schema:drop --force php bin/console doctrine:schema:create +php bin/console doctrine:fixtures:load --no-interaction ``` ### launch local server with ```bash diff --git a/examples.md b/examples.md index 82336a4..fb130c2 100644 --- a/examples.md +++ b/examples.md @@ -1,9 +1,27 @@ # API example after having setup your project and database, and having a local server running you can rest these commands +## get Swagger configuration + +``` +GET +http://127.0.0.1:8000/api/doc.json +``` + +## create a poll +``` +POST +http://127.0.0.1:8000/api/v1/poll/new +Content-Type:"application/json" + + + +``` + +## add a vote to an existing poll ``` POST http://127.0.0.1:8000/api/v1/poll/98/vote -Content-Type:"pplication/json +Content-Type:"application/json" { "pseudo": "Mario Bros", diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 6bfb43a..4cf4ef0 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -2,6 +2,7 @@ namespace App\Controller; +use App\Entity\Choice; use App\Entity\Owner; use App\Entity\Poll; use App\Entity\StackOfVotes; @@ -18,7 +19,7 @@ use Symfony\Component\HttpFoundation\Request; /** * Class DefaultController * @package App\Controller - * @Route("/api/v1/",name="api_") + * @Route("/api/v1",name="api_") */ class DefaultController extends AbstractController { /** @@ -67,7 +68,7 @@ class DefaultController extends AbstractController { /** * @Post( * path = "/poll", - * name = "new_polls", + * name = "new_poll", * requirements = {"creator"="\w+"} * ) * @param Request $request @@ -91,6 +92,7 @@ class DefaultController extends AbstractController { $em = $this->getDoctrine()->getRepository( Owner::class ); $foundOwner = $em->findOneBy( [ 'email' => $data[ 'owner' ][ 'email' ] ] ); + $userWasFound = false; if ( ! $foundOwner ) { //create a new owner @@ -105,9 +107,22 @@ class DefaultController extends AbstractController { // link the owner and the poll $newpoll->setOwner( $foundOwner ); $foundOwner->addPoll( $newpoll ); + + $em = $this->getDoctrine()->getManager(); $em->persist( $newpoll ); $em->persist( $foundOwner ); + + // manage choices + $choices = $data[ 'choices' ]; + foreach ( $choices as $c ) { + var_dump( $c ); + $newChoice = new Choice(); + $newChoice->setPoll( $newpoll ); +// $newpoll->addChoice( $newChoice ); +// $em->persist( $newChoice ); + } + $em->persist( $newpoll ); $em->flush(); $precision = ''; if ( $userWasFound ) { diff --git a/src/Entity/Choice.php b/src/Entity/Choice.php index 40f305b..5b27201 100644 --- a/src/Entity/Choice.php +++ b/src/Entity/Choice.php @@ -2,7 +2,6 @@ namespace App\Entity; -use DateTimeInterface; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; @@ -44,79 +43,77 @@ class Choice { */ public $votes; - public function __construct() { - $this->poll = new ArrayCollection(); - $this->votes = new ArrayCollection(); - $this->setDateTime( new \DateTime() ); - } + public function __construct( $optionalName = null ) { + $this->poll = new ArrayCollection(); + $this->votes = new ArrayCollection(); + $this->setDateTime( new \DateTime() ); + if ( $optionalName ) { + $this->setName( $optionalName ); + } + } + public function setPoll( ?Poll $poll ): self { + $this->poll = $poll; + +// $poll->addChoice( $this ); + + return $this; + } + public function getId(): ?int { - return $this->id; - } + return $this->id; + } public function getName(): ?string { - return $this->name; - } + return $this->name; + } - public function setName( $name ): self { - if ( is_a( $name, 'DateTime' ) ) { - $this->setDateTime( $name ); - $name = $name->format( 'D Y-m-d' ); - } - $this->name = $name; - - return $this; - } + public function setName( ?string $name ): self { + $this->name = $name; - public function getDateTime(): ?DateTimeInterface { - return $this->dateTime; - } + return $this; + } - public function setDateTime( ?DateTimeInterface $dateTime ): self { - $this->dateTime = $dateTime; - - return $this; - } + public function getDateTime(): ?\DateTimeInterface { + return $this->dateTime; + } + public function setDateTime( ?\DateTimeInterface $dateTime ): self { + $this->dateTime = $dateTime; + + return $this; + } + + public function getPoll(): ?Poll { + return $this->poll; + } /** * @return Collection|Vote[] */ public function getVotes(): Collection { - return $this->votes; - } + return $this->votes; + } public function addVote( Vote $vote ): self { - if ( ! $this->votes->contains( $vote ) ) { - $this->votes[] = $vote; - $vote->setChoice( $this ); - } - - return $this; - } + if ( ! $this->votes->contains( $vote ) ) { + $this->votes[] = $vote; + $vote->setChoice( $this ); + } + + return $this; + } public function removeVote( Vote $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; - } + 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 ); + } + } - public function getPoll(): ?Poll - { - return $this->poll; - } - - public function setPoll(?Poll $poll): self - { - $this->poll = $poll; - - return $this; - } + return $this; + } } diff --git a/src/Entity/Poll.php b/src/Entity/Poll.php index 6f79e04..86e985c 100644 --- a/src/Entity/Poll.php +++ b/src/Entity/Poll.php @@ -103,22 +103,22 @@ class Poll { */ public $showResultEvenIfPasswords; /** - * @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="poll", orphanRemoval=true,cascade={"persist", "remove"}) + * @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="poll", orphanRemoval=true, cascade={"persist", "remove"}) * @Serializer\Type("App\Entity\Vote") */ public $votes; /** - * @ORM\OneToMany(targetEntity="App\Entity\StackOfVotes", mappedBy="poll", orphanRemoval=true,cascade={"persist", "remove"}) + * @ORM\OneToMany(targetEntity="App\Entity\StackOfVotes", mappedBy="poll", orphanRemoval=true, cascade={"persist", "remove"}) * @Serializer\Type("App\Entity\StackOfVotes") */ public $stacksOfVotes; /** - * @ORM\OneToMany(targetEntity="App\Entity\Choice", mappedBy="poll", orphanRemoval=true,cascade={"persist", "remove"}) + * @ORM\OneToMany(targetEntity="App\Entity\Choice", mappedBy="poll", orphanRemoval=true, cascade={"persist", "remove"}) * @Serializer\Type("App\Entity\Choice") */ public $choices; /** - * @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="poll", orphanRemoval=true,cascade={"persist", "remove"}) + * @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="poll", orphanRemoval=true, cascade={"persist", "remove"}) * @Serializer\Type("App\Entity\Comment") */ public $comments; @@ -142,349 +142,340 @@ class Poll { public function __construct() { - $this->adminKey = uniqid(); - $this->votes = new ArrayCollection(); - $this->choices = new ArrayCollection(); - $this->comments = new ArrayCollection(); - $this->stacksOfVotes = new ArrayCollection(); - $this->setCreationDate( new \DateTime() ); - $this->setExpiracyDate( $this->addDaysToDate( - new \DateTime(), - $this->defaultExpiracyDaysFromNow - ) ); - $this->setAllowedAnswers( [ 'yes' ] ); - } + $this->adminKey = uniqid(); + $this->votes = new ArrayCollection(); + $this->choices = new ArrayCollection(); + $this->comments = new ArrayCollection(); + $this->stacksOfVotes = new ArrayCollection(); + $this->setCreationDate( new \DateTime() ); + $this->setExpiracyDate( $this->addDaysToDate( + new \DateTime(), + $this->defaultExpiracyDaysFromNow + ) ); + $this->setAllowedAnswers( [ 'yes' ] ); + } public function findChoiceById( int $id ) { - - error_reporting( E_ALL ^ E_NOTICE ); - $choices = $this->getChoices(); - var_dump( count( $choices ) ); - foreach ( $choices as $choice ) { - if ( $choice && $choice->getId() == $id ) { - return $choice; - } - - return null; - } - // if ( $choices && $choices->containsKey( $id ) ) { - // $foundChoice = $choices->get( $id ); - // } else { - // $foundChoice = new Choice(); - // $foundChoice->setPoll( $this ); - // } - // - // return $foundChoice; - } + + error_reporting( E_ALL ^ E_NOTICE ); + $choices = $this->getChoices(); + var_dump( count( $choices ) ); + // there must be something cleaner than this in Doctrine ArrayCollection + foreach ( $choices as $choice ) { + if ( $choice && $choice->getId() == $id ) { + return $choice; + } + + return null; + } + } public function addDaysToDate( \DateTime $date, int $days ) { - $st = strtotime( $date->getTimestamp() . ' + ' . $days . ' days' ); - - return new \DateTime( $st ); - } + $st = strtotime( $date->getTimestamp() . ' + ' . $days . ' days' ); + + return new \DateTime( $st ); + } public function getId(): ?int { - return $this->id; - } + return $this->id; + } public function getTitle(): ?string { - return $this->title; - } + return $this->title; + } public function setTitle( string $title ): self { - $this->title = $title; - - return $this; - } + $this->title = $title; + + 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 setExpiracyDate( DateTimeInterface $expiracyDate ): self { - $this->expiracyDate = $expiracyDate; - - return $this; - } + $this->expiracyDate = $expiracyDate; + + return $this; + } public function getOwner(): ?Owner { - return $this->owner; - } + return $this->owner; + } public function setOwner( ?Owner $owner ): self { - $this->owner = $owner; - - return $this; - } + $this->owner = $owner; + + return $this; + } /** * @return Collection|Vote[] */ public function getVotes(): Collection { - return $this->votes; - } + return $this->votes; + } public function getAdminKey(): ?string { - return $this->adminKey; - } + return $this->adminKey; + } public function setAdminKey( string $adminKey ): self { - $this->adminKey = $adminKey; - - return $this; - } + $this->adminKey = $adminKey; + + return $this; + } public function getDescription(): ?string { - return $this->description; - } + return $this->description; + } public function setDescription( string $description ): self { - $this->description = $description; - - return $this; - } + $this->description = $description; + + return $this; + } public function getKind(): ?string { - return $this->kind; - } + return $this->kind; + } public function setKind( string $kind ): self { - $this->kind = $kind; - - return $this; - } + $this->kind = $kind; + + return $this; + } public function getCustomUrl(): ?string { - return $this->customUrl; - } + return $this->customUrl; + } public function setCustomUrl( string $customUrl ): self { - $this->customUrl = $customUrl; - - return $this; - } + $this->customUrl = $customUrl; + + return $this; + } public function getPassword(): ?string { - return $this->password; - } + return $this->password; + } public function setPassword( string $password ): self { - $this->password = $password; - - return $this; - } + $this->password = $password; + + return $this; + } public function getModificationPolicy(): ?string { - return $this->modificationPolicy; - } + return $this->modificationPolicy; + } public function setModificationPolicy( string $modificationPolicy ): self { - $this->modificationPolicy = $modificationPolicy; - - return $this; - } + $this->modificationPolicy = $modificationPolicy; + + return $this; + } public function getMailOnComment(): ?bool { - return $this->mailOnComment; - } + return $this->mailOnComment; + } public function setMailOnComment( bool $mailOnComment ): self { - $this->mailOnComment = $mailOnComment; - - return $this; - } + $this->mailOnComment = $mailOnComment; + + return $this; + } public function getMailOnVote(): ?bool { - return $this->mailOnVote; - } + return $this->mailOnVote; + } public function setMailOnVote( bool $mailOnVote ): self { - $this->mailOnVote = $mailOnVote; - - return $this; - } + $this->mailOnVote = $mailOnVote; + + return $this; + } public function getHideResults(): ?bool { - return $this->hideResults; - } + return $this->hideResults; + } public function setHideResults( bool $hideResults ): self { - $this->hideResults = $hideResults; - - return $this; - } + $this->hideResults = $hideResults; + + return $this; + } public function getShowResultEvenIfPasswords(): ?bool { - return $this->showResultEvenIfPasswords; - } + return $this->showResultEvenIfPasswords; + } public function setShowResultEvenIfPasswords( bool $showResultEvenIfPasswords ): self { - $this->showResultEvenIfPasswords = $showResultEvenIfPasswords; - - return $this; - } + $this->showResultEvenIfPasswords = $showResultEvenIfPasswords; + + return $this; + } /** * @return Collection|Comment[] */ public function getComments(): Collection { - return $this->comments; - } + return $this->comments; + } public function addComment( Comment $comment ): self { - if ( ! $this->comments->contains( $comment ) ) { - $this->comments[] = $comment; - $comment->setPoll( $this ); - } - - return $this; - } + 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; - } + 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; + } public function getStacksOfVotes() { - return $this->stacksOfVotes; - } + return $this->stacksOfVotes; + } public function setStacksOfVotes( ?StackOfVotes $stacksOfVotes ): self { - $this->stacksOfVotes = $stacksOfVotes; - - return $this; - } + $this->stacksOfVotes = $stacksOfVotes; + + return $this; + } public function addStackOfVote( StackOfVotes $stackOfVote ): self { - if ( ! $this->stacksOfVotes->contains( $stackOfVote ) ) { - $this->stacksOfVotes[] = $stackOfVote; - $stackOfVote->setPoll( $this ); - } - - return $this; - } + if ( ! $this->stacksOfVotes->contains( $stackOfVote ) ) { + $this->stacksOfVotes[] = $stackOfVote; + $stackOfVote->setPoll( $this ); + } + + return $this; + } public function removeStackOfVote( StackOfVotes $stackOfVote ): self { - if ( $this->stacksOfVotes->contains( $stackOfVote ) ) { - $this->stacksOfVotes->removeElement( $stackOfVote ); - // set the owning side to null (unless already changed) - if ( $stackOfVote->getPoll() === $this ) { - $stackOfVote->setPoll( null ); - } - } - - return $this; - } + if ( $this->stacksOfVotes->contains( $stackOfVote ) ) { + $this->stacksOfVotes->removeElement( $stackOfVote ); + // set the owning side to null (unless already changed) + if ( $stackOfVote->getPoll() === $this ) { + $stackOfVote->setPoll( null ); + } + } + + return $this; + } public function getExpiracyDate(): ?\DateTimeInterface { - return $this->expiracyDate; - } + return $this->expiracyDate; + } public function addVote( Vote $vote ): self { - if ( ! $this->votes->contains( $vote ) ) { - $this->votes[] = $vote; - $vote->setPoll( $this ); - } - - return $this; - } + if ( ! $this->votes->contains( $vote ) ) { + $this->votes[] = $vote; + $vote->setPoll( $this ); + } + + return $this; + } public function removeVote( Vote $vote ): self { - if ( $this->votes->contains( $vote ) ) { - $this->votes->removeElement( $vote ); - // set the owning side to null (unless already changed) - if ( $vote->getPoll() === $this ) { - $vote->setPoll( null ); - } - } - - return $this; - } + if ( $this->votes->contains( $vote ) ) { + $this->votes->removeElement( $vote ); + // set the owning side to null (unless already changed) + if ( $vote->getPoll() === $this ) { + $vote->setPoll( null ); + } + } + + return $this; + } /** * @return Collection|Choice[] */ public function getChoices(): Collection { - return $this->choices; - } + return $this->choices; + } public function addTextChoiceArray( Array $choiceTextArray ): self { - foreach ( $choiceTextArray as $text ) { - $newChoice = new Choice(); - $newChoice->setName( $text ); - $this->addChoice( $newChoice ); - } - - return $this; - } + foreach ( $choiceTextArray as $text ) { + $newChoice = new Choice(); + $newChoice->setName( $text ); + $this->addChoice( $newChoice ); + } + + return $this; + } public function addChoice( Choice $choice ): self { - if ( ! $this->choices->contains( $choice ) ) { - $this->choices[] = $choice; - $choice->setPoll( $this ); - } - - return $this; - } + 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; - } + 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; + } public function getAllowedAnswers(): ?array { - return $this->allowedAnswers; - } + return $this->allowedAnswers; + } public function setAllowedAnswers( array $allowedAnswers ): self { - $this->allowedAnswers = $allowedAnswers; - - return $this; - } + $this->allowedAnswers = $allowedAnswers; - public function addStacksOfVote(StackOfVotes $stacksOfVote): self - { - if (!$this->stacksOfVotes->contains($stacksOfVote)) { - $this->stacksOfVotes[] = $stacksOfVote; - $stacksOfVote->setPoll($this); - } + return $this; + } - return $this; - } + public function addStacksOfVote( StackOfVotes $stacksOfVote ): self { + if ( ! $this->stacksOfVotes->contains( $stacksOfVote ) ) { + $this->stacksOfVotes[] = $stacksOfVote; + $stacksOfVote->setPoll( $this ); + } - public function removeStacksOfVote(StackOfVotes $stacksOfVote): self - { - if ($this->stacksOfVotes->contains($stacksOfVote)) { - $this->stacksOfVotes->removeElement($stacksOfVote); - // set the owning side to null (unless already changed) - if ($stacksOfVote->getPoll() === $this) { - $stacksOfVote->setPoll(null); - } - } + return $this; + } - return $this; - } + public function removeStacksOfVote( StackOfVotes $stacksOfVote ): self { + if ( $this->stacksOfVotes->contains( $stacksOfVote ) ) { + $this->stacksOfVotes->removeElement( $stacksOfVote ); + // set the owning side to null (unless already changed) + if ( $stacksOfVote->getPoll() === $this ) { + $stacksOfVote->setPoll( null ); + } + } + + return $this; + } }