date-poll-api/src/Controller/api/VoteController.php

224 lines
5.7 KiB
PHP
Raw Normal View History

2020-01-30 11:28:24 +01:00
<?php
namespace App\Controller\api;
2020-01-30 11:28:24 +01:00
use App\Controller\EmailsController;
2020-01-30 11:28:24 +01:00
use App\Entity\Choice;
use App\Entity\Owner;
use App\Entity\Poll;
use App\Entity\StackOfVotes;
use App\Entity\Vote;
2020-01-30 11:31:23 +01:00
use FOS\RestBundle\Controller\Annotations\Delete;
2020-02-04 12:38:07 +01:00
use FOS\RestBundle\Controller\Annotations\Patch;
2020-01-30 11:31:23 +01:00
use FOS\RestBundle\Controller\Annotations\Post;
2020-01-30 11:28:24 +01:00
use FOS\RestBundle\Controller\Annotations\Route;
use JMS\Serializer\SerializerInterface;
2020-01-30 11:28:24 +01:00
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
2020-01-30 11:28:24 +01:00
/**
* Class DefaultController
* @package App\Controller
* @Route("/api/v1",name="api_")
*/
class VoteController extends EmailsController {
/**
* add a vote stack on a poll
* @Post(
* path = "/poll/{id}/answer",
* name = "new_vote_stack",
* requirements = {"content"="\w+", "poll_id"="\d+"}
* )
2021-04-27 10:22:16 +02:00
*
* @param SerializerInterface $serializer
* @param Poll $poll
* @param Request $request
*
* @return JsonResponse|Response
*/
public function newVoteStackAction(
SerializerInterface $serializer,
Poll $poll,
Request $request
2020-01-30 11:28:24 +01:00
) {
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
2021-04-27 10:41:21 +02:00
->setModifierToken( $poll->generateRandomKey() );
2020-01-30 11:28:24 +01:00
$stack = new StackOfVotes();
$stack
->setOwner( $foundOwner )
2021-04-27 12:51:21 +02:00
->setIp( $_SERVER[ 'REMOTE_ADDR' ] )
2020-01-30 11:28:24 +01:00
->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 )
2021-04-27 10:22:16 +02:00
->setChoice( $foundChoice )
->setValue( $voteInfo[ 'value' ] );
2020-01-30 11:28:24 +01:00
$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();
}
$stacks = $poll->getStacksOfVotes();
2020-04-16 18:14:59 +02:00
2021-04-27 10:22:16 +02:00
if ( $poll->getMailOnVote() ) {
$this->sendVoteNotificationAction( $stack->getOwner(), $stack );
2020-04-16 18:14:59 +02:00
}
2020-06-11 15:55:05 +02:00
$returnedVoteStack = $stack;
2021-04-27 10:22:16 +02:00
$jsonResponse = $serializer->serialize( $returnedVoteStack, 'json' );
2021-04-27 10:22:16 +02:00
$response = new Response( $jsonResponse );
$response->headers->set( 'Content-Type', 'application/json' );
$response->setStatusCode( 200 );
return $response;
2020-04-16 18:14:59 +02:00
2020-01-30 11:28:24 +01:00
}
/**
* update vote stack
* @Patch(
* path = "/vote-stack/{id}/token/{modifierToken}",
* name = "update_vote_stack",
* requirements = { "id"="\d+"}
* )
2021-04-27 10:22:16 +02:00
*
* @param SerializerInterface $serializer
* @param StackOfVotes $id
* @param $modifierToken
* @param Request $request
*
* @return JsonResponse|Response
*/
2020-02-04 12:38:07 +01:00
public
function updateVoteStackAction(
SerializerInterface $serializer,
StackOfVotes $id,
$modifierToken,
Request $request
2020-02-04 12:38:07 +01:00
) {
$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
2021-04-27 10:22:16 +02:00
$jsonResponse = $serializer->serialize( [
'message' => 'ok',
'modifier_token' => $voteStack->getOwner()->getModifierToken(),
'vote_stack' => $voteStack,
2021-04-27 10:22:16 +02:00
],
'json' );
2021-04-27 10:22:16 +02:00
$response = new Response( $jsonResponse );
$response->headers->set( 'Content-Type', 'application/json' );
$response->setStatusCode( 200 );
return $response;
2020-02-04 12:38:07 +01:00
}
2020-01-30 11:28:24 +01:00
/**
* @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',
2021-04-27 10:22:16 +02:00
],
200 );
2020-01-30 11:28:24 +01:00
} else {
return $this->json( [
2021-04-27 10:22:16 +02:00
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage',
],
403 );
2020-01-30 11:28:24 +01:00
}
}
}