diff --git a/composer.json b/composer.json index be90708..4f7b111 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ "symfony/maker-bundle": "^1.14", "symfony/orm-pack": "^1.0", "symfony/validator": "4.3.*", - "symfony/yaml": "4.3.*" + "symfony/yaml": "4.3.*", + "ext-json": "*" }, "require-dev": { "doctrine/doctrine-fixtures-bundle": "^3.2", diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 09ca413..1a7dc93 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -4,6 +4,8 @@ namespace App\Controller; 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\Get; use FOS\RestBundle\Controller\Annotations\Post; @@ -263,17 +265,30 @@ class DefaultController extends AbstractController { if ( ! $poll ) { return $this->json( [ 'message' => 'poll not found' ], 404 ); } -// $data = $request->getContent(); + $data = $request->getContent(); + $data = json_decode( $data, true ); + + $newOwner = new Owner(); + $newOwner + ->setEmail( $data[ 'email' ] ) + ->setPseudo( $data[ 'pseudo' ] ); + $stack = new StackOfVotes(); + foreach ( $data[ 'votes' ] as $voteInfo ) { + $vote = new Vote(); + $foundChoice = $poll->findChoiceById( $voteInfo[ 'choice_id' ] ); + $vote->setPoll( $poll ) + ->setChoice( $foundChoice ) + ->setValue( $voteInfo[ 'value' ] ); + $stack->addVote( $vote ); + } + + // find poll from choices + $poll->addStackOfVote( $stack ); // -// $serializer = SerializerBuilder::create()->build(); -// $comment = $serializer->deserialize( $data, 'App\Entity\Comment', 'json' ); -// -// $em = $this->getDoctrine()->getRepository( Owner::class ); -// -// $data = json_decode( $data, true ); + // // $foundOwner = $em->findByEmail( $data[ 'owner' ][ 'email' ] ); -// // manage existing or new Owner + // manage existing or new Owner // if ( ! $foundOwner ) { // $foundOwner = new Owner(); // $foundOwner->setPseudo( $data[ 'owner' ][ 'email' ] ) @@ -284,13 +299,14 @@ class DefaultController extends AbstractController { // ->setPoll( $poll ); // $foundOwner->addComment( $comment ); // -// $em = $this->getDoctrine()->getManager(); -// $em->persist( $foundOwner ); + $em = $this->getDoctrine()->getManager(); + $em->persist( $stack ); // $em->persist( $comment ); -// $em->flush(); + $em->flush(); return $this->json( [ - 'message' => 'you created a comment', + 'message' => 'you created a comment', + 'json_you_sent' => $data, ], 201 ); } diff --git a/src/Entity/Poll.php b/src/Entity/Poll.php index be04677..83176ae 100644 --- a/src/Entity/Poll.php +++ b/src/Entity/Poll.php @@ -155,6 +155,10 @@ class Poll { $this->setAllowedAnswers( [ 'yes' ] ); } + public function findChoiceById( $id ) { + return new Choice(); // TODO + } + public function addDaysToDate( \DateTime $date, int $days ) { $st = strtotime( $date->getTimestamp() . ' + ' . $days . ' days' );