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

add timestamp trait

This commit is contained in:
Tykayn 2021-04-27 10:21:21 +02:00 committed by tykayn
parent 30c99568dd
commit 56c8b0a5c4
5 changed files with 172 additions and 137 deletions

View File

@ -233,8 +233,13 @@ class Poll {
} }
} }
} }
$answersWithStats = [];
foreach ( $computedArray as $choice_stat ) {
$answersWithStats[] = $choice_stat;
}
return [ return [
'answers' => $computedArray, 'answers' => $computedArray,
'answersWithStats' => $answersWithStats,
'max_score' => $maxScore, 'max_score' => $maxScore,
]; ];
} }
@ -254,6 +259,10 @@ class Poll {
foreach ( $this->getStacksOfVotes() as $stack ) { foreach ( $this->getStacksOfVotes() as $stack ) {
$displayedStackOfVotes[] = $stack->display(); $displayedStackOfVotes[] = $stack->display();
} }
$displayedChoices = [];
foreach ( $this->getChoices() as $choice ) {
$displayedChoices[] = $choice->display();
}
$displayedComments = []; $displayedComments = [];
foreach ( $this->getComments() as $comment ) { foreach ( $this->getComments() as $comment ) {
$displayedComments[] = $comment->display(); $displayedComments[] = $comment->display();
@ -278,7 +287,8 @@ class Poll {
, ,
'password_protected' => $this->getPassword() ? 'yes' : 'no', 'password_protected' => $this->getPassword() ? 'yes' : 'no',
'max_score' => $computedAnswers['max_score'], 'max_score' => $computedAnswers['max_score'],
'choices' => $computedAnswers['answers'], 'choices' => $displayedChoices,
'choices_stats' => $computedAnswers['answersWithStats'],
'stacks' => $displayedStackOfVotes, 'stacks' => $displayedStackOfVotes,
'comments' => $displayedComments, 'comments' => $displayedComments,
]; ];

View File

@ -2,6 +2,7 @@
namespace App\Entity; namespace App\Entity;
use App\Traits\TimeStampableTrait;
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;
@ -14,6 +15,9 @@ use JMS\Serializer\Annotation as Serializer;
* @Serializer\ExclusionPolicy("all") * @Serializer\ExclusionPolicy("all")
*/ */
class StackOfVotes { class StackOfVotes {
use TimeStampableTrait;
/** /**
* @ORM\Id() * @ORM\Id()
* @ORM\GeneratedValue() * @ORM\GeneratedValue()
@ -44,26 +48,25 @@ class StackOfVotes {
public function display() { public function display() {
$votes = $this->getVotes();
$tab = [ $tab = [
'id' => $this->getId(), // 'id' => $this->getId(),
// 'modifier_token' => $this->getOwner()->getModifierToken(), // 'modifier_token' => $this->getOwner()->getModifierToken(),
'pseudo' => '', 'pseudo' => $this->getPseudo(),
'creation_date' => '', 'created_at' => $this->getCreatedAtAsString(),
'votes' => [], 'votes' => [],
]; ];
// prefill votes with all choices ids // prefill votes with all choices ids
foreach ( $this->getPoll()->getChoices() as $choice ) { // foreach ( $this->getPoll()->getChoices() as $choice ) {
$tab[ 'votes' ][ $choice->getId() ] = [ // $tab[ 'votes' ][ $choice->getId() ] = [
'choice_id' => $choice->getId(), // 'choice_id' => $choice->getId(),
'name' => $choice->getName(), // 'value' => null,
]; // ];
} // }
foreach ( $this->getVotes() as $vote ) { foreach ( $votes as $vote ) {
// $tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display(); $tab[ 'votes' ][$vote->getChoice()->getId()] = $vote->display();
$tab[ 'votes' ][ $vote->getChoice()->getId() ][ 'stack_id' ] = $this->getId();
$tab[ 'pseudo' ] = $this->getPseudo();
$tab[ 'creation_date' ] = $vote->getCreationDate();
} }
return $tab; return $tab;

View File

@ -1,124 +1,130 @@
<?php <?php
namespace App\Entity; namespace App\Entity;
use DateTimeInterface; use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer; use JMS\Serializer\Annotation as Serializer;
/**
* @ORM\Entity(repositoryClass="App\Repository\VoteRepository")
* @Serializer\ExclusionPolicy("all")
*/
class Vote {
/** /**
* @ORM\Entity(repositoryClass="App\Repository\VoteRepository") * for a text kind of choice: could be "yes" "no" "maybe" and empty.
* @Serializer\ExclusionPolicy("all") * for a date kind, the choice linked is equivalent to the value selected
* @ORM\Column(type="string", length=255, nullable=true)
* @Serializer\Type("string")
* @Serializer\Expose()
*/ */
class Vote { public $value;
/** /**
* for a text kind of choice: could be "yes" "no" "maybe" and empty. * @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"})
* for a date kind, the choice linked is equivalent to the value selected * @Serializer\Type("datetime")
* @ORM\Column(type="string", length=255, nullable=true) * @Serializer\Expose()
* @Serializer\Type("string") */
* @Serializer\Expose() public $creationDate;
*/ /**
public $value; * @ORM\ManyToOne(targetEntity="App\Entity\Choice", inversedBy="votes", cascade={"persist"})
/** * @ORM\JoinColumn(nullable=false)
* @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"}) * @Serializer\Type("App\Entity\choice")
* @Serializer\Type("datetime") * @Serializer\Expose()
* @Serializer\Expose() */
*/ public $choice;
public $creationDate; /**
/** * @ORM\Id()
* @ORM\ManyToOne(targetEntity="App\Entity\Choice", inversedBy="votes", cascade={"persist"}) * @ORM\GeneratedValue()
* @ORM\JoinColumn(nullable=false) * @ORM\Column(type="integer")
* @Serializer\Type("App\Entity\choice") * @Serializer\Type("integer")
* @Serializer\Expose() * @Serializer\Expose()
*/ */
public $choice; private $id;
/** /**
* @ORM\Id() * @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="votes", cascade={"persist"})
* @ORM\GeneratedValue() * @ORM\JoinColumn(nullable=false)
* @ORM\Column(type="integer") */
* @Serializer\Type("integer") private $poll;
* @Serializer\Expose() /**
*/ * @ORM\ManyToOne(targetEntity="App\Entity\StackOfVotes", inversedBy="votes", cascade={"persist"})
private $id; * @ORM\JoinColumn(nullable=false)
/** * @Serializer\Type("App\Entity\StackOfVotes")
* @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="votes", cascade={"persist"}) */
* @ORM\JoinColumn(nullable=false) private $stacksOfVotes;
*/
private $poll; public function display() {
/** $value = $this->getValue();
* @ORM\ManyToOne(targetEntity="App\Entity\StackOfVotes", inversedBy="votes", cascade={"persist"}) if ( ! $value ) {
* @ORM\JoinColumn(nullable=false) return null;
* @Serializer\Type("App\Entity\StackOfVotes") } else {
*/
private $stacksOfVotes;
public function display() {
return [ return [
'id' => $this->getId(), 'id' => $this->getId(),
'value' => $this->getValue(), 'value' => $this->getValue(),
'choice_id' => $this->getChoice()->getId(), 'choice_id' => $this->getChoice()->getId(),
'text' => $this->getChoice()->getName(), 'text' => $this->getChoice()->getName(),
]; ];
} }
public function __construct() {
$this->setCreationDate( new \DateTime() );
}
public function getId(): ?int {
return $this->id;
}
public function getPoll(): ?Poll {
return $this->poll;
}
public function setPoll( ?Poll $poll ): self {
$this->poll = $poll;
if ( $poll ) {
$poll->addVote( $this );
}
return $this;
}
public function getChoice(): ?Choice {
return $this->choice;
}
public function setChoice( ?Choice $choice ): self {
$this->choice = $choice;
return $this;
}
public function getValue(): ?string {
return $this->value;
}
public function setValue( ?string $value ): self {
$this->value = $value;
return $this;
}
public function getCreationDate(): ?DateTimeInterface {
return $this->creationDate;
}
public function setCreationDate( DateTimeInterface $creationDate ): self {
$this->creationDate = $creationDate;
return $this;
}
public function getStacksOfVotes(): ?StackOfVotes {
return $this->stacksOfVotes;
}
public function setStacksOfVotes( ?StackOfVotes $stacksOfVotes ): self {
$this->stacksOfVotes = $stacksOfVotes;
return $this;
}
} }
public function __construct() {
$this->setCreationDate( new \DateTime() );
}
public function getId(): ?int {
return $this->id;
}
public function getPoll(): ?Poll {
return $this->poll;
}
public function setPoll( ?Poll $poll ): self {
$this->poll = $poll;
if ( $poll ) {
$poll->addVote( $this );
}
return $this;
}
public function getChoice(): ?Choice {
return $this->choice;
}
public function setChoice( ?Choice $choice ): self {
$this->choice = $choice;
return $this;
}
public function getValue(): ?string {
return $this->value;
}
public function setValue( ?string $value ): self {
$this->value = $value;
return $this;
}
public function getCreationDate(): ?DateTimeInterface {
return $this->creationDate;
}
public function setCreationDate( DateTimeInterface $creationDate ): self {
$this->creationDate = $creationDate;
return $this;
}
public function getStacksOfVotes(): ?StackOfVotes {
return $this->stacksOfVotes;
}
public function setStacksOfVotes( ?StackOfVotes $stacksOfVotes ): self {
$this->stacksOfVotes = $stacksOfVotes;
return $this;
}
}

View File

@ -1,12 +0,0 @@
<?php
namespace App\Traits;
use Doctrine\ORM\Mapping as ORM;
trait Timed {
/**
* @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"})
*/
private $createdAt;
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Traits;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
use PhpParser\Node\Scalar\String_;
trait TimeStampableTrait {
/**
* @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"})
*/
private $createdAt;
public function getCreatedAt(): ?DateTimeInterface {
return $this->createdAt;
}
public function getCreatedAtAsString(): string {
return $this->createdAt->format('c');
}
public function setCreatedAt( DateTimeInterface $createdAt ): self {
$this->createdAt = $createdAt;
return $this;
}
}