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:
parent
0f8d981c94
commit
28ccc52dd6
@ -1,3 +1,5 @@
|
||||
sensio_framework_extra:
|
||||
request:
|
||||
converters: true
|
||||
router:
|
||||
annotations: false
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
@ -136,6 +137,7 @@ class VoteController extends EmailsController {
|
||||
* name = "update_vote_stack",
|
||||
* methods={"PATCH","OPTIONS"}
|
||||
* )
|
||||
*
|
||||
* @param SerializerInterface $serializer
|
||||
* @param StackOfVotes $id
|
||||
* @param $modifierToken
|
||||
@ -155,31 +157,50 @@ class VoteController extends EmailsController {
|
||||
return $this->json( [ 'message' => 'vote stack not found' ], 404 );
|
||||
}
|
||||
$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 ) {
|
||||
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
|
||||
|
||||
//TODO
|
||||
// match votes and choices
|
||||
// update answers
|
||||
// save evrything
|
||||
// save everything
|
||||
|
||||
|
||||
$jsonResponse = $serializer->serialize( [
|
||||
$jsonResponse = [
|
||||
'message' => 'ok',
|
||||
'whocanchangeanswers' => $whocanchangeanswers,
|
||||
'modifier_token' => $voteStack->getOwner()->getModifierToken(),
|
||||
'vote_stack' => $voteStack,
|
||||
],
|
||||
'json' );
|
||||
'data' => $id,
|
||||
];
|
||||
|
||||
$response = new Response( $jsonResponse );
|
||||
$response->headers->set( 'Content-Type', 'application/json' );
|
||||
$response->setStatusCode( 200 );
|
||||
|
||||
return $response;
|
||||
return $this->json( $jsonResponse, 200 );
|
||||
|
||||
|
||||
}
|
||||
|
@ -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]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user