advance on choice create

This commit is contained in:
Baptiste Lemoine 2019-11-28 14:16:56 +01:00
parent 65c32d07ed
commit fc40302aa4
5 changed files with 324 additions and 301 deletions

View File

@ -15,9 +15,11 @@ there are examples of request to make it all work.
```bash ```bash
composer install composer install
``` ```
### initiate the database ### initiate the database with fixtures
```bash ```bash
php bin/console doctrine:schema:drop --force
php bin/console doctrine:schema:create php bin/console doctrine:schema:create
php bin/console doctrine:fixtures:load --no-interaction
``` ```
### launch local server with ### launch local server with
```bash ```bash

View File

@ -1,9 +1,27 @@
# API example # API example
after having setup your project and database, and having a local server running you can rest these commands 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 POST
http://127.0.0.1:8000/api/v1/poll/98/vote http://127.0.0.1:8000/api/v1/poll/98/vote
Content-Type:"pplication/json Content-Type:"application/json"
{ {
"pseudo": "Mario Bros", "pseudo": "Mario Bros",

View File

@ -2,6 +2,7 @@
namespace App\Controller; namespace App\Controller;
use App\Entity\Choice;
use App\Entity\Owner; use App\Entity\Owner;
use App\Entity\Poll; use App\Entity\Poll;
use App\Entity\StackOfVotes; use App\Entity\StackOfVotes;
@ -18,7 +19,7 @@ use Symfony\Component\HttpFoundation\Request;
/** /**
* Class DefaultController * Class DefaultController
* @package App\Controller * @package App\Controller
* @Route("/api/v1/",name="api_") * @Route("/api/v1",name="api_")
*/ */
class DefaultController extends AbstractController { class DefaultController extends AbstractController {
/** /**
@ -67,7 +68,7 @@ class DefaultController extends AbstractController {
/** /**
* @Post( * @Post(
* path = "/poll", * path = "/poll",
* name = "new_polls", * name = "new_poll",
* requirements = {"creator"="\w+"} * requirements = {"creator"="\w+"}
* ) * )
* @param Request $request * @param Request $request
@ -91,6 +92,7 @@ class DefaultController extends AbstractController {
$em = $this->getDoctrine()->getRepository( Owner::class ); $em = $this->getDoctrine()->getRepository( Owner::class );
$foundOwner = $em->findOneBy( [ 'email' => $data[ 'owner' ][ 'email' ] ] ); $foundOwner = $em->findOneBy( [ 'email' => $data[ 'owner' ][ 'email' ] ] );
$userWasFound = false; $userWasFound = false;
if ( ! $foundOwner ) { if ( ! $foundOwner ) {
//create a new owner //create a new owner
@ -105,9 +107,22 @@ class DefaultController extends AbstractController {
// link the owner and the poll // link the owner and the poll
$newpoll->setOwner( $foundOwner ); $newpoll->setOwner( $foundOwner );
$foundOwner->addPoll( $newpoll ); $foundOwner->addPoll( $newpoll );
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$em->persist( $newpoll ); $em->persist( $newpoll );
$em->persist( $foundOwner ); $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(); $em->flush();
$precision = ''; $precision = '';
if ( $userWasFound ) { if ( $userWasFound ) {

View File

@ -2,7 +2,6 @@
namespace App\Entity; namespace App\Entity;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
@ -44,79 +43,77 @@ class Choice {
*/ */
public $votes; public $votes;
public function __construct() { public function __construct( $optionalName = null ) {
$this->poll = new ArrayCollection(); $this->poll = new ArrayCollection();
$this->votes = new ArrayCollection(); $this->votes = new ArrayCollection();
$this->setDateTime( new \DateTime() ); $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 { public function getId(): ?int {
return $this->id; return $this->id;
} }
public function getName(): ?string { public function getName(): ?string {
return $this->name; return $this->name;
} }
public function setName( $name ): self { public function setName( ?string $name ): self {
if ( is_a( $name, 'DateTime' ) ) { $this->name = $name;
$this->setDateTime( $name );
$name = $name->format( 'D Y-m-d' );
}
$this->name = $name;
return $this;
}
public function getDateTime(): ?DateTimeInterface { return $this;
return $this->dateTime; }
}
public function setDateTime( ?DateTimeInterface $dateTime ): self { public function getDateTime(): ?\DateTimeInterface {
$this->dateTime = $dateTime; return $this->dateTime;
}
return $this;
}
public function setDateTime( ?\DateTimeInterface $dateTime ): self {
$this->dateTime = $dateTime;
return $this;
}
public function getPoll(): ?Poll {
return $this->poll;
}
/** /**
* @return Collection|Vote[] * @return Collection|Vote[]
*/ */
public function getVotes(): Collection { public function getVotes(): Collection {
return $this->votes; return $this->votes;
} }
public function addVote( Vote $vote ): self { public function addVote( Vote $vote ): self {
if ( ! $this->votes->contains( $vote ) ) { if ( ! $this->votes->contains( $vote ) ) {
$this->votes[] = $vote; $this->votes[] = $vote;
$vote->setChoice( $this ); $vote->setChoice( $this );
} }
return $this; return $this;
} }
public function removeVote( Vote $vote ): self { public function removeVote( Vote $vote ): self {
if ( $this->votes->contains( $vote ) ) { if ( $this->votes->contains( $vote ) ) {
$this->votes->removeElement( $vote ); $this->votes->removeElement( $vote );
// set the owning side to null (unless already changed) // set the owning side to null (unless already changed)
if ( $vote->getChoice() === $this ) { if ( $vote->getChoice() === $this ) {
$vote->setChoice( null ); $vote->setChoice( null );
} }
} }
return $this;
}
public function getPoll(): ?Poll return $this;
{ }
return $this->poll;
}
public function setPoll(?Poll $poll): self
{
$this->poll = $poll;
return $this;
}
} }

View File

@ -103,22 +103,22 @@ class Poll {
*/ */
public $showResultEvenIfPasswords; 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") * @Serializer\Type("App\Entity\Vote")
*/ */
public $votes; 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") * @Serializer\Type("App\Entity\StackOfVotes")
*/ */
public $stacksOfVotes; 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") * @Serializer\Type("App\Entity\Choice")
*/ */
public $choices; 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") * @Serializer\Type("App\Entity\Comment")
*/ */
public $comments; public $comments;
@ -142,349 +142,340 @@ class Poll {
public function __construct() { public function __construct() {
$this->adminKey = uniqid(); $this->adminKey = uniqid();
$this->votes = new ArrayCollection(); $this->votes = new ArrayCollection();
$this->choices = new ArrayCollection(); $this->choices = new ArrayCollection();
$this->comments = new ArrayCollection(); $this->comments = new ArrayCollection();
$this->stacksOfVotes = new ArrayCollection(); $this->stacksOfVotes = new ArrayCollection();
$this->setCreationDate( new \DateTime() ); $this->setCreationDate( new \DateTime() );
$this->setExpiracyDate( $this->addDaysToDate( $this->setExpiracyDate( $this->addDaysToDate(
new \DateTime(), new \DateTime(),
$this->defaultExpiracyDaysFromNow $this->defaultExpiracyDaysFromNow
) ); ) );
$this->setAllowedAnswers( [ 'yes' ] ); $this->setAllowedAnswers( [ 'yes' ] );
} }
public function findChoiceById( int $id ) { public function findChoiceById( int $id ) {
error_reporting( E_ALL ^ E_NOTICE ); error_reporting( E_ALL ^ E_NOTICE );
$choices = $this->getChoices(); $choices = $this->getChoices();
var_dump( count( $choices ) ); var_dump( count( $choices ) );
foreach ( $choices as $choice ) { // there must be something cleaner than this in Doctrine ArrayCollection
if ( $choice && $choice->getId() == $id ) { foreach ( $choices as $choice ) {
return $choice; if ( $choice && $choice->getId() == $id ) {
} return $choice;
}
return null;
} return null;
// if ( $choices && $choices->containsKey( $id ) ) { }
// $foundChoice = $choices->get( $id ); }
// } else {
// $foundChoice = new Choice();
// $foundChoice->setPoll( $this );
// }
//
// return $foundChoice;
}
public function addDaysToDate( \DateTime $date, int $days ) { public function addDaysToDate( \DateTime $date, int $days ) {
$st = strtotime( $date->getTimestamp() . ' + ' . $days . ' days' ); $st = strtotime( $date->getTimestamp() . ' + ' . $days . ' days' );
return new \DateTime( $st ); return new \DateTime( $st );
} }
public function getId(): ?int { public function getId(): ?int {
return $this->id; return $this->id;
} }
public function getTitle(): ?string { public function getTitle(): ?string {
return $this->title; return $this->title;
} }
public function setTitle( string $title ): self { public function setTitle( string $title ): self {
$this->title = $title; $this->title = $title;
return $this; return $this;
} }
public function getCreationDate(): ?DateTimeInterface { public function getCreationDate(): ?DateTimeInterface {
return $this->creationDate; return $this->creationDate;
} }
public function setCreationDate( DateTimeInterface $creationDate ): self { public function setCreationDate( DateTimeInterface $creationDate ): self {
$this->creationDate = $creationDate; $this->creationDate = $creationDate;
return $this; return $this;
} }
public function setExpiracyDate( DateTimeInterface $expiracyDate ): self { public function setExpiracyDate( DateTimeInterface $expiracyDate ): self {
$this->expiracyDate = $expiracyDate; $this->expiracyDate = $expiracyDate;
return $this; return $this;
} }
public function getOwner(): ?Owner { public function getOwner(): ?Owner {
return $this->owner; return $this->owner;
} }
public function setOwner( ?Owner $owner ): self { public function setOwner( ?Owner $owner ): self {
$this->owner = $owner; $this->owner = $owner;
return $this; return $this;
} }
/** /**
* @return Collection|Vote[] * @return Collection|Vote[]
*/ */
public function getVotes(): Collection { public function getVotes(): Collection {
return $this->votes; return $this->votes;
} }
public function getAdminKey(): ?string { public function getAdminKey(): ?string {
return $this->adminKey; return $this->adminKey;
} }
public function setAdminKey( string $adminKey ): self { public function setAdminKey( string $adminKey ): self {
$this->adminKey = $adminKey; $this->adminKey = $adminKey;
return $this; return $this;
} }
public function getDescription(): ?string { public function getDescription(): ?string {
return $this->description; return $this->description;
} }
public function setDescription( string $description ): self { public function setDescription( string $description ): self {
$this->description = $description; $this->description = $description;
return $this; return $this;
} }
public function getKind(): ?string { public function getKind(): ?string {
return $this->kind; return $this->kind;
} }
public function setKind( string $kind ): self { public function setKind( string $kind ): self {
$this->kind = $kind; $this->kind = $kind;
return $this; return $this;
} }
public function getCustomUrl(): ?string { public function getCustomUrl(): ?string {
return $this->customUrl; return $this->customUrl;
} }
public function setCustomUrl( string $customUrl ): self { public function setCustomUrl( string $customUrl ): self {
$this->customUrl = $customUrl; $this->customUrl = $customUrl;
return $this; return $this;
} }
public function getPassword(): ?string { public function getPassword(): ?string {
return $this->password; return $this->password;
} }
public function setPassword( string $password ): self { public function setPassword( string $password ): self {
$this->password = $password; $this->password = $password;
return $this; return $this;
} }
public function getModificationPolicy(): ?string { public function getModificationPolicy(): ?string {
return $this->modificationPolicy; return $this->modificationPolicy;
} }
public function setModificationPolicy( string $modificationPolicy ): self { public function setModificationPolicy( string $modificationPolicy ): self {
$this->modificationPolicy = $modificationPolicy; $this->modificationPolicy = $modificationPolicy;
return $this; return $this;
} }
public function getMailOnComment(): ?bool { public function getMailOnComment(): ?bool {
return $this->mailOnComment; return $this->mailOnComment;
} }
public function setMailOnComment( bool $mailOnComment ): self { public function setMailOnComment( bool $mailOnComment ): self {
$this->mailOnComment = $mailOnComment; $this->mailOnComment = $mailOnComment;
return $this; return $this;
} }
public function getMailOnVote(): ?bool { public function getMailOnVote(): ?bool {
return $this->mailOnVote; return $this->mailOnVote;
} }
public function setMailOnVote( bool $mailOnVote ): self { public function setMailOnVote( bool $mailOnVote ): self {
$this->mailOnVote = $mailOnVote; $this->mailOnVote = $mailOnVote;
return $this; return $this;
} }
public function getHideResults(): ?bool { public function getHideResults(): ?bool {
return $this->hideResults; return $this->hideResults;
} }
public function setHideResults( bool $hideResults ): self { public function setHideResults( bool $hideResults ): self {
$this->hideResults = $hideResults; $this->hideResults = $hideResults;
return $this; return $this;
} }
public function getShowResultEvenIfPasswords(): ?bool { public function getShowResultEvenIfPasswords(): ?bool {
return $this->showResultEvenIfPasswords; return $this->showResultEvenIfPasswords;
} }
public function setShowResultEvenIfPasswords( bool $showResultEvenIfPasswords ): self { public function setShowResultEvenIfPasswords( bool $showResultEvenIfPasswords ): self {
$this->showResultEvenIfPasswords = $showResultEvenIfPasswords; $this->showResultEvenIfPasswords = $showResultEvenIfPasswords;
return $this; return $this;
} }
/** /**
* @return Collection|Comment[] * @return Collection|Comment[]
*/ */
public function getComments(): Collection { public function getComments(): Collection {
return $this->comments; return $this->comments;
} }
public function addComment( Comment $comment ): self { public function addComment( Comment $comment ): self {
if ( ! $this->comments->contains( $comment ) ) { if ( ! $this->comments->contains( $comment ) ) {
$this->comments[] = $comment; $this->comments[] = $comment;
$comment->setPoll( $this ); $comment->setPoll( $this );
} }
return $this; return $this;
} }
public function removeComment( Comment $comment ): self { public function removeComment( Comment $comment ): self {
if ( $this->comments->contains( $comment ) ) { if ( $this->comments->contains( $comment ) ) {
$this->comments->removeElement( $comment ); $this->comments->removeElement( $comment );
// set the owning side to null (unless already changed) // set the owning side to null (unless already changed)
if ( $comment->getPoll() === $this ) { if ( $comment->getPoll() === $this ) {
$comment->setPoll( null ); $comment->setPoll( null );
} }
} }
return $this; return $this;
} }
public function getStacksOfVotes() { public function getStacksOfVotes() {
return $this->stacksOfVotes; return $this->stacksOfVotes;
} }
public function setStacksOfVotes( ?StackOfVotes $stacksOfVotes ): self { public function setStacksOfVotes( ?StackOfVotes $stacksOfVotes ): self {
$this->stacksOfVotes = $stacksOfVotes; $this->stacksOfVotes = $stacksOfVotes;
return $this; return $this;
} }
public function addStackOfVote( StackOfVotes $stackOfVote ): self { public function addStackOfVote( StackOfVotes $stackOfVote ): self {
if ( ! $this->stacksOfVotes->contains( $stackOfVote ) ) { if ( ! $this->stacksOfVotes->contains( $stackOfVote ) ) {
$this->stacksOfVotes[] = $stackOfVote; $this->stacksOfVotes[] = $stackOfVote;
$stackOfVote->setPoll( $this ); $stackOfVote->setPoll( $this );
} }
return $this; return $this;
} }
public function removeStackOfVote( StackOfVotes $stackOfVote ): self { public function removeStackOfVote( StackOfVotes $stackOfVote ): self {
if ( $this->stacksOfVotes->contains( $stackOfVote ) ) { if ( $this->stacksOfVotes->contains( $stackOfVote ) ) {
$this->stacksOfVotes->removeElement( $stackOfVote ); $this->stacksOfVotes->removeElement( $stackOfVote );
// set the owning side to null (unless already changed) // set the owning side to null (unless already changed)
if ( $stackOfVote->getPoll() === $this ) { if ( $stackOfVote->getPoll() === $this ) {
$stackOfVote->setPoll( null ); $stackOfVote->setPoll( null );
} }
} }
return $this; return $this;
} }
public function getExpiracyDate(): ?\DateTimeInterface { public function getExpiracyDate(): ?\DateTimeInterface {
return $this->expiracyDate; return $this->expiracyDate;
} }
public function addVote( Vote $vote ): self { public function addVote( Vote $vote ): self {
if ( ! $this->votes->contains( $vote ) ) { if ( ! $this->votes->contains( $vote ) ) {
$this->votes[] = $vote; $this->votes[] = $vote;
$vote->setPoll( $this ); $vote->setPoll( $this );
} }
return $this; return $this;
} }
public function removeVote( Vote $vote ): self { public function removeVote( Vote $vote ): self {
if ( $this->votes->contains( $vote ) ) { if ( $this->votes->contains( $vote ) ) {
$this->votes->removeElement( $vote ); $this->votes->removeElement( $vote );
// set the owning side to null (unless already changed) // set the owning side to null (unless already changed)
if ( $vote->getPoll() === $this ) { if ( $vote->getPoll() === $this ) {
$vote->setPoll( null ); $vote->setPoll( null );
} }
} }
return $this; return $this;
} }
/** /**
* @return Collection|Choice[] * @return Collection|Choice[]
*/ */
public function getChoices(): Collection { public function getChoices(): Collection {
return $this->choices; return $this->choices;
} }
public function addTextChoiceArray( Array $choiceTextArray ): self { public function addTextChoiceArray( Array $choiceTextArray ): self {
foreach ( $choiceTextArray as $text ) { foreach ( $choiceTextArray as $text ) {
$newChoice = new Choice(); $newChoice = new Choice();
$newChoice->setName( $text ); $newChoice->setName( $text );
$this->addChoice( $newChoice ); $this->addChoice( $newChoice );
} }
return $this; return $this;
} }
public function addChoice( Choice $choice ): self { public function addChoice( Choice $choice ): self {
if ( ! $this->choices->contains( $choice ) ) { if ( ! $this->choices->contains( $choice ) ) {
$this->choices[] = $choice; $this->choices[] = $choice;
$choice->setPoll( $this ); $choice->setPoll( $this );
} }
return $this; return $this;
} }
public function removeChoice( Choice $choice ): self { public function removeChoice( Choice $choice ): self {
if ( $this->choices->contains( $choice ) ) { if ( $this->choices->contains( $choice ) ) {
$this->choices->removeElement( $choice ); $this->choices->removeElement( $choice );
// set the owning side to null (unless already changed) // set the owning side to null (unless already changed)
if ( $choice->getPoll() === $this ) { if ( $choice->getPoll() === $this ) {
$choice->setPoll( null ); $choice->setPoll( null );
} }
} }
return $this; return $this;
} }
public function getAllowedAnswers(): ?array { public function getAllowedAnswers(): ?array {
return $this->allowedAnswers; return $this->allowedAnswers;
} }
public function setAllowedAnswers( array $allowedAnswers ): self { public function setAllowedAnswers( array $allowedAnswers ): self {
$this->allowedAnswers = $allowedAnswers; $this->allowedAnswers = $allowedAnswers;
return $this;
}
public function addStacksOfVote(StackOfVotes $stacksOfVote): self return $this;
{ }
if (!$this->stacksOfVotes->contains($stacksOfVote)) {
$this->stacksOfVotes[] = $stacksOfVote;
$stacksOfVote->setPoll($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 return $this;
{ }
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; 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;
}
} }