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

persist date choices on creation

This commit is contained in:
Tykayn 2021-05-18 22:35:32 +02:00 committed by tykayn
parent f5a154b20e
commit 03cba297d5
2 changed files with 23 additions and 9 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', 'framadate-api.cipherbliss.com'] trusted_hosts: ['localhost:4200', 'localhost', 'tktest.lan', '127.0.0.1', '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

@ -240,9 +240,17 @@ class PollController extends EmailsController {
$data = $request->getContent(); $data = $request->getContent();
$data = json_decode( $data, true ); $data = json_decode( $data, true );
// search for existing custom url, which must be unique
$custom_url = $data[ 'custom_url' ];
$repository = $this->getDoctrine()->getRepository( Poll::class );
$poll = $repository->findOneByCustomUrl( $custom_url );
if ( $poll ) {
throw new \JsonException( 'NOPE, ce sondage existe déjà: '.$custom_url );
}
$newpoll = new Poll(); $newpoll = new Poll();
$newpoll $newpoll
->setModificationPolicy( $data[ 'modification_policy' ] ) ->setModificationPolicy( isset( $data[ 'modification_policy' ] ) ? $data[ 'modification_policy' ] : 'everybody' )
->setTitle( $data[ 'title' ] ) ->setTitle( $data[ 'title' ] )
->setKind( $data[ 'kind' ] ) ->setKind( $data[ 'kind' ] )
->setCustomUrl( $data[ 'custom_url' ] ); ->setCustomUrl( $data[ 'custom_url' ] );
@ -294,17 +302,18 @@ class PollController extends EmailsController {
if ( $data[ 'password' ] ) { if ( $data[ 'password' ] ) {
$newpoll->setPassword( $data[ 'password' ] ); $newpoll->setPassword( $data[ 'password' ] );
} }
// manage choices
// text kind of answers, dates are below // text kind of answers, dates are below
if ( $data[ 'kind' ] == 'text' ) { if ( $data[ 'kind' ] == 'text' ) {
// manage choices
$choices = $data[ 'dateChoices' ]; $choices = $data[ 'choices' ];
foreach ( $choices as $c ) { foreach ( $choices as $c ) {
$newChoice = new Choice(); $newChoice = new Choice();
$newChoice $newChoice
->setPoll( $newpoll ) ->setPoll( $newpoll )
->setName( $c[ 'literal' ] ); ->setName( $c[ 'literal' ] );
$em->persist( $newChoice ); $em->persist( $newChoice );
$newpoll->addChoice( $newChoice );
} }
} // date kind of poll } // date kind of poll
elseif ( $data[ 'kind' ] == 'date' ) { elseif ( $data[ 'kind' ] == 'date' ) {
@ -316,7 +325,7 @@ class PollController extends EmailsController {
foreach ( $choices as $c ) { foreach ( $choices as $c ) {
$currentDate = $c[ 'literal' ]; $currentDate = $c[ 'literal' ];
$timeSlicesOfThisChoice = $c[ 'timeList' ]; $timeSlicesOfThisChoice = $c[ 'timeSlices' ];
foreach ( $timeSlicesOfThisChoice as $t ) { foreach ( $timeSlicesOfThisChoice as $t ) {
$newChoice = new Choice(); $newChoice = new Choice();
@ -324,14 +333,16 @@ class PollController extends EmailsController {
->setPoll( $newpoll ) ->setPoll( $newpoll )
->setName( $currentDate . ' >>> ' . $t[ 'literal' ] ); ->setName( $currentDate . ' >>> ' . $t[ 'literal' ] );
$em->persist( $newChoice ); $em->persist( $newChoice );
$newpoll->addChoice( $newChoice );
} }
} }
} else { } else {
// all choices will be having the same time slices from timeSlices // all choices will be having the same time slices from timeSlices
$timeSlicesForAllChoices = $data[ 'timeSlices' ]; $timeSlicesForAllChoices = $data[ 'timeSlices' ];
foreach ( $choices as $c ) { foreach ( $choices as $c ) {
$currentDate = $c[ 'literal' ]; $currentDate = $c[ 'date_object' ];
foreach ( $timeSlicesForAllChoices as $t ) { foreach ( $timeSlicesForAllChoices as $t ) {
@ -340,6 +351,7 @@ class PollController extends EmailsController {
->setPoll( $newpoll ) ->setPoll( $newpoll )
->setName( $currentDate . ' >>> ' . $t[ 'literal' ] ); ->setName( $currentDate . ' >>> ' . $t[ 'literal' ] );
$em->persist( $newChoice ); $em->persist( $newChoice );
$newpoll->addChoice( $newChoice );
} }
@ -347,7 +359,6 @@ class PollController extends EmailsController {
} }
} }
$em->persist( $newpoll ); $em->persist( $newpoll );
@ -359,8 +370,11 @@ class PollController extends EmailsController {
$this->sendCreationMailAction( $foundOwner, $newpoll ); $this->sendCreationMailAction( $foundOwner, $newpoll );
$newChoices = $newpoll->display()[ 'choices' ];
return $this->json( [ return $this->json( [
'message' => 'you created a poll ' . $precision, 'message' => 'you created the poll ' . $newpoll->getCustomUrl() . $precision,
'id' => $newpoll->getId(),
'poll' => $newpoll->displayForAdmin(), 'poll' => $newpoll->displayForAdmin(),
'password_protected' => is_string( $newpoll->getPassword() ), 'password_protected' => is_string( $newpoll->getPassword() ),