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

send modifier token after submitting a vote stack

This commit is contained in:
Tykayn 2021-06-07 12:13:00 +02:00 committed by tykayn
parent 57adb29cb9
commit 0f8d981c94
3 changed files with 50 additions and 17 deletions

View File

@ -1,6 +1,6 @@
# see https://symfony.com/doc/current/reference/configuration/framework.html
framework:
trusted_hosts: ['localhost:4200', 'localhost', 'tktest.lan', '127.0.0.1', '127.0.0.1:8000', 'framadate-api.cipherbliss.com']
trusted_hosts: ['localhost:4200', 'localhost', 'tktest.lan', '127.0.0.1', '127.0.0.1:4200', '127.0.0.1:8000', 'framadate-api.cipherbliss.com']
secret: '%env(APP_SECRET)%'
#csrf_protection: true
#http_method_override: true

View File

@ -21,50 +21,50 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class DefaultController
* @package App\Controller
* @Route("/api/v1/vote",name="api_")
* @Route("/api/v1/vote-stack",name="api_")
*/
class VoteController extends EmailsController {
/**
* add a vote stack on a poll
* @Route(
* path = "/vote-stack",
* path = "/",
* name = "new_vote_stack",
* methods={"POST","OPTIONS"}
* )
*
* @param SerializerInterface $serializer
* @param string $custom_url
* @param Request $request
*
* @return JsonResponse|Response
*/
public function newVoteStackAction(
SerializerInterface $serializer,
string $custom_url,
Request $request,
ChoiceRepository $choice_repository
) {
$data = $request->getContent();
$data = json_decode( $data, true );
$poll_custom_url = $data['poll_custom_url'];
/***
* checks before persisting
*/
$em = $this->getDoctrine()->getManager();
$emPol = $em->getRepository( Poll::class );
$poll = $emPol->findOneByCustomUrl( $custom_url );
$poll = $emPol->findOneByCustomUrl( $poll_custom_url );
// check : existence of poll
if ( ! $poll ) {
return $this->json( [ 'message' => 'poll "' . $custom_url . '" not found' ], 404 );
return $this->json( [ 'message' => 'poll "' . $poll_custom_url . '" not found' ], 404 );
}
// check : limit of number of participation max
if ( count( $poll->getStacksOfVotes() ) == $poll->getVotesMax() ) {
return $this->json( [ 'message' => 'poll "' . $custom_url . '" not allowed to have more stack of votes than ' . $poll->getVotesMax() ],
return $this->json( [ 'message' => 'poll "' . $poll_custom_url . '" not allowed to have more stack of votes than ' . $poll->getVotesMax() ],
403 );
}
$data = $request->getContent();
$data = json_decode( $data, true );
// var_dump($data);
// die();
@ -125,16 +125,15 @@ class VoteController extends EmailsController {
$this->sendVoteNotificationAction( $newStack->getOwner(), $newStack );
}
return $this->json( $newStack->display() );
return $this->json( $newStack->displayForAdmin() );
}
/**
* update vote stack
* @Route(
* path = "/vote-stack/{id}/token/{modifierToken}",
* path = "/{id}/token/{modifierToken}",
* name = "update_vote_stack",
* requirements = { "id"="\d+"}
* methods={"PATCH","OPTIONS"}
* )
* @param SerializerInterface $serializer
@ -185,6 +184,34 @@ class VoteController extends EmailsController {
}
/**
* @Route(
* path = "/{id}/token/{modifierToken}",
* name = "delete_vote_stack",
* requirements = { "id"="\d+","modifierToken"="\w+"},
* methods={"DELETE"}
* )
* @param StackOfVotes $stack_of_votes
*/
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->remove( $stack_of_votes );
$em->flush();
return $this->json( [
'message' => 'boom! la stack de vote ' . $id . ' a été supprimée',
],
200 );
} else {
return $this->json( [
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier cet ensemble de réponses',
],
403 );
}
}
/**
* @Delete(
* path = "/poll/{id}/votes/{accessToken}",

View File

@ -58,18 +58,24 @@ class StackOfVotes {
}
public function display() {
$votes = $this->getVotes();
$tab = [
// 'id' => $this->getId(),
// 'modifier_token' => $this->getOwner()->getModifierToken(),
'id' => $this->getId(),
'pseudo' => $this->getPseudo(),
'created_at' => $this->getCreatedAtAsString(),
'votes' => [],
];
foreach ( $this->getVotes() as $vote ) {
$tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display();
$tab[ 'votes' ][ ] = $vote->display();
}
$tab[ 'owner' ] = $this->getOwner()->display();
return $tab;
}
public function displayForAdmin() {
$tab = $this->display();
$tab[ 'owner' ] = $this->getOwner()->displayForAdmin();
return $tab;
}