1
0
mirror of https://framagit.org/tykayn/date-poll-api synced 2023-08-25 08:23:11 +02:00

compute vote counts in get poll config

This commit is contained in:
Baptiste Lemoine 2020-01-28 20:43:16 +01:00
parent 64a770d99a
commit 86fe1d5720
3 changed files with 1266 additions and 1232 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,144 +1,145 @@
<?php <?php
namespace App\Entity; namespace App\Entity;
use DateTime; use DateTime;
use DateTimeInterface; 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;
use JMS\Serializer\Annotation as Serializer; use JMS\Serializer\Annotation as Serializer;
/**
* one poll choice, could be a text or a date
* @ORM\Entity(repositoryClass="App\Repository\ChoiceRepository")
* @Serializer\ExclusionPolicy("all")
*/
class Choice {
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Serializer\Expose()
*/
public $id;
/** /**
* @ORM\Column(type="string", length=255, nullable=true) * one poll choice, could be a text or a date
* @Serializer\Type("string") * @ORM\Entity(repositoryClass="App\Repository\ChoiceRepository")
* @Serializer\Expose() * @Serializer\ExclusionPolicy("all")
*/ */
public $name; class Choice {
/** /**
* @ORM\Column(type="string", length=1024, nullable=true) * @ORM\Id()
* @Serializer\Type("string") * @ORM\GeneratedValue()
* @Serializer\Expose() * @ORM\Column(type="integer")
*/ * @Serializer\Expose()
public $url; */
public $id;
/** /**
* @ORM\Column(type="datetime", nullable=true) * @ORM\Column(type="string", length=255, nullable=true)
* @Serializer\Type("datetime") * @Serializer\Type("string")
*/ * @Serializer\Expose()
public $dateTime; */
/** public $name;
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="choice", cascade={"persist"}) /**
* @Serializer\Type("App\Entity\Vote") * @ORM\Column(type="string", length=1024, nullable=true)
*/ * @Serializer\Type("string")
public $votes; * @Serializer\Expose()
/** */
* @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="choices", cascade={"persist"}) public $url;
* @Serializer\Type("App\Entity\Poll")
*/
private $poll;
public function __construct( $optionalName = null ) { /**
$this->poll = new ArrayCollection(); * @ORM\Column(type="datetime", nullable=true)
$this->votes = new ArrayCollection(); * @Serializer\Type("datetime")
$this->setDateTime( new DateTime() ); */
if ( $optionalName ) { public $dateTime;
$this->setName( $optionalName ); /**
} * @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="choice", cascade={"persist"})
} * @Serializer\Type("App\Entity\Vote")
*/
public $votes;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="choices", cascade={"persist"})
* @Serializer\Type("App\Entity\Poll")
*/
private $poll;
public function display() { public function __construct( $optionalName = null ) {
return [ $this->poll = new ArrayCollection();
'id' => $this->getId(), $this->votes = new ArrayCollection();
'date' => $this->getDateTime(), $this->setDateTime( new DateTime() );
'text' => $this->getName(), if ( $optionalName ) {
'url' => $this->getUrl(), $this->setName( $optionalName );
];
}
public function getId(): ?int {
return $this->id;
}
public function getDateTime(): ?DateTimeInterface {
return $this->dateTime;
}
public function setDateTime( ?DateTimeInterface $dateTime ): self {
$this->dateTime = $dateTime;
return $this;
}
public function getName(): ?string {
return $this->name;
}
public function setName( ?string $name ): self {
$this->name = $name;
return $this;
}
public function getPoll(): ?Poll {
return $this->poll;
}
public function setPoll( ?Poll $poll ): self {
$this->poll = $poll;
return $this;
}
/**
* @return Collection|Vote[]
*/
public function getVotes(): Collection {
return $this->votes;
}
public function addVote( Vote $vote ): self {
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;
}
public function getUrl(): ?string { public function display() {
return $this->url; return [
} 'id' => $this->getId(),
'date' => $this->getDateTime(),
'text' => $this->getName(),
'url' => $this->getUrl(),
];
}
public function setUrl( ?string $url ): self { public function getId(): ?int {
$this->url = $url; return $this->id;
}
return $this; public function getDateTime(): ?DateTimeInterface {
return $this->dateTime;
}
public function setDateTime( ?DateTimeInterface $dateTime ): self {
$this->dateTime = $dateTime;
return $this;
}
public function getName(): ?string {
return $this->name;
}
public function setName( ?string $name ): self {
$this->name = $name;
return $this;
}
public function getPoll(): ?Poll {
return $this->poll;
}
public function setPoll( ?Poll $poll ): self {
$this->poll = $poll;
return $this;
}
/**
* @return Collection|Vote[]
*/
public function getVotes(): Collection {
return $this->votes;
}
public function addVote( Vote $vote ): self {
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;
}
public function getUrl(): ?string {
return $this->url;
}
public function setUrl( ?string $url ): self {
$this->url = $url;
return $this;
}
} }
}

File diff suppressed because it is too large Load Diff