mirror of
https://framagit.org/tykayn/date-poll-api
synced 2023-08-25 08:23:11 +02:00
⚡ advance on choice create
This commit is contained in:
parent
65c32d07ed
commit
fc40302aa4
@ -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
|
||||||
|
20
examples.md
20
examples.md
@ -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",
|
||||||
|
@ -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 ) {
|
||||||
|
@ -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,13 +43,24 @@ 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;
|
||||||
}
|
}
|
||||||
@ -59,26 +69,25 @@ class Choice {
|
|||||||
return $this->name;
|
return $this->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setName( $name ): self {
|
public function setName( ?string $name ): self {
|
||||||
if ( is_a( $name, 'DateTime' ) ) {
|
|
||||||
$this->setDateTime( $name );
|
|
||||||
$name = $name->format( 'D Y-m-d' );
|
|
||||||
}
|
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDateTime(): ?DateTimeInterface {
|
public function getDateTime(): ?\DateTimeInterface {
|
||||||
return $this->dateTime;
|
return $this->dateTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setDateTime( ?DateTimeInterface $dateTime ): self {
|
public function setDateTime( ?\DateTimeInterface $dateTime ): self {
|
||||||
$this->dateTime = $dateTime;
|
$this->dateTime = $dateTime;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getPoll(): ?Poll {
|
||||||
|
return $this->poll;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Collection|Vote[]
|
* @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;
|
return $this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
@ -160,6 +160,7 @@ class Poll {
|
|||||||
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 ) );
|
||||||
|
// there must be something cleaner than this in Doctrine ArrayCollection
|
||||||
foreach ( $choices as $choice ) {
|
foreach ( $choices as $choice ) {
|
||||||
if ( $choice && $choice->getId() == $id ) {
|
if ( $choice && $choice->getId() == $id ) {
|
||||||
return $choice;
|
return $choice;
|
||||||
@ -167,14 +168,6 @@ class Poll {
|
|||||||
|
|
||||||
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 ) {
|
||||||
@ -465,23 +458,21 @@ class Poll {
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addStacksOfVote(StackOfVotes $stacksOfVote): self
|
public function addStacksOfVote( StackOfVotes $stacksOfVote ): self {
|
||||||
{
|
if ( ! $this->stacksOfVotes->contains( $stacksOfVote ) ) {
|
||||||
if (!$this->stacksOfVotes->contains($stacksOfVote)) {
|
|
||||||
$this->stacksOfVotes[] = $stacksOfVote;
|
$this->stacksOfVotes[] = $stacksOfVote;
|
||||||
$stacksOfVote->setPoll($this);
|
$stacksOfVote->setPoll( $this );
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeStacksOfVote(StackOfVotes $stacksOfVote): self
|
public function removeStacksOfVote( StackOfVotes $stacksOfVote ): self {
|
||||||
{
|
if ( $this->stacksOfVotes->contains( $stacksOfVote ) ) {
|
||||||
if ($this->stacksOfVotes->contains($stacksOfVote)) {
|
$this->stacksOfVotes->removeElement( $stacksOfVote );
|
||||||
$this->stacksOfVotes->removeElement($stacksOfVote);
|
|
||||||
// set the owning side to null (unless already changed)
|
// set the owning side to null (unless already changed)
|
||||||
if ($stacksOfVote->getPoll() === $this) {
|
if ( $stacksOfVote->getPoll() === $this ) {
|
||||||
$stacksOfVote->setPoll(null);
|
$stacksOfVote->setPoll( null );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user