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:
|
sensio_framework_extra:
|
||||||
|
request:
|
||||||
|
converters: true
|
||||||
router:
|
router:
|
||||||
annotations: false
|
annotations: false
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
@ -125,7 +126,7 @@ class VoteController extends EmailsController {
|
|||||||
$this->sendVoteNotificationAction( $newStack->getOwner(), $newStack );
|
$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",
|
* 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
|
||||||
@ -154,32 +156,51 @@ class VoteController extends EmailsController {
|
|||||||
if ( ! $voteStack ) {
|
if ( ! $voteStack ) {
|
||||||
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();
|
||||||
|
|
||||||
|
$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 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
|
//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',
|
||||||
'modifier_token' => $voteStack->getOwner()->getModifierToken(),
|
'whocanchangeanswers' => $whocanchangeanswers,
|
||||||
'vote_stack' => $voteStack,
|
'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 );
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -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();
|
||||||
|
|
||||||
|
@ -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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user