mirror of
https://framagit.org/tykayn/date-poll-api
synced 2023-08-25 08:23:11 +02:00
166 lines
4.4 KiB
PHP
166 lines
4.4 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Controller;
|
||
|
|
||
|
use App\Entity\Choice;
|
||
|
use App\Entity\Owner;
|
||
|
use App\Entity\Poll;
|
||
|
use App\Entity\StackOfVotes;
|
||
|
use App\Entity\Vote;
|
||
|
use FOS\RestBundle\Controller\Annotations\Route;
|
||
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||
|
use Symfony\Component\HttpFoundation\Request;
|
||
|
|
||
|
/**
|
||
|
* Class DefaultController
|
||
|
* @package App\Controller
|
||
|
* @Route("/api/v1",name="api_")
|
||
|
*/
|
||
|
class VoteController extends AbstractController {
|
||
|
|
||
|
/**
|
||
|
* add a vote on a poll
|
||
|
* @Post(
|
||
|
* path = "/poll/{id}/vote",
|
||
|
* name = "new_vote_stack",
|
||
|
* requirements = {"content"="\w+", "poll_id"="\d+"}
|
||
|
* )
|
||
|
*/
|
||
|
public
|
||
|
function newVoteStackAction(
|
||
|
Poll $poll,
|
||
|
Request $request
|
||
|
) {
|
||
|
if ( ! $poll ) {
|
||
|
return $this->json( [ 'message' => 'poll not found' ], 404 );
|
||
|
}
|
||
|
|
||
|
$em = $this->getDoctrine()->getManager();
|
||
|
$data = $request->getContent();
|
||
|
$data = json_decode( $data, true );
|
||
|
|
||
|
|
||
|
$emOwner = $this->getDoctrine()->getRepository( Owner::class );
|
||
|
$emChoice = $this->getDoctrine()->getRepository( Choice::class );
|
||
|
$existingOwner = false;
|
||
|
$foundOwner = $emOwner->findOneByEmail( trim( $data[ 'email' ] ) );
|
||
|
// manage existing or new Owner
|
||
|
if ( ! $foundOwner ) {
|
||
|
$foundOwner = new Owner();
|
||
|
$foundOwner
|
||
|
->setEmail( $data[ 'email' ] )
|
||
|
->setPseudo( $data[ 'pseudo' ] );
|
||
|
} else {
|
||
|
$existingOwner = true;
|
||
|
}
|
||
|
// TODO anti flood
|
||
|
$foundOwner
|
||
|
->setModifierToken( $poll->generateAdminKey() );
|
||
|
$stack = new StackOfVotes();
|
||
|
$stack
|
||
|
->setOwner( $foundOwner )
|
||
|
->setPseudo( $data[ 'pseudo' ] )
|
||
|
->setPoll( $poll );
|
||
|
foreach ( $data[ 'votes' ] as $voteInfo ) {
|
||
|
|
||
|
if ( ! isset( $voteInfo[ 'value' ] ) ) {
|
||
|
continue;
|
||
|
}
|
||
|
$allowedValuesToAnswer = [ 'yes', 'maybe', 'no' ];
|
||
|
|
||
|
if ( ! in_array( $voteInfo[ 'value' ], $allowedValuesToAnswer ) ) {
|
||
|
return $this->json( [
|
||
|
'message' => 'answer ' . $voteInfo[ 'value' ] . ' is not allowed. should be yes, maybe, or no.',
|
||
|
'vote_stack' => $stack,
|
||
|
],
|
||
|
404 );
|
||
|
}
|
||
|
$vote = new Vote();
|
||
|
$foundChoice = $emChoice->find( $voteInfo[ 'choice_id' ] );
|
||
|
if ( ! $foundChoice ) {
|
||
|
return $this->json( [
|
||
|
'message' => 'choice ' . $voteInfo[ 'choice_id' ] . ' was not found',
|
||
|
'vote_stack' => $stack,
|
||
|
],
|
||
|
404 );
|
||
|
}
|
||
|
$vote->setPoll( $poll )
|
||
|
->setChoice( $foundChoice )
|
||
|
->setValue( $voteInfo[ 'value' ] );
|
||
|
$vote->setPoll( $poll );
|
||
|
$stack->addVote( $vote );
|
||
|
$poll->addVote( $vote );
|
||
|
$em->persist( $vote );
|
||
|
$em->persist( $foundChoice );
|
||
|
}
|
||
|
|
||
|
// find poll from choices
|
||
|
$poll->addStackOfVote( $stack );
|
||
|
$em->persist( $stack );
|
||
|
$em->persist( $poll );
|
||
|
$em->flush();
|
||
|
$precision = '';
|
||
|
if ( $existingOwner ) {
|
||
|
$precision = ' from an existing owner : ' . $foundOwner->getEmail();
|
||
|
}
|
||
|
$comments = [];
|
||
|
$stacks = [];
|
||
|
$choices = [];
|
||
|
foreach ( $poll->getComments() as $c ) {
|
||
|
$comments[] = $c->display();
|
||
|
}
|
||
|
foreach ( $poll->getStacksOfVotes() as $c ) {
|
||
|
$stacks[] = $c->display();
|
||
|
}
|
||
|
foreach ( $poll->getChoices() as $c ) {
|
||
|
$choices[] = $c->display();
|
||
|
}
|
||
|
|
||
|
return $this->json( [
|
||
|
'message' => 'you created a vote stack' . $precision,
|
||
|
'poll' => $poll,
|
||
|
'vote_stack' => $stack->display(),
|
||
|
'stacks' => $stacks,
|
||
|
'comments' => $comments,
|
||
|
'choices' => $choices,
|
||
|
'choices_count' => $poll->computeAnswers(),
|
||
|
'vote_count' => count( $poll->getStacksOfVotes() ),
|
||
|
'owner_modifier_token' => $foundOwner->getModifierToken(),
|
||
|
'admin_key' => $poll->getAdminKey(),
|
||
|
'json_you_sent' => $data,
|
||
|
],
|
||
|
201 );
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @Delete(
|
||
|
* path = "/poll/{id}/votes/{accessToken}",
|
||
|
* name = "poll_votes_delete",
|
||
|
* requirements = {"accessToken"="\w+", "poll_id"="\d+"}
|
||
|
* )
|
||
|
* @return JsonResponse
|
||
|
*/
|
||
|
public
|
||
|
function deletePollVotesAction(
|
||
|
Poll $poll,
|
||
|
$accessToken
|
||
|
) {
|
||
|
if ( $accessToken == $poll->getAdminKey() ) {
|
||
|
$em = $this->getDoctrine()->getManager();
|
||
|
$length = count( $poll->getVotes() );
|
||
|
$em->remove( $poll->getVotes() );
|
||
|
$em->flush();
|
||
|
|
||
|
return $this->json( [
|
||
|
'message' => 'boom! les ' . $length . ' votes du sondage ont été supprimés',
|
||
|
] );
|
||
|
} else {
|
||
|
return $this->json( [
|
||
|
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage',
|
||
|
] );
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|