mirror of
https://framagit.org/tykayn/date-poll-api
synced 2023-08-25 08:23:11 +02:00
Compare commits
4 Commits
9781c2be2c
...
1bc69f56ad
Author | SHA1 | Date | |
---|---|---|---|
1bc69f56ad | |||
272588d000 | |||
d63929eb93 | |||
2733c1f2a3 |
143
composer.lock
generated
143
composer.lock
generated
@ -59,24 +59,6 @@
|
||||
}
|
||||
],
|
||||
"description": "Composer plugin that provides efficient querying for installed package versions (no runtime IO)",
|
||||
"support": {
|
||||
"issues": "https://github.com/composer/package-versions-deprecated/issues",
|
||||
"source": "https://github.com/composer/package-versions-deprecated/tree/1.11.99.1"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://packagist.com",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://github.com/composer",
|
||||
"type": "github"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/composer/composer",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2020-11-11T10:22:58+00:00"
|
||||
},
|
||||
{
|
||||
@ -229,24 +211,6 @@
|
||||
"redis",
|
||||
"xcache"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/doctrine/cache/issues",
|
||||
"source": "https://github.com/doctrine/cache/tree/1.10.x"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://www.doctrine-project.org/sponsorship.html",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://www.patreon.com/phpdoctrine",
|
||||
"type": "patreon"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/doctrine%2Fcache",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2020-07-07T18:54:01+00:00"
|
||||
},
|
||||
{
|
||||
@ -312,10 +276,6 @@
|
||||
"iterators",
|
||||
"php"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/doctrine/collections/issues",
|
||||
"source": "https://github.com/doctrine/collections/tree/1.6.7"
|
||||
},
|
||||
"time": "2020-07-27T17:53:49+00:00"
|
||||
},
|
||||
{
|
||||
@ -825,24 +785,6 @@
|
||||
"event system",
|
||||
"events"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/doctrine/event-manager/issues",
|
||||
"source": "https://github.com/doctrine/event-manager/tree/1.1.x"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://www.doctrine-project.org/sponsorship.html",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://www.patreon.com/phpdoctrine",
|
||||
"type": "patreon"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/doctrine%2Fevent-manager",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2020-05-29T18:28:51+00:00"
|
||||
},
|
||||
{
|
||||
@ -989,24 +931,6 @@
|
||||
"constructor",
|
||||
"instantiate"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/doctrine/instantiator/issues",
|
||||
"source": "https://github.com/doctrine/instantiator/tree/1.4.0"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://www.doctrine-project.org/sponsorship.html",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://www.patreon.com/phpdoctrine",
|
||||
"type": "patreon"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/doctrine%2Finstantiator",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2020-11-10T18:47:58+00:00"
|
||||
},
|
||||
{
|
||||
@ -1069,24 +993,6 @@
|
||||
"parser",
|
||||
"php"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/doctrine/lexer/issues",
|
||||
"source": "https://github.com/doctrine/lexer/tree/1.2.1"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://www.doctrine-project.org/sponsorship.html",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://www.patreon.com/phpdoctrine",
|
||||
"type": "patreon"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/doctrine%2Flexer",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2020-05-25T17:44:05+00:00"
|
||||
},
|
||||
{
|
||||
@ -1418,10 +1324,6 @@
|
||||
"highlight",
|
||||
"sql"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/doctrine/sql-formatter/issues",
|
||||
"source": "https://github.com/doctrine/sql-formatter/tree/1.1.x"
|
||||
},
|
||||
"time": "2020-07-30T16:57:33+00:00"
|
||||
},
|
||||
{
|
||||
@ -2424,9 +2326,6 @@
|
||||
"psr",
|
||||
"psr-3"
|
||||
],
|
||||
"support": {
|
||||
"source": "https://github.com/php-fig/log/tree/1.1.3"
|
||||
},
|
||||
"time": "2020-03-23T09:12:05+00:00"
|
||||
},
|
||||
{
|
||||
@ -2767,7 +2666,6 @@
|
||||
"suggest": {
|
||||
"symfony/cache-implementation": ""
|
||||
},
|
||||
"default-branch": true,
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
@ -3136,23 +3034,6 @@
|
||||
],
|
||||
"description": "A generic function and convention to trigger deprecation notices",
|
||||
"homepage": "https://symfony.com",
|
||||
"support": {
|
||||
"source": "https://github.com/symfony/deprecation-contracts/tree/master"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://symfony.com/sponsor",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://github.com/fabpot",
|
||||
"type": "github"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2020-09-07T11:33:47+00:00"
|
||||
},
|
||||
{
|
||||
@ -6371,10 +6252,6 @@
|
||||
}
|
||||
],
|
||||
"description": "JSONP callback validator.",
|
||||
"support": {
|
||||
"issues": "https://github.com/willdurand/JsonpCallbackValidator/issues",
|
||||
"source": "https://github.com/willdurand/JsonpCallbackValidator/tree/master"
|
||||
},
|
||||
"time": "2014-01-20T22:35:06+00:00"
|
||||
},
|
||||
{
|
||||
@ -6573,24 +6450,6 @@
|
||||
"Fixture",
|
||||
"persistence"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/doctrine/DoctrineFixturesBundle/issues",
|
||||
"source": "https://github.com/doctrine/DoctrineFixturesBundle/tree/3.4.0"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://www.doctrine-project.org/sponsorship.html",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://www.patreon.com/phpdoctrine",
|
||||
"type": "patreon"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/doctrine%2Fdoctrine-fixtures-bundle",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2020-11-14T09:36:49+00:00"
|
||||
},
|
||||
{
|
||||
@ -6749,5 +6608,5 @@
|
||||
"ext-iconv": "*"
|
||||
},
|
||||
"platform-dev": [],
|
||||
"plugin-api-version": "2.0.0"
|
||||
"plugin-api-version": "1.1.0"
|
||||
}
|
||||
|
@ -1,10 +1,24 @@
|
||||
nelmio_cors:
|
||||
defaults:
|
||||
origin_regex: true
|
||||
allow_origin: ['%env(CORS_ALLOW_ORIGIN)%']
|
||||
allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
|
||||
allow_headers: ['Content-Type', 'Authorization']
|
||||
expose_headers: ['Link']
|
||||
max_age: 3600
|
||||
allow_credentials: false
|
||||
allow_origin: []
|
||||
allow_headers: []
|
||||
allow_methods: []
|
||||
expose_headers: []
|
||||
max_age: 0
|
||||
hosts: []
|
||||
origin_regex: false
|
||||
forced_allow_origin_value: ~
|
||||
paths:
|
||||
'^/': null
|
||||
'^/api/':
|
||||
allow_origin: ['*']
|
||||
allow_headers: ['*']
|
||||
allow_methods: ['POST', 'PUT', 'GET', 'DELETE', 'OPTIONS']
|
||||
max_age: 3600
|
||||
'^/':
|
||||
origin_regex: true
|
||||
allow_origin: ['^http://localhost:[0-9]+']
|
||||
allow_headers: ['*']
|
||||
allow_methods: ['POST', 'PUT', 'GET', 'DELETE', 'OPTIONS']
|
||||
max_age: 3600
|
||||
hosts: ['^api\.']
|
||||
|
@ -1,15 +0,0 @@
|
||||
nelmio_cors:
|
||||
defaults:
|
||||
origin_regex: true
|
||||
allow_origin: ['%env(CORS_ALLOW_ORIGIN)%']
|
||||
allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
|
||||
allow_headers: ['Content-Type', 'Authorization']
|
||||
expose_headers: ['Link']
|
||||
max_age: 3600
|
||||
paths:
|
||||
'^/': null
|
||||
'^/api/':
|
||||
allow_origin: ['*']
|
||||
allow_headers: ['X-Custom-Auth']
|
||||
allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
|
||||
max_age: 3600
|
@ -6,8 +6,8 @@
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
header( 'Access-Control-Allow-Origin: *' );
|
||||
header( "Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method" );
|
||||
header( "Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE" );
|
||||
// header( "Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method" );
|
||||
// header( "Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE" );
|
||||
header( "Allow: GET, POST, OPTIONS, PUT, DELETE" );
|
||||
$method = $_SERVER[ 'REQUEST_METHOD' ];
|
||||
if ( $method == "OPTIONS" ) {
|
||||
|
@ -1,223 +1,269 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\api;
|
||||
namespace App\Controller\api;
|
||||
|
||||
use App\Controller\EmailsController;
|
||||
use App\Entity\Choice;
|
||||
use App\Entity\Owner;
|
||||
use App\Entity\Poll;
|
||||
use App\Entity\StackOfVotes;
|
||||
use App\Entity\Vote;
|
||||
use FOS\RestBundle\Controller\Annotations\Delete;
|
||||
use FOS\RestBundle\Controller\Annotations\Patch;
|
||||
use FOS\RestBundle\Controller\Annotations\Post;
|
||||
use FOS\RestBundle\Controller\Annotations\Route;
|
||||
use JMS\Serializer\SerializerInterface;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Class DefaultController
|
||||
* @package App\Controller
|
||||
* @Route("/api/v1",name="api_")
|
||||
*/
|
||||
class VoteController extends EmailsController {
|
||||
use App\Controller\EmailsController;
|
||||
use App\Entity\Choice;
|
||||
use App\Entity\Owner;
|
||||
use App\Entity\Poll;
|
||||
use App\Entity\StackOfVotes;
|
||||
use App\Entity\Vote;
|
||||
use App\Repository\ChoiceRepository;
|
||||
use FOS\RestBundle\Controller\Annotations\Delete;
|
||||
use FOS\RestBundle\Controller\Annotations\Patch;
|
||||
use FOS\RestBundle\Controller\Annotations\Post;
|
||||
use FOS\RestBundle\Controller\Annotations\Route;
|
||||
use JMS\Serializer\SerializerInterface;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
* add a vote stack on a poll
|
||||
* @Post(
|
||||
* path = "/poll/{id}/answer",
|
||||
* name = "new_vote_stack",
|
||||
* requirements = {"content"="\w+", "poll_id"="\d+"}
|
||||
* )
|
||||
*
|
||||
* @param SerializerInterface $serializer
|
||||
* @param Poll $poll
|
||||
* @param Request $request
|
||||
*
|
||||
* @return JsonResponse|Response
|
||||
* Class DefaultController
|
||||
* @package App\Controller
|
||||
* @Route("/api/v1",name="api_")
|
||||
*/
|
||||
public function newVoteStackAction(
|
||||
SerializerInterface $serializer,
|
||||
Poll $poll,
|
||||
Request $request
|
||||
) {
|
||||
if ( ! $poll ) {
|
||||
return $this->json( [ 'message' => 'poll not found' ], 404 );
|
||||
}
|
||||
class VoteController extends EmailsController {
|
||||
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$data = $request->getContent();
|
||||
$data = json_decode( $data, true );
|
||||
/**
|
||||
* add a vote stack on a poll
|
||||
* @Post(
|
||||
* path = "/poll/{custom_url}/answer",
|
||||
* name = "new_vote_stack",
|
||||
* )
|
||||
*
|
||||
* @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,
|
||||
) {
|
||||
|
||||
|
||||
$emOwner = $this->getDoctrine()->getRepository( Owner::class );
|
||||
$emChoice = $this->getDoctrine()->getRepository( Choice::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;
|
||||
}
|
||||
// TODO anti flood
|
||||
$foundOwner
|
||||
->setModifierToken( $poll->generateRandomKey() );
|
||||
$stack = new StackOfVotes();
|
||||
$stack
|
||||
->setOwner( $foundOwner )
|
||||
->setIp( $_SERVER[ 'REMOTE_ADDR' ] )
|
||||
->setPseudo( $data[ 'pseudo' ] )
|
||||
->setPoll( $poll );
|
||||
foreach ( $data[ 'votes' ] as $voteInfo ) {
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$emPol = $em->getRepository( Poll::class );
|
||||
$poll = $emPol->findOneByCustomUrl( $custom_url );
|
||||
|
||||
if ( ! isset( $voteInfo[ 'value' ] ) ) {
|
||||
continue;
|
||||
if ( ! $poll ) {
|
||||
return $this->json( [ 'message' => 'poll "' . $custom_url . '" not found' ], 404 );
|
||||
}
|
||||
$allowedValuesToAnswer = [ 'yes', 'maybe', 'no' ];
|
||||
|
||||
if ( ! in_array( $voteInfo[ 'value' ], $allowedValuesToAnswer ) ) {
|
||||
return $this->json( [
|
||||
'message' => 'answer ' . $voteInfo[ 'value' ] . ' is not allowed. should be yes, maybe, or no.',
|
||||
'vote_stack' => $stack,
|
||||
],
|
||||
404 );
|
||||
$data = $request->getContent();
|
||||
$data = json_decode( $data, true );
|
||||
// $data = $data['data'];
|
||||
|
||||
$owner = new Owner();
|
||||
$owner->addPoll($poll);
|
||||
$newStack = new StackOfVotes();
|
||||
$newStack
|
||||
->setPseudo( $data[ 'pseudo' ] )
|
||||
->setOwner($owner );
|
||||
|
||||
// TODO manage new comment
|
||||
$emChoice = $choice_repository;
|
||||
|
||||
foreach ( $data[ 'votes' ] as $vote ) {
|
||||
$newVote = new Vote();
|
||||
$newVote->setPoll($poll);
|
||||
|
||||
$newStack->addVote( $newVote );
|
||||
$choiceFound = $emChoice->find( $vote[ 'choice_id' ] );
|
||||
if ( $choiceFound ) {
|
||||
|
||||
$choiceFound->addVote( $newVote );
|
||||
$newVote->setStacksOfVotes( $newStack )
|
||||
->setChoice( $choiceFound )
|
||||
->setValue( $vote[ 'value' ] );
|
||||
$em->persist( $choiceFound );
|
||||
} else {
|
||||
throw new NotFoundHttpException( 'no choice of id' . $vote[ 'choice_id' ] );
|
||||
}
|
||||
|
||||
$poll->addVote($newVote);
|
||||
$em->persist( $newVote );
|
||||
}
|
||||
$vote = new Vote();
|
||||
$foundChoice = $emChoice->find( $voteInfo[ 'choice_id' ] );
|
||||
if ( ! $foundChoice ) {
|
||||
return $this->json( [
|
||||
'message' => 'choice ' . $voteInfo[ 'choice_id' ] . ' was not found',
|
||||
'vote_stack' => $stack,
|
||||
],
|
||||
404 );
|
||||
}
|
||||
$vote->setPoll( $poll )
|
||||
->setChoice( $foundChoice )
|
||||
->setValue( $voteInfo[ 'value' ] );
|
||||
$vote->setPoll( $poll );
|
||||
$stack->addVote( $vote );
|
||||
$poll->addVote( $vote );
|
||||
$em->persist( $vote );
|
||||
$em->persist( $foundChoice );
|
||||
}
|
||||
|
||||
// find poll from choices
|
||||
$poll->addStackOfVote( $stack );
|
||||
$em->persist( $stack );
|
||||
$em->persist( $poll );
|
||||
$em->flush();
|
||||
$precision = '';
|
||||
if ( $existingOwner ) {
|
||||
$precision = ' from an existing owner : ' . $foundOwner->getEmail();
|
||||
}
|
||||
$stacks = $poll->getStacksOfVotes();
|
||||
|
||||
if ( $poll->getMailOnVote() ) {
|
||||
$this->sendVoteNotificationAction( $stack->getOwner(), $stack );
|
||||
}
|
||||
|
||||
$returnedVoteStack = $stack;
|
||||
|
||||
$jsonResponse = $serializer->serialize( $returnedVoteStack, 'json' );
|
||||
|
||||
$response = new Response( $jsonResponse );
|
||||
$response->headers->set( 'Content-Type', 'application/json' );
|
||||
$response->setStatusCode( 200 );
|
||||
|
||||
return $response;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* update vote stack
|
||||
* @Patch(
|
||||
* path = "/vote-stack/{id}/token/{modifierToken}",
|
||||
* name = "update_vote_stack",
|
||||
* requirements = { "id"="\d+"}
|
||||
* )
|
||||
*
|
||||
* @param SerializerInterface $serializer
|
||||
* @param StackOfVotes $id
|
||||
* @param $modifierToken
|
||||
* @param Request $request
|
||||
*
|
||||
* @return JsonResponse|Response
|
||||
*/
|
||||
public
|
||||
function updateVoteStackAction(
|
||||
SerializerInterface $serializer,
|
||||
StackOfVotes $id,
|
||||
$modifierToken,
|
||||
Request $request
|
||||
) {
|
||||
$voteStack = $id;
|
||||
if ( ! $voteStack ) {
|
||||
return $this->json( [ 'message' => 'vote stack not found' ], 404 );
|
||||
}
|
||||
$poll = $voteStack->getPoll();
|
||||
|
||||
// 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
|
||||
// match votes and choices
|
||||
// update answers
|
||||
// save evrything
|
||||
|
||||
|
||||
$jsonResponse = $serializer->serialize( [
|
||||
'message' => 'ok',
|
||||
'modifier_token' => $voteStack->getOwner()->getModifierToken(),
|
||||
'vote_stack' => $voteStack,
|
||||
],
|
||||
'json' );
|
||||
|
||||
$response = new Response( $jsonResponse );
|
||||
$response->headers->set( 'Content-Type', 'application/json' );
|
||||
$response->setStatusCode( 200 );
|
||||
|
||||
return $response;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @Delete(
|
||||
* path = "/poll/{id}/votes/{accessToken}",
|
||||
* name = "poll_votes_delete",
|
||||
* requirements = {"accessToken"="\w+", "poll_id"="\d+"}
|
||||
* )
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public
|
||||
function deletePollVotesAction(
|
||||
Poll $poll,
|
||||
$accessToken
|
||||
) {
|
||||
if ( $accessToken == $poll->getAdminKey() ) {
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$length = count( $poll->getVotes() );
|
||||
$em->remove( $poll->getVotes() );
|
||||
$newStack
|
||||
->setPoll( $poll );
|
||||
$em->persist( $newStack );
|
||||
$em->persist( $poll );
|
||||
$em->flush();
|
||||
|
||||
|
||||
return $this->json( [
|
||||
'message' => 'boom! les ' . $length . ' votes du sondage ont été supprimés',
|
||||
'data' => $data,
|
||||
] );
|
||||
// $emOwner = $em->getRepository( Owner::class );
|
||||
// $emChoice = $em->getRepository( Choice::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;
|
||||
// }
|
||||
// TODO anti flood
|
||||
// $foundOwner
|
||||
// ->setModifierToken( $poll->generateRandomKey() );
|
||||
// $stack = new StackOfVotes();
|
||||
// $stack
|
||||
// ->setOwner( $foundOwner )
|
||||
// ->setIp( $_SERVER[ 'REMOTE_ADDR' ] )
|
||||
// ->setPseudo( $data[ 'pseudo' ] )
|
||||
// ->setPoll( $custom_url );
|
||||
// foreach ( $data[ 'votes' ] as $voteInfo ) {
|
||||
//
|
||||
// if ( ! isset( $voteInfo[ 'value' ] ) ) {
|
||||
// continue;
|
||||
// }
|
||||
// $allowedValuesToAnswer = [ 'yes', 'maybe', 'no' ];
|
||||
//
|
||||
// if ( ! in_array( $voteInfo[ 'value' ], $allowedValuesToAnswer ) ) {
|
||||
// return $this->json( [
|
||||
// 'message' => 'answer ' . $voteInfo[ 'value' ] . ' is not allowed. should be yes, maybe, or no.',
|
||||
// 'vote_stack' => $stack,
|
||||
// ],
|
||||
// 404 );
|
||||
// }
|
||||
// $vote = new Vote();
|
||||
// $foundChoice = $emChoice->find( $voteInfo[ 'choice_id' ] );
|
||||
// if ( ! $foundChoice ) {
|
||||
// return $this->json( [
|
||||
// 'message' => 'choice ' . $voteInfo[ 'choice_id' ] . ' was not found',
|
||||
// 'vote_stack' => $stack,
|
||||
// ],
|
||||
// 404 );
|
||||
// }
|
||||
// $vote->setPoll( $poll )
|
||||
// ->setChoice( $foundChoice )
|
||||
// ->setValue( $voteInfo[ 'value' ] );
|
||||
// $vote->setPoll( $poll );
|
||||
// $stack->addVote( $vote );
|
||||
// $poll->addVote( $vote );
|
||||
// $em->persist( $vote );
|
||||
// $em->persist( $foundChoice );
|
||||
// }
|
||||
//
|
||||
// // find poll from choices
|
||||
// $poll->addStackOfVote( $stack );
|
||||
// $em->persist( $stack );
|
||||
// $em->persist( $poll );
|
||||
// $em->flush();
|
||||
// $precision = '';
|
||||
// if ( $existingOwner ) {
|
||||
// $precision = ' from an existing owner : ' . $foundOwner->getEmail();
|
||||
// }
|
||||
//
|
||||
// if ( $poll->getMailOnVote() ) {
|
||||
// $this->sendVoteNotificationAction( $stack->getOwner(), $stack );
|
||||
// }
|
||||
//
|
||||
// $returnedVoteStack = $stack;
|
||||
//
|
||||
// $jsonResponse = $serializer->serialize( $returnedVoteStack, 'json' );
|
||||
//
|
||||
// $response = new Response( $jsonResponse );
|
||||
// $response->headers->set( 'Content-Type', 'application/json' );
|
||||
// $response->setStatusCode( 200 );
|
||||
//
|
||||
// return $response;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* update vote stack
|
||||
* @Patch(
|
||||
* path = "/vote-stack/{id}/token/{modifierToken}",
|
||||
* name = "update_vote_stack",
|
||||
* requirements = { "id"="\d+"}
|
||||
* )
|
||||
*
|
||||
* @param SerializerInterface $serializer
|
||||
* @param StackOfVotes $id
|
||||
* @param $modifierToken
|
||||
* @param Request $request
|
||||
*
|
||||
* @return JsonResponse|Response
|
||||
*/
|
||||
public
|
||||
function updateVoteStackAction(
|
||||
SerializerInterface $serializer,
|
||||
StackOfVotes $id,
|
||||
$modifierToken,
|
||||
Request $request
|
||||
) {
|
||||
$voteStack = $id;
|
||||
if ( ! $voteStack ) {
|
||||
return $this->json( [ 'message' => 'vote stack not found' ], 404 );
|
||||
}
|
||||
$poll = $voteStack->getPoll();
|
||||
|
||||
// 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
|
||||
// match votes and choices
|
||||
// update answers
|
||||
// save evrything
|
||||
|
||||
|
||||
$jsonResponse = $serializer->serialize( [
|
||||
'message' => 'ok',
|
||||
'modifier_token' => $voteStack->getOwner()->getModifierToken(),
|
||||
'vote_stack' => $voteStack,
|
||||
],
|
||||
200 );
|
||||
} else {
|
||||
return $this->json( [
|
||||
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage',
|
||||
],
|
||||
403 );
|
||||
'json' );
|
||||
|
||||
$response = new Response( $jsonResponse );
|
||||
$response->headers->set( 'Content-Type', 'application/json' );
|
||||
$response->setStatusCode( 200 );
|
||||
|
||||
return $response;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @Delete(
|
||||
* path = "/poll/{id}/votes/{accessToken}",
|
||||
* name = "poll_votes_delete",
|
||||
* requirements = {"accessToken"="\w+", "poll_id"="\d+"}
|
||||
* )
|
||||
* @return JsonResponse
|
||||
*/
|
||||
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',
|
||||
],
|
||||
200 );
|
||||
} else {
|
||||
return $this->json( [
|
||||
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage',
|
||||
],
|
||||
403 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,13 +21,13 @@ class Owner {
|
||||
* @Serializer\Type("string")
|
||||
* @Serializer\Expose()
|
||||
*/
|
||||
public $pseudo;
|
||||
public $pseudo = 'anonyme';
|
||||
/**
|
||||
* @ORM\Column(type="string", length=255)
|
||||
* @Serializer\Type("string")
|
||||
* @Serializer\Expose()
|
||||
*/
|
||||
public $email;
|
||||
public $email = "anonyme@anonyme.com";
|
||||
/**
|
||||
* @ORM\Id()
|
||||
* @ORM\GeneratedValue()
|
||||
|
@ -108,15 +108,6 @@ class Vote {
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCreationDate(): ?DateTimeInterface {
|
||||
return $this->creationDate;
|
||||
}
|
||||
|
||||
public function setCreationDate( DateTimeInterface $creationDate ): self {
|
||||
$this->creationDate = $creationDate;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getStacksOfVotes(): ?StackOfVotes {
|
||||
return $this->stacksOfVotes;
|
||||
|
@ -4,7 +4,7 @@ namespace App\Repository;
|
||||
|
||||
use App\Entity\Choice;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Common\Persistence\ManagerRegistry;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @method Choice|null find( $id, $lockMode = null, $lockVersion = null )
|
||||
@ -17,32 +17,4 @@ class ChoiceRepository extends ServiceEntityRepository {
|
||||
parent::__construct( $registry, Choice::class );
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @return Choice[] Returns an array of Choice objects
|
||||
// */
|
||||
/*
|
||||
public function findByExampleField($value)
|
||||
{
|
||||
return $this->createQueryBuilder('c')
|
||||
->andWhere('c.exampleField = :val')
|
||||
->setParameter('val', $value)
|
||||
->orderBy('c.id', 'ASC')
|
||||
->setMaxResults(10)
|
||||
->getQuery()
|
||||
->getResult()
|
||||
;
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
public function findOneBySomeField($value): ?Choice
|
||||
{
|
||||
return $this->createQueryBuilder('c')
|
||||
->andWhere('c.exampleField = :val')
|
||||
->setParameter('val', $value)
|
||||
->getQuery()
|
||||
->getOneOrNullResult()
|
||||
;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ namespace App\Repository;
|
||||
|
||||
use App\Entity\Comment;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Common\Persistence\ManagerRegistry;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @method Comment|null find( $id, $lockMode = null, $lockVersion = null )
|
||||
|
@ -4,7 +4,7 @@ namespace App\Repository;
|
||||
|
||||
use App\Entity\Owner;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Common\Persistence\ManagerRegistry;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @method Owner|null find( $id, $lockMode = null, $lockVersion = null )
|
||||
|
@ -4,7 +4,7 @@ namespace App\Repository;
|
||||
|
||||
use App\Entity\StackOfVotes;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Common\Persistence\ManagerRegistry;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @method StackOfVotes|null find( $id, $lockMode = null, $lockVersion = null )
|
||||
|
@ -4,7 +4,7 @@ namespace App\Repository;
|
||||
|
||||
use App\Entity\Vote;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Common\Persistence\ManagerRegistry;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @method Vote|null find( $id, $lockMode = null, $lockVersion = null )
|
||||
|
Loading…
Reference in New Issue
Block a user