Compare commits

...

2 Commits

Author SHA1 Message Date
Tykayn 0f8d981c94 send modifier token after submitting a vote stack 2021-06-07 12:13:00 +02:00
Tykayn 57adb29cb9 update vote stack methods and routes 2021-06-07 09:32:44 +02:00
3 changed files with 52 additions and 23 deletions

View File

@ -1,6 +1,6 @@
# see https://symfony.com/doc/current/reference/configuration/framework.html # see https://symfony.com/doc/current/reference/configuration/framework.html
framework: 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)%' secret: '%env(APP_SECRET)%'
#csrf_protection: true #csrf_protection: true
#http_method_override: true #http_method_override: true

View File

@ -21,50 +21,50 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/** /**
* Class DefaultController * Class DefaultController
* @package App\Controller * @package App\Controller
* @Route("/api/v1/vote",name="api_") * @Route("/api/v1/vote-stack",name="api_")
*/ */
class VoteController extends EmailsController { class VoteController extends EmailsController {
/** /**
* add a vote stack on a poll * add a vote stack on a poll
* @Route( * @Route(
* path = "/poll/{custom_url}/answer", * path = "/",
* name = "new_vote_stack", * name = "new_vote_stack",
* methods={"POST","OPTIONS"} * methods={"POST","OPTIONS"}
* ) * )
* *
* @param SerializerInterface $serializer * @param SerializerInterface $serializer
* @param string $custom_url
* @param Request $request * @param Request $request
* *
* @return JsonResponse|Response * @return JsonResponse|Response
*/ */
public function newVoteStackAction( public function newVoteStackAction(
SerializerInterface $serializer, SerializerInterface $serializer,
string $custom_url,
Request $request, Request $request,
ChoiceRepository $choice_repository ChoiceRepository $choice_repository
) { ) {
$data = $request->getContent();
$data = json_decode( $data, true );
$poll_custom_url = $data['poll_custom_url'];
/*** /***
* checks before persisting * checks before persisting
*/ */
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$emPol = $em->getRepository( Poll::class ); $emPol = $em->getRepository( Poll::class );
$poll = $emPol->findOneByCustomUrl( $custom_url ); $poll = $emPol->findOneByCustomUrl( $poll_custom_url );
// check : existence of poll // check : existence of poll
if ( ! $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 // check : limit of number of participation max
if ( count( $poll->getStacksOfVotes() ) == $poll->getVotesMax() ) { 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 ); 403 );
} }
$data = $request->getContent();
$data = json_decode( $data, true );
// var_dump($data); // var_dump($data);
// die(); // die();
@ -83,7 +83,6 @@ class VoteController extends EmailsController {
->setPseudo( $data[ 'owner' ][ "email" ] ) ->setPseudo( $data[ 'owner' ][ "email" ] )
->addStackOfVote( $newStack ); ->addStackOfVote( $newStack );
// TODO manage new comment
$emChoice = $choice_repository; $emChoice = $choice_repository;
$newComment = new Comment(); $newComment = new Comment();
$newComment->setPseudo( $data [ 'pseudo' ] ) $newComment->setPseudo( $data [ 'pseudo' ] )
@ -126,21 +125,17 @@ class VoteController extends EmailsController {
$this->sendVoteNotificationAction( $newStack->getOwner(), $newStack ); $this->sendVoteNotificationAction( $newStack->getOwner(), $newStack );
} }
return $this->json( [ return $this->json( $newStack->displayForAdmin() );
'poll' => $poll->display(),
] );
} }
/** /**
* update vote stack * update vote stack
* @Patch( * @Route(
* path = "/vote-stack/{id}/token/{modifierToken}", * path = "/{id}/token/{modifierToken}",
* name = "update_vote_stack", * name = "update_vote_stack",
* requirements = { "id"="\d+"} * methods={"PATCH","OPTIONS"}
* ) * )
*
* @param SerializerInterface $serializer * @param SerializerInterface $serializer
* @param StackOfVotes $id * @param StackOfVotes $id
* @param $modifierToken * @param $modifierToken
@ -189,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( * @Delete(
* path = "/poll/{id}/votes/{accessToken}", * path = "/poll/{id}/votes/{accessToken}",

View File

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