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

use trait timestamp and replace

This commit is contained in:
Tykayn 2021-04-27 12:51:10 +02:00 committed by tykayn
parent ecdbf3fb33
commit 94c02be927
9 changed files with 121 additions and 119 deletions

View File

@ -74,7 +74,7 @@ class MigrationController extends EmailsController {
->setChoicesMax( $d->ValueMax ) ->setChoicesMax( $d->ValueMax )
->setPassword( $d->password_hash ) ->setPassword( $d->password_hash )
->setDescription( $d->description ) ->setDescription( $d->description )
->setCreationDate( date_create( $d->creation_date ) ); ->setCreatedAt( date_create( $d->creation_date ) );
$pollsBySlug[ $d->id ] = $newPoll; $pollsBySlug[ $d->id ] = $newPoll;

View File

@ -142,7 +142,7 @@ class PollController extends EmailsController {
return $this->notFoundPoll( $customUrl ); return $this->notFoundPoll( $customUrl );
} }
if ( $poll->getPassword() === $md5 ) { if ( md5($poll->getPassword()) === $md5 ) {
// good matching pass // good matching pass
return $this->json( $poll->display() ); return $this->json( $poll->display() );
} else { } else {

View File

@ -53,6 +53,7 @@ class Choice {
private $poll; private $poll;
public function __construct( $optionalName = null ) { public function __construct( $optionalName = null ) {
$this->setCreatedAt( new DateTime());
$this->poll = new ArrayCollection(); $this->poll = new ArrayCollection();
$this->votes = new ArrayCollection(); $this->votes = new ArrayCollection();
$this->setDateTime( new DateTime() ); $this->setDateTime( new DateTime() );

View File

@ -66,6 +66,7 @@ class Owner {
$this->stackOfVotes = new ArrayCollection(); $this->stackOfVotes = new ArrayCollection();
$this->setCreatedAt( new DateTime() ); $this->setCreatedAt( new DateTime() );
$this->setModifierToken( uniqid() ); $this->setModifierToken( uniqid() );
$this->setCreatedAt( new DateTime());
} }
public function setCreatedAt( DateTimeInterface $createdAt ): self { public function setCreatedAt( DateTimeInterface $createdAt ): self {

View File

@ -3,6 +3,7 @@
namespace App\Entity; namespace App\Entity;
use App\Traits\RandomTrait; use App\Traits\RandomTrait;
use App\Traits\TimeStampableTrait;
use DateTime; use DateTime;
use DateTimeInterface; use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
@ -19,6 +20,8 @@ class Poll {
use RandomTrait; use RandomTrait;
use TimeStampableTrait;
/** /**
* @ORM\Id() * @ORM\Id()
* @ORM\GeneratedValue() * @ORM\GeneratedValue()
@ -46,11 +49,7 @@ class Poll {
* @Serializer\Type("string") * @Serializer\Type("string")
*/ */
public $description; public $description;
/**
* @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"})
* @Serializer\Expose()
*/
public $creationDate;
/** /**
* @ORM\Column(type="datetime") * @ORM\Column(type="datetime")
* @Serializer\Expose() * @Serializer\Expose()
@ -196,6 +195,7 @@ class Poll {
public function __construct() { public function __construct() {
$this->initiate(); $this->initiate();
$this->setCreatedAt( new DateTime());
$this->votes = new ArrayCollection(); $this->votes = new ArrayCollection();
$this->stacksOfVotes = new ArrayCollection(); $this->stacksOfVotes = new ArrayCollection();
$this->choices = new ArrayCollection(); $this->choices = new ArrayCollection();
@ -208,7 +208,7 @@ class Poll {
$this->choices = new ArrayCollection(); $this->choices = new ArrayCollection();
$this->comments = new ArrayCollection(); $this->comments = new ArrayCollection();
$this->setAdminKey( $this->generateRandomKey() ); $this->setAdminKey( $this->generateRandomKey() );
$this->setCreationDate( new DateTime() ); $this->setCreatedAt( new DateTime() );
$this->setExpiracyDate( $this->addDaysToDate( $this->setExpiracyDate( $this->addDaysToDate(
new DateTime(), new DateTime(),
$this->defaultExpiracyDaysFromNow $this->defaultExpiracyDaysFromNow
@ -247,7 +247,7 @@ class Poll {
return [ return [
'title' => $this->getTitle(), 'title' => $this->getTitle(),
'description' => $this->getDescription(), 'description' => $this->getDescription(),
'created_at' => $this->getCreationDate()->format( 'c' ), 'created_at' => $this->getCreatedAt()->format( 'c' ),
'expiracy_date' => $this->getExpiracyDate()->format( 'c' ), 'expiracy_date' => $this->getExpiracyDate()->format( 'c' ),
'votes_max' => $this->getVotesMax(), 'votes_max' => $this->getVotesMax(),
'choices_max' => $this->getChoicesMax(), 'choices_max' => $this->getChoicesMax(),
@ -274,23 +274,25 @@ class Poll {
$computedArray = []; $computedArray = [];
$maxScore = 0; $maxScore = 0;
$scoreInfos = ['score' => 0, $scoreInfos = [
'yes' => [ 'score' => 0,
'count' => 0, 'yes' => [
'people' => [], 'count' => 0,
], 'people' => [],
'maybe' => [ ],
'count' => 0, 'maybe' => [
'people' => [], 'count' => 0,
], 'people' => [],
'no' => [ ],
'count' => 0, 'no' => [
'people' => [], 'count' => 0,
]]; 'people' => [],
],
];
// first, prefill all choices // first, prefill all choices
foreach ( $this->getChoices() as $choice ) { foreach ( $this->getChoices() as $choice ) {
$computedArray[ $choice->getId() ] = array_merge($scoreInfos, $choice->display($this->getKind())); $computedArray[ $choice->getId() ] = array_merge( $scoreInfos, $choice->display( $this->getKind() ) );
} }
// then, compute stack of votes scores on each choice // then, compute stack of votes scores on each choice
foreach ( $this->getStacksOfVotes() as $stack_of_vote ) { foreach ( $this->getStacksOfVotes() as $stack_of_vote ) {
@ -301,9 +303,9 @@ class Poll {
if ( ! isset( $computedArray[ $choice_id ] ) ) { if ( ! isset( $computedArray[ $choice_id ] ) ) {
$computedArray[ $choice_id ] = [ $computedArray[ $choice_id ] = [
'id' => $choice_id, 'id' => $choice_id,
'url' => $choice_url, 'url' => $choice_url,
'name' => $vote->getChoice()->getName(), 'name' => $vote->getChoice()->getName(),
]; ];
} }
@ -327,8 +329,8 @@ class Poll {
} }
return [ return [
'answers' => $answersWithStats, 'answers' => $answersWithStats,
'max_score' => $maxScore, 'max_score' => $maxScore,
]; ];
} }
@ -376,12 +378,12 @@ class Poll {
return $this; return $this;
} }
public function getCreationDate(): ?DateTimeInterface { public function getCreatedAt(): ?DateTimeInterface {
return $this->creationDate; return $this->createdAt;
} }
public function setCreationDate( DateTimeInterface $creationDate ): self { public function setCreatedAt( DateTimeInterface $createdAt ): self {
$this->creationDate = $creationDate; $this->createdAt = $createdAt;
return $this; return $this;
} }

View File

@ -3,6 +3,7 @@
namespace App\Entity; namespace App\Entity;
use App\Traits\TimeStampableTrait; use App\Traits\TimeStampableTrait;
use DateTime;
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;
@ -52,114 +53,113 @@ class StackOfVotes {
private $ip; private $ip;
public function __construct() { public function __construct() {
$this->votes = new ArrayCollection(); $this->setCreatedAt( new DateTime() );
} $this->votes = new ArrayCollection();
}
public function display() { public function display() {
$votes = $this->getVotes(); $votes = $this->getVotes();
$tab = [ $tab = [
// 'id' => $this->getId(), // 'id' => $this->getId(),
// 'modifier_token' => $this->getOwner()->getModifierToken(), // 'modifier_token' => $this->getOwner()->getModifierToken(),
'pseudo' => $this->getPseudo(), 'pseudo' => $this->getPseudo(),
'created_at' => $this->getCreatedAtAsString(), '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(),
// 'value' => null, // 'value' => null,
// ]; // ];
// } // }
foreach ( $votes as $vote ) { foreach ( $votes as $vote ) {
$tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display(); $tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display();
} }
return $tab; return $tab;
} }
/** /**
* @return Collection|poll[] * @return Collection|poll[]
*/ */
public function getVotes(): Collection { public function getVotes(): Collection {
return $this->votes; return $this->votes;
} }
public function getPseudo(): ?string { public function getPseudo(): ?string {
return $this->pseudo; return $this->pseudo;
} }
public function setPseudo( ?string $pseudo ): self { public function setPseudo( ?string $pseudo ): self {
$this->pseudo = $pseudo; $this->pseudo = $pseudo;
return $this; return $this;
} }
/** /**
* @ORM\PrePersist * @ORM\PrePersist
*/ */
public function prePersist() { public function prePersist() {
$this->setPseudo( $this->getOwner()->getPseudo() ); $this->setPseudo( $this->getOwner()->getPseudo() );
} }
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;
} }
public function getId(): ?int { public function getId(): ?int {
return $this->id; return $this->id;
} }
public function addVote( Vote $vote ): self { public function addVote( Vote $vote ): self {
if ( ! $this->votes->contains( $vote ) ) { if ( ! $this->votes->contains( $vote ) ) {
$vote->setPoll( $this->getPoll() ); $vote->setPoll( $this->getPoll() );
$this->votes[] = $vote; $this->votes[] = $vote;
$vote->setStacksOfVotes( $this ); $vote->setStacksOfVotes( $this );
} }
return $this; return $this;
} }
public function getPoll(): ?Poll { public function getPoll(): ?Poll {
return $this->poll; return $this->poll;
} }
public function setPoll( ?Poll $poll ): self { public function setPoll( ?Poll $poll ): self {
$this->poll = $poll; $this->poll = $poll;
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->getStacksOfVotes() === $this ) { if ( $vote->getStacksOfVotes() === $this ) {
$vote->setStacksOfVotes( null ); $vote->setStacksOfVotes( null );
} }
} }
return $this; return $this;
} }
public function getIp(): ?string public function getIp(): ?string {
{ return $this->ip;
return $this->ip; }
}
public function setIp(string $ip): self public function setIp( string $ip ): self {
{ $this->ip = $ip;
$this->ip = $ip;
return $this; return $this;
} }
} }

View File

@ -2,6 +2,7 @@
namespace App\Entity; namespace App\Entity;
use App\Traits\TimeStampableTrait;
use DateTime; use DateTime;
use DateTimeInterface; use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
@ -12,6 +13,9 @@ use JMS\Serializer\Annotation as Serializer;
* @Serializer\ExclusionPolicy("all") * @Serializer\ExclusionPolicy("all")
*/ */
class Vote { class Vote {
use TimeStampableTrait;
/** /**
* for a text kind of choice: could be "yes" "no" "maybe" and empty. * for a text kind of choice: could be "yes" "no" "maybe" and empty.
* for a date kind, the choice linked is equivalent to the value selected * for a date kind, the choice linked is equivalent to the value selected
@ -20,12 +24,7 @@ class Vote {
* @Serializer\Expose() * @Serializer\Expose()
*/ */
public $value; public $value;
/**
* @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"})
* @Serializer\Type("datetime")
* @Serializer\Expose()
*/
public $creationDate;
/** /**
* @ORM\ManyToOne(targetEntity="App\Entity\Choice", inversedBy="votes", cascade={"persist"}) * @ORM\ManyToOne(targetEntity="App\Entity\Choice", inversedBy="votes", cascade={"persist"})
* @ORM\JoinColumn(nullable=false) * @ORM\JoinColumn(nullable=false)
@ -54,7 +53,7 @@ class Vote {
private $stacksOfVotes; private $stacksOfVotes;
public function __construct() { public function __construct() {
$this->setCreationDate( new DateTime() ); $this->setCreatedAt( new DateTime());
} }
public function display() { public function display() {

View File

@ -13,7 +13,7 @@ class PollType extends AbstractType {
->add( 'title' ) ->add( 'title' )
->add( 'customUrl' ) ->add( 'customUrl' )
->add( 'description' ) ->add( 'description' )
->add( 'creationDate' ) ->add( 'createdAt' )
->add( 'expiracyDate' ) ->add( 'expiracyDate' )
->add( 'kind' ) ->add( 'kind' )
->add( 'allowedAnswers' ) ->add( 'allowedAnswers' )

View File

@ -28,9 +28,8 @@ trait TimeStampableTrait {
public function addDaysToDate( DateTime $date, int $days ) { public function addDaysToDate( DateTime $date, int $days ) {
$st = strtotime( $date->getTimestamp() );
return new DateTime( $st ); return $date->add(new \DateInterval('P'.$days.'D'));
} }