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

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:
request:
converters: true
router:
annotations: false

View File

@ -72,6 +72,10 @@ server {
include fastcgi.conf;
fastcgi_intercept_errors on;
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
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
@ -92,4 +96,5 @@ server {
location ~ \.php$ {
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\Route;
use JMS\Serializer\SerializerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@ -46,7 +47,7 @@ class VoteController extends EmailsController {
$data = $request->getContent();
$data = json_decode( $data, true );
$poll_custom_url = $data['poll_custom_url'];
$poll_custom_url = $data[ 'poll_custom_url' ];
/***
* checks before persisting
@ -125,7 +126,7 @@ class VoteController extends EmailsController {
$this->sendVoteNotificationAction( $newStack->getOwner(), $newStack );
}
return $this->json( $newStack->displayForAdmin() );
return $this->json( $newStack->displayForAdmin() );
}
@ -136,6 +137,7 @@ class VoteController extends EmailsController {
* name = "update_vote_stack",
* methods={"PATCH","OPTIONS"}
* )
*
* @param SerializerInterface $serializer
* @param StackOfVotes $id
* @param $modifierToken
@ -154,32 +156,51 @@ class VoteController extends EmailsController {
if ( ! $voteStack ) {
return $this->json( [ 'message' => 'vote stack not found' ], 404 );
}
$poll = $voteStack->getPoll();
$poll = $voteStack->getPoll();
$whocanchangeanswers = $poll->getModificationPolicy();
$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 ) {
return $this->json( [ 'message' => 'your token does not allow you to modify this vote ' ],
403 );
}
$voteStack->patchVotes( $data[ 'votes' ] );
// 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
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
// save everything
$jsonResponse = $serializer->serialize( [
'message' => 'ok',
'modifier_token' => $voteStack->getOwner()->getModifierToken(),
'vote_stack' => $voteStack,
],
'json' );
$jsonResponse = [
'message' => 'ok',
'whocanchangeanswers' => $whocanchangeanswers,
'modifier_token' => $voteStack->getOwner()->getModifierToken(),
'vote_stack' => $voteStack,
'data' => $id,
];
$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
*/
public function deleteVoteStackAction(StackOfVotes $stack_of_votes,$modifierToken){
public function deleteVoteStackAction( StackOfVotes $stack_of_votes, $modifierToken ) {
if ( $modifierToken == $stack_of_votes->getOwner()->getModifierToken() ) {
$em = $this->getDoctrine()->getManager();
$id = $stack_of_votes->getId() ;
$em = $this->getDoctrine()->getManager();
$id = $stack_of_votes->getId();
$em->remove( $stack_of_votes );
$em->flush();

View File

@ -66,7 +66,7 @@ class StackOfVotes {
'votes' => [],
];
foreach ( $this->getVotes() as $vote ) {
$tab[ 'votes' ][ ] = $vote->display();
$tab[ 'votes' ][] = $vote->display();
}
$tab[ 'owner' ] = $this->getOwner()->display();
@ -160,4 +160,30 @@ class StackOfVotes {
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]);
}
}
}
}