mirror of
https://framagit.org/tykayn/date-poll-api
synced 2023-08-25 08:23:11 +02:00
⚡ handle modifier token
This commit is contained in:
parent
158a795446
commit
c2995e9799
@ -8,6 +8,7 @@ use App\Entity\Poll;
|
|||||||
use App\Entity\StackOfVotes;
|
use App\Entity\StackOfVotes;
|
||||||
use App\Entity\Vote;
|
use App\Entity\Vote;
|
||||||
use FOS\RestBundle\Controller\Annotations\Delete;
|
use FOS\RestBundle\Controller\Annotations\Delete;
|
||||||
|
use FOS\RestBundle\Controller\Annotations\Patch;
|
||||||
use FOS\RestBundle\Controller\Annotations\Post;
|
use FOS\RestBundle\Controller\Annotations\Post;
|
||||||
use FOS\RestBundle\Controller\Annotations\Route;
|
use FOS\RestBundle\Controller\Annotations\Route;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
@ -22,7 +23,7 @@ use Symfony\Component\HttpFoundation\Request;
|
|||||||
class VoteController extends AbstractController {
|
class VoteController extends AbstractController {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* add a vote on a poll
|
* add a vote stack on a poll
|
||||||
* @Post(
|
* @Post(
|
||||||
* path = "/poll/{id}/vote",
|
* path = "/poll/{id}/vote",
|
||||||
* name = "new_vote_stack",
|
* name = "new_vote_stack",
|
||||||
@ -128,13 +129,55 @@ class VoteController extends AbstractController {
|
|||||||
'choices' => $choices,
|
'choices' => $choices,
|
||||||
'choices_count' => $poll->computeAnswers(),
|
'choices_count' => $poll->computeAnswers(),
|
||||||
'vote_count' => count( $poll->getStacksOfVotes() ),
|
'vote_count' => count( $poll->getStacksOfVotes() ),
|
||||||
'owner_modifier_token' => $foundOwner->getModifierToken(),
|
'owner' => $stack->getOwner(),
|
||||||
|
'owner_modifier_token' => $stack->getOwner()->getModifierToken(),
|
||||||
'admin_key' => $poll->getAdminKey(),
|
'admin_key' => $poll->getAdminKey(),
|
||||||
'json_you_sent' => $data,
|
'json_you_sent' => $data,
|
||||||
],
|
],
|
||||||
201 );
|
201 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* update vote stack
|
||||||
|
* @Patch(
|
||||||
|
* path = "/vote-stack/{id}/token/{modifierToken}",
|
||||||
|
* name = "update_vote_stack",
|
||||||
|
* requirements = { "id"="\d+"}
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
public
|
||||||
|
function updateVoteStackAction(
|
||||||
|
StackOfVotes $id,
|
||||||
|
$modifierToken,
|
||||||
|
Request $request
|
||||||
|
) {
|
||||||
|
$voteStack = $id;
|
||||||
|
if ( ! $voteStack ) {
|
||||||
|
return $this->json( [ 'message' => 'vote stack not found' ], 404 );
|
||||||
|
}
|
||||||
|
$poll = $voteStack->getPoll();
|
||||||
|
|
||||||
|
// if only self users are allowed to modify a vote, check it
|
||||||
|
if ( ! $modifierToken || $voteStack->getOwner()->getModifierToken() !== $modifierToken ) {
|
||||||
|
return $this->json( [ 'message' => 'your token does not allow you to modify this vote ' ],
|
||||||
|
403 );
|
||||||
|
}
|
||||||
|
// everything is ok, we can update all the votes of the vote stack
|
||||||
|
//TODO
|
||||||
|
// match votes and choices
|
||||||
|
// update answers
|
||||||
|
// save evrything
|
||||||
|
|
||||||
|
return $this->json( [
|
||||||
|
'message' => 'ok',
|
||||||
|
'modifier_token' => $voteStack->getOwner()->getModifierToken(),
|
||||||
|
'vote_stack' => $voteStack->display(),
|
||||||
|
],
|
||||||
|
200 );
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Delete(
|
* @Delete(
|
||||||
* path = "/poll/{id}/votes/{accessToken}",
|
* path = "/poll/{id}/votes/{accessToken}",
|
||||||
|
@ -45,10 +45,11 @@ class StackOfVotes {
|
|||||||
|
|
||||||
public function display() {
|
public function display() {
|
||||||
$tab = [
|
$tab = [
|
||||||
'id' => $this->getId(),
|
'id' => $this->getId(),
|
||||||
'pseudo' => '',
|
'modifier_token' => $this->getOwner()->getModifierToken(),
|
||||||
'creation_date' => '',
|
'pseudo' => '',
|
||||||
'votes' => [],
|
'creation_date' => '',
|
||||||
|
'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 ) {
|
||||||
@ -58,15 +59,10 @@ class StackOfVotes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach ( $this->getVotes() as $vote ) {
|
foreach ( $this->getVotes() as $vote ) {
|
||||||
$tab[ 'votes' ][ $vote->getChoice()->getId() ] = [
|
$tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display();
|
||||||
'id' => $this->getId(),
|
$tab[ 'votes' ][ $vote->getChoice()->getId() ][ 'stack_id' ] = $this->getId();
|
||||||
'vote_id' => $vote->getId(),
|
$tab[ 'pseudo' ] = $this->getOwner()->getPseudo();
|
||||||
'value' => $vote->getValue(),
|
$tab[ 'creation_date' ] = $vote->getCreationDate();
|
||||||
'choice_id' => $vote->getChoice()->getId(),
|
|
||||||
'text' => $vote->getChoice()->getName(),
|
|
||||||
];
|
|
||||||
$tab[ 'pseudo' ] = $this->getOwner()->getPseudo();
|
|
||||||
$tab[ 'creation_date' ] = $vote->getCreationDate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $tab;
|
return $tab;
|
||||||
|
@ -1,115 +1,124 @@
|
|||||||
<?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")
|
|
||||||
*/
|
|
||||||
class Vote {
|
|
||||||
/**
|
/**
|
||||||
* for a text kind of choice: could be "yes" "no" "maybe" and emptu.
|
* @ORM\Entity(repositoryClass="App\Repository\VoteRepository")
|
||||||
* 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()
|
|
||||||
*/
|
*/
|
||||||
public $value;
|
class Vote {
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"})
|
* for a text kind of choice: could be "yes" "no" "maybe" and emptu.
|
||||||
* @Serializer\Type("datetime")
|
* for a date kind, the choice linked is equivalent to the value selected
|
||||||
* @Serializer\Expose()
|
* @ORM\Column(type="string", length=255, nullable=true)
|
||||||
*/
|
* @Serializer\Type("string")
|
||||||
public $creationDate;
|
* @Serializer\Expose()
|
||||||
/**
|
*/
|
||||||
* @ORM\ManyToOne(targetEntity="App\Entity\Choice", inversedBy="votes", cascade={"persist"})
|
public $value;
|
||||||
* @ORM\JoinColumn(nullable=false)
|
/**
|
||||||
* @Serializer\Type("App\Entity\choice")
|
* @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"})
|
||||||
* @Serializer\Expose()
|
* @Serializer\Type("datetime")
|
||||||
*/
|
* @Serializer\Expose()
|
||||||
public $choice;
|
*/
|
||||||
/**
|
public $creationDate;
|
||||||
* @ORM\Id()
|
/**
|
||||||
* @ORM\GeneratedValue()
|
* @ORM\ManyToOne(targetEntity="App\Entity\Choice", inversedBy="votes", cascade={"persist"})
|
||||||
* @ORM\Column(type="integer")
|
* @ORM\JoinColumn(nullable=false)
|
||||||
* @Serializer\Type("integer")
|
* @Serializer\Type("App\Entity\choice")
|
||||||
* @Serializer\Expose()
|
* @Serializer\Expose()
|
||||||
*/
|
*/
|
||||||
private $id;
|
public $choice;
|
||||||
/**
|
/**
|
||||||
* @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="votes", cascade={"persist"})
|
* @ORM\Id()
|
||||||
* @ORM\JoinColumn(nullable=false)
|
* @ORM\GeneratedValue()
|
||||||
* @Serializer\Type("App\Entity\Poll")
|
* @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;
|
* @Serializer\Type("App\Entity\Poll")
|
||||||
|
*/
|
||||||
|
private $poll;
|
||||||
|
/**
|
||||||
|
* @ORM\ManyToOne(targetEntity="App\Entity\StackOfVotes", inversedBy="votes", cascade={"persist"})
|
||||||
|
* @ORM\JoinColumn(nullable=false)
|
||||||
|
* @Serializer\Type("App\Entity\StackOfVotes")
|
||||||
|
*/
|
||||||
|
private $stacksOfVotes;
|
||||||
|
|
||||||
public function __construct() {
|
public function display() {
|
||||||
$this->setCreationDate( new \DateTime() );
|
return [
|
||||||
}
|
'id' => $this->getId(),
|
||||||
|
'value' => $this->getValue(),
|
||||||
public function getId(): ?int {
|
'choice_id' => $this->getChoice()->getId(),
|
||||||
return $this->id;
|
'text' => $this->getChoice()->getName(),
|
||||||
}
|
];
|
||||||
|
|
||||||
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 __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 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user