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
composer install
```
### initiate the database
### 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

View File

@ -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",

View File

@ -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 ) {

View File

@ -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,13 +43,24 @@ class Choice {
*/
public $votes;
public function __construct() {
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;
}
@ -59,26 +69,25 @@ class Choice {
return $this->name;
}
public function setName( $name ): self {
if ( is_a( $name, 'DateTime' ) ) {
$this->setDateTime( $name );
$name = $name->format( 'D Y-m-d' );
}
public function setName( ?string $name ): self {
$this->name = $name;
return $this;
}
public function getDateTime(): ?DateTimeInterface {
public function getDateTime(): ?\DateTimeInterface {
return $this->dateTime;
}
public function setDateTime( ?DateTimeInterface $dateTime ): self {
public function setDateTime( ?\DateTimeInterface $dateTime ): self {
$this->dateTime = $dateTime;
return $this;
}
public function getPoll(): ?Poll {
return $this->poll;
}
/**
* @return Collection|Vote[]
@ -105,18 +114,6 @@ class Choice {
}
}
return $this;
}
public function getPoll(): ?Poll
{
return $this->poll;
}
public function setPoll(?Poll $poll): self
{
$this->poll = $poll;
return $this;
}
}

View File

@ -160,6 +160,7 @@ class Poll {
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;
@ -167,14 +168,6 @@ class Poll {
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 ) {
@ -465,8 +458,7 @@ class Poll {
return $this;
}
public function addStacksOfVote(StackOfVotes $stacksOfVote): self
{
public function addStacksOfVote( StackOfVotes $stacksOfVote ): self {
if ( ! $this->stacksOfVotes->contains( $stacksOfVote ) ) {
$this->stacksOfVotes[] = $stacksOfVote;
$stacksOfVote->setPoll( $this );
@ -475,8 +467,7 @@ class Poll {
return $this;
}
public function removeStacksOfVote(StackOfVotes $stacksOfVote): self
{
public function removeStacksOfVote( StackOfVotes $stacksOfVote ): self {
if ( $this->stacksOfVotes->contains( $stacksOfVote ) ) {
$this->stacksOfVotes->removeElement( $stacksOfVote );
// set the owning side to null (unless already changed)