From 28ccc52dd62df67303bca41f681289aecc98f494 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Tue, 8 Jun 2021 10:22:58 +0200 Subject: [PATCH] update votes action --- config/packages/sensio_framework_extra.yaml | 2 + doc/nginx/framadate-api.conf | 5 ++ src/Controller/api/v1/VoteController.php | 65 ++++++++++++++------- src/Entity/StackOfVotes.php | 28 ++++++++- 4 files changed, 77 insertions(+), 23 deletions(-) diff --git a/config/packages/sensio_framework_extra.yaml b/config/packages/sensio_framework_extra.yaml index 1821ccc..56cfef0 100644 --- a/config/packages/sensio_framework_extra.yaml +++ b/config/packages/sensio_framework_extra.yaml @@ -1,3 +1,5 @@ sensio_framework_extra: + request: + converters: true router: annotations: false diff --git a/doc/nginx/framadate-api.conf b/doc/nginx/framadate-api.conf index 1e8b1eb..4060cf5 100755 --- a/doc/nginx/framadate-api.conf +++ b/doc/nginx/framadate-api.conf @@ -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; } + } diff --git a/src/Controller/api/v1/VoteController.php b/src/Controller/api/v1/VoteController.php index 21b80ef..fda3510 100644 --- a/src/Controller/api/v1/VoteController.php +++ b/src/Controller/api/v1/VoteController.php @@ -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(); diff --git a/src/Entity/StackOfVotes.php b/src/Entity/StackOfVotes.php index 5783b75..4671d13 100755 --- a/src/Entity/StackOfVotes.php +++ b/src/Entity/StackOfVotes.php @@ -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]); + } + + } + + } }