2019-11-05 12:16:16 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
2019-11-28 14:16:56 +01:00
|
|
|
use App\Entity\Choice;
|
2019-11-06 12:27:46 +01:00
|
|
|
use App\Entity\Owner;
|
2019-11-05 16:31:27 +01:00
|
|
|
use App\Entity\Poll;
|
2019-11-27 21:46:35 +01:00
|
|
|
use App\Entity\StackOfVotes;
|
|
|
|
use App\Entity\Vote;
|
2019-11-28 16:48:35 +01:00
|
|
|
use DateTime;
|
|
|
|
use DateTimeZone;
|
2019-11-12 11:44:09 +01:00
|
|
|
use FOS\RestBundle\Controller\Annotations\Delete;
|
2019-11-05 12:16:16 +01:00
|
|
|
use FOS\RestBundle\Controller\Annotations\Get;
|
2019-11-05 16:31:27 +01:00
|
|
|
use FOS\RestBundle\Controller\Annotations\Post;
|
2019-11-12 11:44:09 +01:00
|
|
|
use FOS\RestBundle\Controller\Annotations\Put;
|
2019-11-05 16:31:27 +01:00
|
|
|
use FOS\RestBundle\Controller\Annotations\Route;
|
2019-11-06 12:27:46 +01:00
|
|
|
use JMS\Serializer\SerializerBuilder;
|
2019-11-05 12:16:16 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2019-11-28 16:48:35 +01:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2019-11-06 11:23:43 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2019-11-05 12:16:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class DefaultController
|
|
|
|
* @package App\Controller
|
2019-11-28 14:16:56 +01:00
|
|
|
* @Route("/api/v1",name="api_")
|
2019-11-05 12:16:16 +01:00
|
|
|
*/
|
|
|
|
class DefaultController extends AbstractController {
|
|
|
|
/**
|
|
|
|
* @Get(path ="/",
|
2019-11-05 16:31:27 +01:00
|
|
|
* name = "get_default")
|
2019-11-05 12:16:16 +01:00
|
|
|
*/
|
|
|
|
public function index() {
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'Welcome to your new controller!',
|
|
|
|
'path' => 'src/Controller/DefaultController.php',
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Get(
|
|
|
|
* path = "/my-polls",
|
2019-11-05 16:31:27 +01:00
|
|
|
* name = "get_my_polls",
|
2019-11-05 12:16:16 +01:00
|
|
|
* requirements = {"access_token"="\w+"}
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function showMyPollsAction() {
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'here are your polls',
|
2019-11-05 16:31:27 +01:00
|
|
|
'data' => new Poll(),
|
2019-11-05 12:16:16 +01:00
|
|
|
] );
|
|
|
|
}
|
2019-11-05 16:31:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Get(
|
2019-11-12 12:04:38 +01:00
|
|
|
* path = "/polls",
|
2019-11-05 16:31:27 +01:00
|
|
|
* name = "get_all_polls"
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function getAllPollsAction() {
|
|
|
|
$repository = $this->getDoctrine()->getRepository( Poll::class );
|
2019-11-05 17:31:07 +01:00
|
|
|
$data = $repository->findall();
|
2019-11-05 16:31:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'here are your polls',
|
2019-11-05 17:31:07 +01:00
|
|
|
'data' => $data,
|
2019-11-12 11:44:09 +01:00
|
|
|
],
|
|
|
|
200 );
|
2019-11-05 16:31:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Post(
|
2019-11-12 12:04:38 +01:00
|
|
|
* path = "/poll",
|
2019-11-28 14:16:56 +01:00
|
|
|
* name = "new_poll",
|
2019-11-05 16:31:27 +01:00
|
|
|
* requirements = {"creator"="\w+"}
|
|
|
|
* )
|
2019-11-06 11:23:43 +01:00
|
|
|
* @param Request $request
|
|
|
|
*
|
2019-11-28 16:48:35 +01:00
|
|
|
* @return JsonResponse
|
2019-11-05 16:31:27 +01:00
|
|
|
*/
|
2019-11-06 11:23:43 +01:00
|
|
|
public function newPollAction( Request $request ) {
|
|
|
|
|
|
|
|
$data = $request->getContent();
|
2019-11-05 16:31:27 +01:00
|
|
|
|
2019-11-06 12:27:46 +01:00
|
|
|
$serializer = SerializerBuilder::create()->build();
|
|
|
|
$newpoll = $serializer->deserialize( $data, 'App\Entity\Poll', 'json' );
|
2019-11-28 16:18:41 +01:00
|
|
|
$newpoll
|
|
|
|
->setAdminKey( $newpoll->generateAdminKey() )
|
2019-11-28 16:48:35 +01:00
|
|
|
->setCreationDate( new DateTime() )
|
|
|
|
->setModificationPolicy( 'nobody' );
|
2019-11-06 12:27:46 +01:00
|
|
|
$timeStamp = time() + ( 3600 * 24 * 90 ); // 90 days by default
|
2019-11-28 16:48:35 +01:00
|
|
|
$newpoll->setExpiracyDate( ( new DateTime() )->setTimestamp( $timeStamp ),
|
|
|
|
new DateTimeZone( 'Europe/Paris' ) );
|
2019-11-06 15:01:39 +01:00
|
|
|
$data = json_decode( $data, true );
|
2019-11-06 12:27:46 +01:00
|
|
|
$em = $this->getDoctrine()->getRepository( Owner::class );
|
2019-11-06 12:49:30 +01:00
|
|
|
$foundOwner = $em->findOneBy( [ 'email' => $data[ 'owner' ][ 'email' ] ] );
|
|
|
|
|
2019-11-28 14:16:56 +01:00
|
|
|
|
2019-11-06 12:27:46 +01:00
|
|
|
$userWasFound = false;
|
|
|
|
if ( ! $foundOwner ) {
|
|
|
|
//create a new owner
|
|
|
|
$owner = new Owner();
|
|
|
|
|
|
|
|
$owner->setPseudo( $data[ 'owner' ][ 'pseudo' ] );
|
|
|
|
$owner->setEmail( $data[ 'owner' ][ 'email' ] );
|
|
|
|
$foundOwner = $owner;
|
|
|
|
} else {
|
|
|
|
$userWasFound = true;
|
|
|
|
}
|
|
|
|
// link the owner and the poll
|
|
|
|
$newpoll->setOwner( $foundOwner );
|
|
|
|
$foundOwner->addPoll( $newpoll );
|
2019-11-28 14:16:56 +01:00
|
|
|
|
|
|
|
|
2019-11-06 12:49:30 +01:00
|
|
|
$em = $this->getDoctrine()->getManager();
|
2019-11-06 12:27:46 +01:00
|
|
|
$em->persist( $newpoll );
|
2019-11-06 12:49:30 +01:00
|
|
|
$em->persist( $foundOwner );
|
2019-11-28 14:16:56 +01:00
|
|
|
|
2019-11-28 16:48:35 +01:00
|
|
|
// setup the password, converting the raw with md5 hash
|
|
|
|
if ( $data[ 'password' ] ) {
|
|
|
|
$newpoll->setPassword( $data[ 'password' ] );
|
|
|
|
}
|
2019-11-28 14:16:56 +01:00
|
|
|
// manage choices
|
2019-11-28 16:18:41 +01:00
|
|
|
$choices = $data[ 'choices_to_create' ];
|
2019-11-28 14:16:56 +01:00
|
|
|
foreach ( $choices as $c ) {
|
|
|
|
$newChoice = new Choice();
|
2019-11-28 16:18:41 +01:00
|
|
|
$newChoice
|
|
|
|
->setPoll( $newpoll )
|
|
|
|
->setName( $c );
|
|
|
|
$em->persist( $newChoice );
|
2019-11-28 14:16:56 +01:00
|
|
|
}
|
|
|
|
$em->persist( $newpoll );
|
2019-11-05 16:31:27 +01:00
|
|
|
$em->flush();
|
2019-11-06 12:27:46 +01:00
|
|
|
$precision = '';
|
|
|
|
if ( $userWasFound ) {
|
|
|
|
$precision = 'from an existing user : ' . $foundOwner->getEmail();
|
|
|
|
}
|
2019-11-05 16:31:27 +01:00
|
|
|
|
2019-11-06 11:23:43 +01:00
|
|
|
return $this->json( [
|
2019-11-28 16:48:35 +01:00
|
|
|
'message' => 'you created a poll ' . $precision,
|
|
|
|
'data' => $newpoll,
|
|
|
|
'password_protected' => is_string( $newpoll->getPassword() ),
|
|
|
|
'admin_key' => $newpoll->getAdminKey(),
|
2019-11-06 12:27:46 +01:00
|
|
|
|
2019-11-06 11:23:43 +01:00
|
|
|
],
|
2019-11-12 11:44:09 +01:00
|
|
|
201 );
|
2019-11-05 16:31:27 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-06 14:54:04 +01:00
|
|
|
/**
|
|
|
|
* @Get(
|
|
|
|
* path = "/poll/{id}/comments",
|
|
|
|
* name = "get_poll_comment",
|
2019-11-12 12:04:38 +01:00
|
|
|
* requirements = {"poll_id"="\d+"}
|
2019-11-06 14:54:04 +01:00
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function getPollCommentsAction( Poll $poll ) {
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'here are your comments of the poll',
|
|
|
|
'data' => $poll->getComments(),
|
2019-11-12 11:44:09 +01:00
|
|
|
],
|
|
|
|
200 );
|
2019-11-06 14:54:04 +01:00
|
|
|
}
|
|
|
|
|
2019-11-05 16:31:27 +01:00
|
|
|
/**
|
|
|
|
* @Get(
|
|
|
|
* path = "/poll/{id}",
|
|
|
|
* name = "get_poll",
|
2019-11-12 12:04:38 +01:00
|
|
|
* requirements = {"poll_id"="\d+"}
|
2019-11-05 16:31:27 +01:00
|
|
|
* )
|
|
|
|
*/
|
2019-11-28 16:48:35 +01:00
|
|
|
public function getPollConfig( Poll $poll, Request $request ) {
|
|
|
|
$pass = $poll->getPassword();
|
|
|
|
$data = $request->getContent();
|
|
|
|
$data = json_decode( $data, true );
|
2019-12-01 15:36:31 +01:00
|
|
|
|
|
|
|
$comments = [];
|
|
|
|
$stacks = [];
|
|
|
|
$choices = [];
|
|
|
|
foreach ( $poll->getComments() as $c ) {
|
|
|
|
$comments[] = $c;
|
|
|
|
}
|
|
|
|
foreach ( $poll->getStacksOfVotes() as $c ) {
|
|
|
|
$stacks[] = $c;
|
|
|
|
}
|
|
|
|
foreach ( $poll->getChoices() as $c ) {
|
|
|
|
$choices[] = $c;
|
|
|
|
}
|
|
|
|
$returnedPoll = [
|
|
|
|
'message' => 'your poll config',
|
|
|
|
'data' => $poll,
|
|
|
|
'stacks_count' => count( $stacks ),
|
|
|
|
'stacks' => $stacks,
|
|
|
|
'choices_count' => count( $choices ),
|
|
|
|
'choices' => $choices,
|
|
|
|
'comments' => $comments,
|
|
|
|
];
|
2019-11-28 16:48:35 +01:00
|
|
|
/**
|
|
|
|
* password protected content
|
|
|
|
*/
|
|
|
|
if ( $pass ) {
|
|
|
|
|
|
|
|
if ( $pass == md5( $data[ 'password_input' ] ) ) {
|
2019-12-01 15:36:31 +01:00
|
|
|
return $this->json(
|
|
|
|
$returnedPoll,
|
2019-11-28 16:48:35 +01:00
|
|
|
200 );
|
|
|
|
} else {
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'your password ' . $data[ 'password_input' ] . ' is wrong, and you should feel bad',
|
|
|
|
'data' => null,
|
|
|
|
],
|
|
|
|
403 );
|
|
|
|
}
|
|
|
|
} else {
|
2019-12-01 15:36:31 +01:00
|
|
|
return $this->json(
|
|
|
|
$returnedPoll
|
|
|
|
,
|
2019-11-28 16:48:35 +01:00
|
|
|
200 );
|
|
|
|
}
|
|
|
|
|
2019-11-05 16:31:27 +01:00
|
|
|
}
|
|
|
|
|
2019-11-20 11:24:54 +01:00
|
|
|
/**
|
|
|
|
* Delete all expired polls and their children
|
|
|
|
* @Get(
|
|
|
|
* path = "/clean-polls",
|
|
|
|
* name = "clean_expired_polls",
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function cleanExpiredPolls() {
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$emPoll = $this->getDoctrine()->getRepository( Poll::class );
|
|
|
|
|
|
|
|
$queryFind = $em->createQuery(
|
|
|
|
'SELECT p
|
|
|
|
FROM App\Entity\Poll p
|
|
|
|
WHERE p.expiracyDate < CURRENT_DATE()'
|
|
|
|
);
|
|
|
|
$queryDelete = $em->createQuery(
|
|
|
|
'DELETE
|
|
|
|
FROM App\Entity\Poll p
|
|
|
|
WHERE p.expiracyDate < CURRENT_DATE()'
|
|
|
|
);
|
|
|
|
|
|
|
|
$foundPolls = $queryFind->getResult();
|
|
|
|
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'clean routine has been done, here are the numbers of polls deleted: ' . count( $foundPolls ),
|
|
|
|
'data' => [
|
|
|
|
'count' => count( $foundPolls ),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
200 );
|
|
|
|
}
|
|
|
|
|
2019-11-05 16:31:27 +01:00
|
|
|
/**
|
2019-11-12 11:44:09 +01:00
|
|
|
* @Put(
|
|
|
|
* path = "/poll/{id}",
|
|
|
|
* name = "update_poll",
|
|
|
|
* requirements = {"content"="\w+", "poll_id"="\d+"}
|
2019-11-05 16:31:27 +01:00
|
|
|
* )
|
|
|
|
*/
|
2019-11-12 11:44:09 +01:00
|
|
|
public function updatePollConfig( Poll $poll, Request $request ) {
|
|
|
|
|
2019-11-12 12:04:38 +01:00
|
|
|
// TODO check validity of request
|
|
|
|
// update only if we have the admin key
|
2019-11-12 11:44:09 +01:00
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$em->persist( $poll );
|
|
|
|
$em->flush();
|
|
|
|
|
2019-11-05 16:31:27 +01:00
|
|
|
return $this->json( [
|
2019-11-12 12:04:38 +01:00
|
|
|
'message' => 'you updated the poll ' . $poll->getTitle(),
|
2019-11-05 16:31:27 +01:00
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-12 11:52:09 +01:00
|
|
|
* add a comment on a poll
|
2019-11-05 16:31:27 +01:00
|
|
|
* @Post(
|
2019-11-28 16:48:35 +01:00
|
|
|
* path = "/poll/{id}/comment",
|
2019-11-05 16:31:27 +01:00
|
|
|
* name = "new_comment",
|
2019-11-06 15:01:39 +01:00
|
|
|
* requirements = {"content"="\w+", "poll_id"="\d+"}
|
2019-11-05 16:31:27 +01:00
|
|
|
* )
|
|
|
|
*/
|
2019-11-12 11:44:09 +01:00
|
|
|
public function newCommentAction( Poll $poll, Request $request ) {
|
|
|
|
if ( ! $poll ) {
|
|
|
|
return $this->json( [ 'message' => 'poll not found' ], 404 );
|
|
|
|
}
|
2019-11-06 15:01:39 +01:00
|
|
|
$data = $request->getContent();
|
|
|
|
|
|
|
|
$serializer = SerializerBuilder::create()->build();
|
|
|
|
$comment = $serializer->deserialize( $data, 'App\Entity\Comment', 'json' );
|
2019-11-12 11:44:09 +01:00
|
|
|
|
2019-11-12 11:52:09 +01:00
|
|
|
$em = $this->getDoctrine()->getRepository( Owner::class );
|
|
|
|
|
|
|
|
$data = json_decode( $data, true );
|
|
|
|
|
|
|
|
$foundOwner = $em->findByEmail( $data[ 'owner' ][ 'email' ] );
|
|
|
|
// manage existing or new Owner
|
|
|
|
if ( ! $foundOwner ) {
|
|
|
|
$foundOwner = new Owner();
|
|
|
|
$foundOwner->setPseudo( $data[ 'owner' ][ 'email' ] )
|
|
|
|
->setEmail( $data[ 'owner' ][ 'email' ] )
|
|
|
|
->setModifierToken( uniqid() );
|
|
|
|
}
|
2019-11-06 15:01:39 +01:00
|
|
|
$comment->setOwner( $foundOwner )
|
2019-11-12 11:44:09 +01:00
|
|
|
->setPoll( $poll );
|
2019-11-12 11:52:09 +01:00
|
|
|
$foundOwner->addComment( $comment );
|
2019-11-06 15:01:39 +01:00
|
|
|
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
2019-11-12 11:52:09 +01:00
|
|
|
$em->persist( $foundOwner );
|
2019-11-06 15:01:39 +01:00
|
|
|
$em->persist( $comment );
|
|
|
|
$em->flush();
|
|
|
|
|
2019-11-05 16:31:27 +01:00
|
|
|
return $this->json( [
|
|
|
|
'message' => 'you created a comment',
|
2019-11-12 11:44:09 +01:00
|
|
|
],
|
|
|
|
201 );
|
2019-11-05 16:31:27 +01:00
|
|
|
}
|
|
|
|
|
2019-11-12 17:26:56 +01:00
|
|
|
/**
|
|
|
|
* add a comment on a poll
|
|
|
|
* @Post(
|
2019-11-28 16:48:35 +01:00
|
|
|
* path = "/poll/{id}/vote",
|
2019-11-12 17:26:56 +01:00
|
|
|
* name = "new_vote_stack",
|
|
|
|
* requirements = {"content"="\w+", "poll_id"="\d+"}
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function newVoteStackAction( Poll $poll, Request $request ) {
|
2019-11-27 20:55:59 +01:00
|
|
|
if ( ! $poll ) {
|
|
|
|
return $this->json( [ 'message' => 'poll not found' ], 404 );
|
|
|
|
}
|
2019-11-28 11:51:25 +01:00
|
|
|
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
2019-11-27 21:46:35 +01:00
|
|
|
$data = $request->getContent();
|
|
|
|
$data = json_decode( $data, true );
|
|
|
|
|
2019-11-28 17:09:07 +01:00
|
|
|
|
|
|
|
$emOwner = $this->getDoctrine()->getRepository( Owner::class );
|
|
|
|
$existingOwner = false;
|
|
|
|
$foundOwner = $emOwner->findOneByEmail( trim( $data[ 'email' ] ) );
|
|
|
|
// manage existing or new Owner
|
|
|
|
if ( ! $foundOwner ) {
|
|
|
|
$foundOwner = new Owner();
|
|
|
|
$foundOwner
|
|
|
|
->setEmail( $data[ 'email' ] )
|
|
|
|
->setPseudo( $data[ 'pseudo' ] );
|
|
|
|
} else {
|
|
|
|
$existingOwner = true;
|
|
|
|
}
|
|
|
|
$foundOwner
|
|
|
|
->setModifierToken( $poll->generateAdminKey() );
|
2019-11-27 21:46:35 +01:00
|
|
|
$stack = new StackOfVotes();
|
2019-11-28 11:51:25 +01:00
|
|
|
$stack
|
2019-11-28 17:09:07 +01:00
|
|
|
->setOwner( $foundOwner )
|
2019-11-28 11:51:25 +01:00
|
|
|
->setPseudo( $data[ 'pseudo' ] )
|
|
|
|
->setPoll( $poll );
|
2019-11-27 21:46:35 +01:00
|
|
|
foreach ( $data[ 'votes' ] as $voteInfo ) {
|
|
|
|
$vote = new Vote();
|
|
|
|
$foundChoice = $poll->findChoiceById( $voteInfo[ 'choice_id' ] );
|
2019-11-28 17:00:17 +01:00
|
|
|
if ( ! $foundChoice ) {
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'choice ' . $voteInfo[ 'choice_id' ] . ' was not found',
|
|
|
|
'vote_stack' => $stack,
|
|
|
|
],
|
|
|
|
404 );
|
|
|
|
}
|
2019-11-27 21:46:35 +01:00
|
|
|
$vote->setPoll( $poll )
|
|
|
|
->setChoice( $foundChoice )
|
|
|
|
->setValue( $voteInfo[ 'value' ] );
|
2019-11-28 11:51:25 +01:00
|
|
|
$vote->setPoll( $poll );
|
2019-11-27 21:46:35 +01:00
|
|
|
$stack->addVote( $vote );
|
2019-11-28 11:51:25 +01:00
|
|
|
$poll->addVote( $vote );
|
|
|
|
$em->persist( $vote );
|
|
|
|
$em->persist( $foundChoice );
|
2019-11-27 21:46:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// find poll from choices
|
|
|
|
$poll->addStackOfVote( $stack );
|
|
|
|
$em->persist( $stack );
|
2019-11-28 11:51:25 +01:00
|
|
|
$em->persist( $poll );
|
2019-11-27 21:46:35 +01:00
|
|
|
$em->flush();
|
2019-11-28 17:09:07 +01:00
|
|
|
$precision = '';
|
|
|
|
if ( $existingOwner ) {
|
|
|
|
$precision = ' from an existing owner : ' . $foundOwner->getEmail();
|
|
|
|
}
|
2019-11-12 17:26:56 +01:00
|
|
|
|
|
|
|
return $this->json( [
|
2019-11-28 17:09:07 +01:00
|
|
|
'message' => 'you created a vote stack' . $precision,
|
|
|
|
'vote_stack' => $stack,
|
|
|
|
'owner_modifier_token' => $foundOwner->getModifierToken(),
|
|
|
|
'json_you_sent' => $data,
|
2019-11-12 17:26:56 +01:00
|
|
|
],
|
|
|
|
201 );
|
|
|
|
}
|
|
|
|
|
2019-11-05 17:22:30 +01:00
|
|
|
|
2019-11-12 11:44:09 +01:00
|
|
|
/**
|
|
|
|
* @Delete(
|
|
|
|
* path = "/poll/{id}",
|
|
|
|
* name = "poll_delete",
|
|
|
|
* requirements = {"accessToken"="\w+", "poll_id"="\d+"}
|
|
|
|
* )
|
|
|
|
* @param Poll $poll
|
|
|
|
* @param $accessToken
|
|
|
|
*
|
2019-11-28 16:48:35 +01:00
|
|
|
* @return JsonResponse
|
2019-11-12 11:44:09 +01:00
|
|
|
*/
|
|
|
|
public function deletePollAction( Poll $poll, $accessToken ) {
|
|
|
|
|
|
|
|
if ( $accessToken == $poll->getAdminKey() ) {
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$em->remove( $poll );
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'boom! le sondage et ses objets assocités a été supprimé',
|
|
|
|
] );
|
|
|
|
} else {
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage',
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
2019-11-05 17:22:30 +01:00
|
|
|
}
|
|
|
|
|
2019-11-12 11:44:09 +01:00
|
|
|
/**
|
|
|
|
* Erase all comments of a poll
|
|
|
|
* @Delete(
|
|
|
|
* path = "/poll/{id}/comments",
|
|
|
|
* name = "poll_comments_delete",
|
|
|
|
* requirements = {"accessToken"="\w+", "poll_id"="\d+"}
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @param Poll $poll
|
|
|
|
* @param $accessToken
|
|
|
|
*
|
2019-11-28 16:48:35 +01:00
|
|
|
* @return JsonResponse
|
2019-11-12 11:44:09 +01:00
|
|
|
*/
|
|
|
|
public function deletePollCommentsAction( Poll $poll, $accessToken ) {
|
|
|
|
if ( $accessToken == $poll->getAdminKey() ) {
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$length = count( $poll->getComments() );
|
|
|
|
$em->remove( $poll->getComments() );
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'boom! les ' . $length . ' commentaires du sondage ont été supprimés',
|
|
|
|
] );
|
|
|
|
} else {
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage',
|
|
|
|
] );
|
|
|
|
}
|
2019-11-05 17:22:30 +01:00
|
|
|
}
|
|
|
|
|
2019-11-12 11:44:09 +01:00
|
|
|
/**
|
|
|
|
* @Delete(
|
2019-11-28 16:48:35 +01:00
|
|
|
* path = "/poll/{id}/votes/{accessToken}",
|
2019-11-12 11:44:09 +01:00
|
|
|
* name = "poll_votes_delete",
|
|
|
|
* requirements = {"accessToken"="\w+", "poll_id"="\d+"}
|
|
|
|
* )
|
2019-11-28 16:48:35 +01:00
|
|
|
* @return JsonResponse
|
2019-11-12 11:44:09 +01:00
|
|
|
*/
|
|
|
|
public function deletePollVotesAction( Poll $poll, $accessToken ) {
|
|
|
|
if ( $accessToken == $poll->getAdminKey() ) {
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$length = count( $poll->getVotes() );
|
|
|
|
$em->remove( $poll->getVotes() );
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'boom! les ' . $length . ' votes du sondage ont été supprimés',
|
|
|
|
] );
|
|
|
|
} else {
|
|
|
|
return $this->json( [
|
|
|
|
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage',
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
2019-11-05 17:22:30 +01:00
|
|
|
}
|
|
|
|
|
2019-11-05 12:16:16 +01:00
|
|
|
}
|