update votes action

This commit is contained in:
Tykayn 2021-06-08 10:22:58 +02:00 committed by tykayn
parent 0f8d981c94
commit 28ccc52dd6
4 changed files with 77 additions and 23 deletions

View File

@ -1,3 +1,5 @@
sensio_framework_extra: sensio_framework_extra:
request:
converters: true
router: router:
annotations: false annotations: false

View File

@ -72,6 +72,10 @@ server {
include fastcgi.conf; include fastcgi.conf;
fastcgi_intercept_errors on; fastcgi_intercept_errors on;
fastcgi_pass php-handler; fastcgi_pass php-handler;
proxy_busy_buffers_size 512k;
proxy_buffers 4 512k;
proxy_buffer_size 256k;
# When you are using symlinks to link the document root to the # When you are using symlinks to link the document root to the
# current version of your application, you should pass the real # current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP # application path instead of the path to the symlink to PHP
@ -92,4 +96,5 @@ server {
location ~ \.php$ { location ~ \.php$ {
return 404; return 404;
} }
} }

View File

@ -13,6 +13,7 @@ use FOS\RestBundle\Controller\Annotations\Delete;
use FOS\RestBundle\Controller\Annotations\Patch; use FOS\RestBundle\Controller\Annotations\Patch;
use FOS\RestBundle\Controller\Annotations\Route; use FOS\RestBundle\Controller\Annotations\Route;
use JMS\Serializer\SerializerInterface; use JMS\Serializer\SerializerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@ -46,7 +47,7 @@ class VoteController extends EmailsController {
$data = $request->getContent(); $data = $request->getContent();
$data = json_decode( $data, true ); $data = json_decode( $data, true );
$poll_custom_url = $data['poll_custom_url']; $poll_custom_url = $data[ 'poll_custom_url' ];
/*** /***
* checks before persisting * checks before persisting
@ -136,6 +137,7 @@ class VoteController extends EmailsController {
* name = "update_vote_stack", * name = "update_vote_stack",
* methods={"PATCH","OPTIONS"} * methods={"PATCH","OPTIONS"}
* ) * )
*
* @param SerializerInterface $serializer * @param SerializerInterface $serializer
* @param StackOfVotes $id * @param StackOfVotes $id
* @param $modifierToken * @param $modifierToken
@ -155,31 +157,50 @@ class VoteController extends EmailsController {
return $this->json( [ 'message' => 'vote stack not found' ], 404 ); return $this->json( [ 'message' => 'vote stack not found' ], 404 );
} }
$poll = $voteStack->getPoll(); $poll = $voteStack->getPoll();
$whocanchangeanswers = $poll->getModificationPolicy();
// if only self users are allowed to modify a vote, check it $data = $request->getContent();
$data = json_decode( $data, true );
if ( $whocanchangeanswers == 'everybody' ) {
$voteStack->patchVotes( $data[ 'votes' ] );
} else if ( $whocanchangeanswers == 'self' ) {
// someone with the right token of this vote stack only can change this
if ( ! $modifierToken || $voteStack->getOwner()->getModifierToken() !== $modifierToken ) { if ( ! $modifierToken || $voteStack->getOwner()->getModifierToken() !== $modifierToken ) {
return $this->json( [ 'message' => 'your token does not allow you to modify this vote ' ], return $this->json( [ 'message' => 'your token does not allow you to modify this vote ' ],
403 ); 403 );
} }
$voteStack->patchVotes( $data[ 'votes' ] );
// everything is ok, we can update all the votes of the vote stack // everything is ok, we can update all the votes of the vote stack
} else if ( $whocanchangeanswers == 'nobody' ) {
// only the poll admin with the poll modifier token can change this
if ( ! $modifierToken || $poll->getOwner()->getModifierToken() !== $modifierToken ) {
return $this->json( [ 'message' => 'your token does not allow you to modify this vote ' ],
403 );
}
$voteStack->patchVotes( $data[ 'votes' ] );
}
// if only self users are allowed to modify a vote, check it
//TODO //TODO
// match votes and choices // match votes and choices
// update answers // update answers
// save evrything // save everything
$jsonResponse = $serializer->serialize( [ $jsonResponse = [
'message' => 'ok', 'message' => 'ok',
'whocanchangeanswers' => $whocanchangeanswers,
'modifier_token' => $voteStack->getOwner()->getModifierToken(), 'modifier_token' => $voteStack->getOwner()->getModifierToken(),
'vote_stack' => $voteStack, 'vote_stack' => $voteStack,
], 'data' => $id,
'json' ); ];
$response = new Response( $jsonResponse );
$response->headers->set( 'Content-Type', 'application/json' );
$response->setStatusCode( 200 );
return $response; return $this->json( $jsonResponse, 200 );
} }
@ -193,10 +214,10 @@ class VoteController extends EmailsController {
* ) * )
* @param StackOfVotes $stack_of_votes * @param StackOfVotes $stack_of_votes
*/ */
public function deleteVoteStackAction(StackOfVotes $stack_of_votes,$modifierToken){ public function deleteVoteStackAction( StackOfVotes $stack_of_votes, $modifierToken ) {
if ( $modifierToken == $stack_of_votes->getOwner()->getModifierToken() ) { if ( $modifierToken == $stack_of_votes->getOwner()->getModifierToken() ) {
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$id = $stack_of_votes->getId() ; $id = $stack_of_votes->getId();
$em->remove( $stack_of_votes ); $em->remove( $stack_of_votes );
$em->flush(); $em->flush();

View File

@ -66,7 +66,7 @@ class StackOfVotes {
'votes' => [], 'votes' => [],
]; ];
foreach ( $this->getVotes() as $vote ) { foreach ( $this->getVotes() as $vote ) {
$tab[ 'votes' ][ ] = $vote->display(); $tab[ 'votes' ][] = $vote->display();
} }
$tab[ 'owner' ] = $this->getOwner()->display(); $tab[ 'owner' ] = $this->getOwner()->display();
@ -160,4 +160,30 @@ class StackOfVotes {
return $this; return $this;
} }
/**
* only update the values of votes
*
* @param $votes
*/
public function patchVotes( $votes ) {
$table_votes_by_id = [];
$stackVotes = $this->getVotes();
foreach ( $stackVotes as $stack_vote ) {
$table_votes_by_id[ $stack_vote->getId() ] = $stack_vote;
}
foreach ( $votes as $vote ) {
$newValue = $vote[ 'value' ];
$id = $vote[ 'id' ];
// if the new value is null, remove the vote
if ( $newValue && isset( $table_votes_by_id[ $id ] ) ) {
$table_votes_by_id[ $id ]->setValue( $newValue );
// $this->addVote($table_votes_by_id[$id]);
}
}
}
} }