save new stack and return updated poll
This commit is contained in:
parent
a481a41a6c
commit
80abd003ae
|
@ -1,7 +1,9 @@
|
||||||
{
|
{
|
||||||
"type": "project",
|
"type": "project",
|
||||||
"license": "proprietary",
|
"license": "aGPLv3",
|
||||||
"minimum-stability": "dev",
|
"minimum-stability": "dev",
|
||||||
|
"description": "Une API pour framadate funky",
|
||||||
|
"name": "datepollapi",
|
||||||
"prefer-stable": true,
|
"prefer-stable": true,
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=7.2.5",
|
"php": ">=7.2.5",
|
||||||
|
@ -25,7 +27,8 @@
|
||||||
"symfony/proxy-manager-bridge": "5.2.*",
|
"symfony/proxy-manager-bridge": "5.2.*",
|
||||||
"symfony/swiftmailer-bundle": "^3.5",
|
"symfony/swiftmailer-bundle": "^3.5",
|
||||||
"symfony/twig-bundle": "5.2.*",
|
"symfony/twig-bundle": "5.2.*",
|
||||||
"symfony/yaml": "5.2.*"
|
"symfony/yaml": "5.2.*",
|
||||||
|
"ext-json": "*"
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"optimize-autoloader": true,
|
"optimize-autoloader": true,
|
||||||
|
|
|
@ -1,5 +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']
|
||||||
secret: '%env(APP_SECRET)%'
|
secret: '%env(APP_SECRET)%'
|
||||||
#csrf_protection: true
|
#csrf_protection: true
|
||||||
#http_method_override: true
|
#http_method_override: true
|
||||||
|
|
|
@ -3,8 +3,9 @@ nelmio_cors:
|
||||||
origin_regex: true
|
origin_regex: true
|
||||||
allow_origin: [ '*' ]
|
allow_origin: [ '*' ]
|
||||||
allow_methods: [ 'GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE' ]
|
allow_methods: [ 'GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE' ]
|
||||||
allow_headers: [ 'Accept','Authorization','Cache-Control','Content-Type','DNT','If-Modified-Since','Keep-Alive','Origin','User-Agent','X-Requested-With' ]
|
allow_headers: [ '*' ]
|
||||||
expose_headers: [ 'Authorization' ]
|
expose_headers: [ 'Authorization' ]
|
||||||
|
allow_credentials: true
|
||||||
max_age: 3600
|
max_age: 3600
|
||||||
paths:
|
paths:
|
||||||
'^/': ~
|
'^/': ~
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
use Symfony\Component\ErrorHandler\Debug;
|
use Symfony\Component\ErrorHandler\Debug;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
header( 'Access-Control-Allow-Origin: *' );
|
// 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-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-Methods: GET, POST, OPTIONS, PUT, DELETE" );
|
||||||
header( "Allow: GET, POST, OPTIONS, PUT, DELETE" );
|
header( "Allow: GET, POST, OPTIONS, PUT, DELETE" );
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
namespace App\Controller\api;
|
namespace App\Controller\api;
|
||||||
|
|
||||||
use App\Controller\EmailsController;
|
use App\Controller\EmailsController;
|
||||||
use App\Entity\Choice;
|
|
||||||
use App\Entity\Owner;
|
use App\Entity\Owner;
|
||||||
use App\Entity\Poll;
|
use App\Entity\Poll;
|
||||||
use App\Entity\StackOfVotes;
|
use App\Entity\StackOfVotes;
|
||||||
|
@ -11,7 +10,6 @@
|
||||||
use App\Repository\ChoiceRepository;
|
use App\Repository\ChoiceRepository;
|
||||||
use FOS\RestBundle\Controller\Annotations\Delete;
|
use FOS\RestBundle\Controller\Annotations\Delete;
|
||||||
use FOS\RestBundle\Controller\Annotations\Patch;
|
use FOS\RestBundle\Controller\Annotations\Patch;
|
||||||
use FOS\RestBundle\Controller\Annotations\Post;
|
|
||||||
use FOS\RestBundle\Controller\Annotations\Route;
|
use FOS\RestBundle\Controller\Annotations\Route;
|
||||||
use JMS\Serializer\SerializerInterface;
|
use JMS\Serializer\SerializerInterface;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
|
@ -28,9 +26,10 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* add a vote stack on a poll
|
* add a vote stack on a poll
|
||||||
* @Post(
|
* @Route(
|
||||||
* path = "/poll/{custom_url}/answer",
|
* path = "/poll/{custom_url}/answer",
|
||||||
* name = "new_vote_stack",
|
* name = "new_vote_stack",
|
||||||
|
* methods={"POST","OPTIONS"}
|
||||||
* )
|
* )
|
||||||
*
|
*
|
||||||
* @param SerializerInterface $serializer
|
* @param SerializerInterface $serializer
|
||||||
|
@ -43,7 +42,7 @@
|
||||||
SerializerInterface $serializer,
|
SerializerInterface $serializer,
|
||||||
string $custom_url,
|
string $custom_url,
|
||||||
Request $request,
|
Request $request,
|
||||||
ChoiceRepository $choice_repository,
|
ChoiceRepository $choice_repository
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
|
||||||
|
@ -57,19 +56,33 @@
|
||||||
|
|
||||||
$data = $request->getContent();
|
$data = $request->getContent();
|
||||||
$data = json_decode( $data, true );
|
$data = json_decode( $data, true );
|
||||||
// $data = $data['data'];
|
|
||||||
|
// var_dump($data);
|
||||||
|
// die();
|
||||||
|
|
||||||
$owner = new Owner();
|
$owner = new Owner();
|
||||||
$owner->addPoll($poll);
|
$owner
|
||||||
|
->addPoll( $poll )
|
||||||
|
|
||||||
|
;
|
||||||
$newStack = new StackOfVotes();
|
$newStack = new StackOfVotes();
|
||||||
$newStack
|
$newStack
|
||||||
->setPseudo( $data[ 'pseudo' ] )
|
->setPoll($poll)
|
||||||
|
->setIp( $_SERVER[ 'REMOTE_ADDR' ] )
|
||||||
|
->setPseudo( $data[ "pseudo" ] )
|
||||||
->setOwner( $owner );
|
->setOwner( $owner );
|
||||||
|
$owner
|
||||||
|
->setPseudo($data['owner'][ "pseudo" ])
|
||||||
|
->setPseudo($data['owner'][ "email" ])
|
||||||
|
->addStackOfVote($newStack);
|
||||||
|
|
||||||
// TODO manage new comment
|
// TODO manage new comment
|
||||||
$emChoice = $choice_repository;
|
$emChoice = $choice_repository;
|
||||||
|
|
||||||
foreach ( $data[ 'votes' ] as $vote ) {
|
foreach ( $data[ 'votes' ] as $vote ) {
|
||||||
|
if(!$vote[ 'value' ]){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
$newVote = new Vote();
|
$newVote = new Vote();
|
||||||
$newVote->setPoll( $poll );
|
$newVote->setPoll( $poll );
|
||||||
|
|
||||||
|
@ -95,88 +108,14 @@
|
||||||
$em->persist( $poll );
|
$em->persist( $poll );
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
|
if ( $poll->getMailOnVote() ) {
|
||||||
|
$this->sendVoteNotificationAction( $newStack->getOwner(), $newStack );
|
||||||
|
}
|
||||||
|
|
||||||
return $this->json( [
|
return $this->json( [
|
||||||
'data' => $data,
|
'poll' => $poll->display(),
|
||||||
] );
|
] );
|
||||||
// $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;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -316,6 +316,19 @@
|
||||||
'id' => $choice_id,
|
'id' => $choice_id,
|
||||||
'url' => $choice_url,
|
'url' => $choice_url,
|
||||||
'name' => $vote->getChoice()->getName(),
|
'name' => $vote->getChoice()->getName(),
|
||||||
|
'score' => 0,
|
||||||
|
'yes' => [
|
||||||
|
'count' => 0,
|
||||||
|
'people' => [],
|
||||||
|
],
|
||||||
|
'maybe' => [
|
||||||
|
'count' => 0,
|
||||||
|
'people' => [],
|
||||||
|
],
|
||||||
|
'no' => [
|
||||||
|
'count' => 0,
|
||||||
|
'people' => [],
|
||||||
|
],
|
||||||
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,15 +67,7 @@ class StackOfVotes {
|
||||||
'created_at' => $this->getCreatedAtAsString(),
|
'created_at' => $this->getCreatedAtAsString(),
|
||||||
'votes' => [],
|
'votes' => [],
|
||||||
];
|
];
|
||||||
// prefill votes with all choices ids
|
foreach ( $this->getVotes() as $vote ) {
|
||||||
// foreach ( $this->getPoll()->getChoices() as $choice ) {
|
|
||||||
// $tab[ 'votes' ][ $choice->getId() ] = [
|
|
||||||
// 'choice_id' => $choice->getId(),
|
|
||||||
// 'value' => null,
|
|
||||||
// ];
|
|
||||||
// }
|
|
||||||
|
|
||||||
foreach ( $votes as $vote ) {
|
|
||||||
$tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display();
|
$tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue