From 8d831ba98c9a804a6e66b106f8f297fc49d7aa96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Touz=C3=A9?= Date: Sat, 25 Apr 2020 16:44:17 +0200 Subject: [PATCH 1/3] Update requirements for install. --- README.md | 4 ++++ composer.json | 1 + 2 files changed, 5 insertions(+) diff --git a/README.md b/README.md index 6bca0f5..9c8a8da 100755 --- a/README.md +++ b/README.md @@ -26,6 +26,10 @@ install dependencies with Composer there are examples of request to make it all work in the [doc/examples.md](doc/examples.md). +### Check prerequisites +```bash +composer check-platform-reqs +``` ### install the vendors ```bash diff --git a/composer.json b/composer.json index f13b676..447f112 100755 --- a/composer.json +++ b/composer.json @@ -6,6 +6,7 @@ "ext-ctype": "*", "ext-iconv": "*", "ext-json": "*", + "ext-pdo_mysql": "*", "friendsofsymfony/rest-bundle": "^2.6", "jms/serializer-bundle": "^3.4", "nelmio/api-doc-bundle": "^3.4", From 1605754a63bb712ca3334f15f2fea892ee1bc91f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Touz=C3=A9?= Date: Sun, 26 Apr 2020 12:33:11 +0200 Subject: [PATCH 2/3] Fix serialization of entities using JMS Serializer, done for PollController. --- src/Controller/PollController.php | 81 ++++++++++++++----------------- src/Entity/Comment.php | 2 - src/Entity/Poll.php | 9 ++-- src/Entity/StackOfVotes.php | 2 +- src/Entity/Vote.php | 4 +- 5 files changed, 44 insertions(+), 54 deletions(-) diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php index f3f53c2..baa1d60 100644 --- a/src/Controller/PollController.php +++ b/src/Controller/PollController.php @@ -11,9 +11,11 @@ use FOS\RestBundle\Controller\Annotations\Post; use FOS\RestBundle\Controller\Annotations\Put; use FOS\RestBundle\Controller\Annotations\Route; use JMS\Serializer\SerializerBuilder; +use JMS\Serializer\SerializerInterface; use Swift_Mailer; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; /** * Class DefaultController @@ -21,8 +23,6 @@ use Symfony\Component\HttpFoundation\Request; * @Route("/api/v1/poll",name="api_") */ class PollController extends FramadateController { - - /** * @Get( * path = "/", @@ -41,65 +41,56 @@ class PollController extends FramadateController { 200 ); } - /** - * @Get( - * path = "/{id}", - * name = "get_poll", - * requirements = {"poll_id"="\d+"} - * ) - */ - public - function getPollConfig( - Poll $poll, - Request $request - ) { + /** + * @Get( + * path = "/{id}", + * name = "get_poll", + * requirements = {"poll_id"="\d+"} + * ) + * @param SerializerInterface $serializer + * @param Poll $poll + * @param Request $request + * @return JsonResponse|Response + */ + public function getPollConfig( + SerializerInterface $serializer, + Poll $poll, + Request $request + ) { $pass = $poll->getPassword(); $data = $request->getContent(); $data = json_decode( $data, true ); - $comments = []; - $stacks = []; - $choices = []; - foreach ( $poll->getComments() as $c ) { - $comments[] = $c->display(); - } - foreach ( $poll->getStacksOfVotes() as $c ) { - $stacks[] = $c->display(); - } - foreach ( $poll->getChoices() as $c ) { - $choices[] = $c->display(); - } + $comments = $poll->getComments(); + $returnedPoll = [ 'message' => 'your poll config', 'poll' => $poll, 'stacks_count' => count( $poll->getStacksOfVotes() ), - 'stacks' => $stacks, + 'stacks' => $poll->getStacksOfVotes(), 'choices_count' => $poll->computeAnswers(), - 'choices' => $choices, + 'choices' => $poll->getChoices(), 'comments' => $comments, 'comments_count' => count( $comments ), ]; + /** * password protected content */ - if ( $pass ) { - - if ( $pass == md5( $data[ 'password_input' ] ) ) { - return $this->json( - $returnedPoll, - 200 ); - } else { - return $this->json( [ - 'message' => 'your password ' . $data[ 'password_input' ] . ' is wrong, and you should feel bad', - 'data' => null, - ], - 403 ); - } + if ( $pass && $pass !== md5($data[ 'password_input' ])) { + return $this->json( [ + 'message' => 'your password ' . $data[ 'password_input' ] . ' is wrong, and you should feel bad', + 'data' => null, + ], + 403 ); } else { - return $this->json( - $returnedPoll - , - 200 ); + $jsonResponse = $serializer->serialize($returnedPoll, 'json'); + + $response = new Response($jsonResponse); + $response->headers->set('Content-Type', 'application/json'); + $response->setStatusCode(200); + + return $response; } } diff --git a/src/Entity/Comment.php b/src/Entity/Comment.php index ee0a3e5..c715869 100755 --- a/src/Entity/Comment.php +++ b/src/Entity/Comment.php @@ -41,8 +41,6 @@ class Comment { /** * @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="comments") - * @Serializer\Type("App\Entity\Poll") - * @Serializer\Expose() */ private $poll; diff --git a/src/Entity/Poll.php b/src/Entity/Poll.php index 900341d..4b5fced 100755 --- a/src/Entity/Poll.php +++ b/src/Entity/Poll.php @@ -10,7 +10,7 @@ /** * @ORM\Entity(repositoryClass="App\Repository\PollRepository") - * @Serializer\ExclusionPolicy("all") + * @Serializer\ExclusionPolicy("all") */ class Poll { /** @@ -68,7 +68,7 @@ * array of possible answer to each choice, by default: "yes" or nothing only. * could be also "yes", "maybe", "no". extensible to anything * @ORM\Column(type="array") - * @Serializer\Type("string") + * @Serializer\Type("array") * @Serializer\Expose() */ public $allowedAnswers; @@ -112,7 +112,7 @@ public $showResultEvenIfPasswords; /** * @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="poll", orphanRemoval=true, cascade={"persist", "remove"}) - * @Serializer\Type("App\Entity\Vote") + * @Serializer\Type("ArrayCollection") * @Serializer\Expose() */ public $votes; @@ -124,12 +124,13 @@ /** * @ORM\OneToMany(targetEntity="App\Entity\Choice", mappedBy="poll", orphanRemoval=true, cascade={"persist", "remove"}) * @Serializer\Expose() + * @Serializer\Type("ArrayCollection") */ public $choices; /** * @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="poll", orphanRemoval=true, cascade={"persist", "remove"}) * @Serializer\Expose() - * @Serializer\Type("App\Entity\Comment") + * @Serializer\Type("ArrayCollection") */ public $comments; /** diff --git a/src/Entity/StackOfVotes.php b/src/Entity/StackOfVotes.php index 397464c..2916446 100755 --- a/src/Entity/StackOfVotes.php +++ b/src/Entity/StackOfVotes.php @@ -11,6 +11,7 @@ use JMS\Serializer\Annotation as Serializer; * contains the votes for one answer to a poll * @ORM\Entity(repositoryClass="App\Repository\StackOfVotesRepository") * @ORM\HasLifecycleCallbacks() + * @Serializer\ExclusionPolicy("all") */ class StackOfVotes { /** @@ -32,7 +33,6 @@ class StackOfVotes { public $votes; /** * @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="stacksOfVotes", cascade={"persist"}) - * @Serializer\Expose() */ private $poll; diff --git a/src/Entity/Vote.php b/src/Entity/Vote.php index 00a5857..6134d18 100755 --- a/src/Entity/Vote.php +++ b/src/Entity/Vote.php @@ -8,10 +8,11 @@ /** * @ORM\Entity(repositoryClass="App\Repository\VoteRepository") + * @Serializer\ExclusionPolicy("all") */ class Vote { /** - * for a text kind of choice: could be "yes" "no" "maybe" and emptu. + * for a text kind of choice: could be "yes" "no" "maybe" and empty. * for a date kind, the choice linked is equivalent to the value selected * @ORM\Column(type="string", length=255, nullable=true) * @Serializer\Type("string") @@ -42,7 +43,6 @@ /** * @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="votes", cascade={"persist"}) * @ORM\JoinColumn(nullable=false) - * @Serializer\Type("App\Entity\Poll") */ private $poll; /** From 4bd70f0fc7b515af0d2aaa1a4bbaaa295323f6a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Touz=C3=A9?= Date: Sun, 26 Apr 2020 13:04:29 +0200 Subject: [PATCH 3/3] Fix serialization with JMS Serializer for remainitn controllers anc methods. --- src/Controller/CommentController.php | 20 +++-- src/Controller/PollController.php | 56 ++++++------ src/Controller/VoteController.php | 129 +++++++++++++++------------ 3 files changed, 114 insertions(+), 91 deletions(-) diff --git a/src/Controller/CommentController.php b/src/Controller/CommentController.php index 9a5c690..18765d3 100644 --- a/src/Controller/CommentController.php +++ b/src/Controller/CommentController.php @@ -11,8 +11,10 @@ use FOS\RestBundle\Controller\Annotations\Get; use FOS\RestBundle\Controller\Annotations\Post; use FOS\RestBundle\Controller\Annotations\Route; use JMS\Serializer\SerializerBuilder; +use JMS\Serializer\SerializerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; /** * Class DefaultController @@ -30,13 +32,19 @@ class CommentController extends FramadateController { */ public function getPollCommentsAction( - Poll $poll + SerializerInterface $serializer, + Poll $poll ) { - return $this->json( [ - 'message' => 'here are your comments of the poll', - 'data' => $poll->getComments(), - ], - 200 ); + $jsonResponse = $serializer->serialize([ + 'message' => 'here are your comments of the poll', + 'data' => $poll->getComments()], 'json'); + + $response = new Response($jsonResponse); + $response->headers->set('Content-Type', 'application/json'); + $response->setStatusCode(200); + + return $response; + } /** diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php index baa1d60..31ff4a3 100644 --- a/src/Controller/PollController.php +++ b/src/Controller/PollController.php @@ -50,6 +50,7 @@ class PollController extends FramadateController { * @param SerializerInterface $serializer * @param Poll $poll * @param Request $request + * * @return JsonResponse|Response */ public function getPollConfig( @@ -102,8 +103,7 @@ class PollController extends FramadateController { * requirements = {"content"="\w+", "poll_id"="\d+"} * ) */ - public - function updatePollConfig( + public function updatePollConfig( Poll $poll, string $token, Request $request @@ -211,7 +211,7 @@ class PollController extends FramadateController { // different hours spans $choices = $data[ 'dateList' ]; } else { - +//TODO (Sébastien) I assume this shouldn't be empty ? // all days have the same hour spans } @@ -350,46 +350,46 @@ class PollController extends FramadateController { } - - /** - * Get Admin poll config - * @Get( - * path = "/admin/{token}", - * name = "get_admin_config", - * ) - */ - public function getAdministrationConfig( $token ) { + /** + * Get Admin poll config + * @Get( + * path = "/admin/{token}", + * name = "get_admin_config", + * ) + * @param SerializerInterface $serializer + * @param $token + * + * @return JsonResponse|Response + */ + public function getAdministrationConfig(SerializerInterface $serializer, $token ) { $emPoll = $this->getDoctrine()->getRepository( Poll::class ); $pollFound = $emPoll->findOneByAdminKey( $token ); + if ( $pollFound ) { $poll = $pollFound; - $comments = []; - $stacks = []; - $choices = []; - foreach ( $poll->getComments() as $c ) { - $comments[] = $c->display(); - } - foreach ( $poll->getStacksOfVotes() as $c ) { - $stacks[] = $c->display(); - } - foreach ( $poll->getChoices() as $c ) { - $choices[] = $c->display(); - } + $comments = $poll->getComments(); + $stacks = $poll->getStacksOfVotes(); + $returnedPoll = [ 'message' => 'your poll config', 'poll' => $poll, - 'stacks_count' => count( $poll->getStacksOfVotes() ), + 'stacks_count' => count( $stacks ), 'stacks' => $stacks, 'choices_count' => $poll->computeAnswers(), - 'choices' => $choices, + 'choices' => $poll->getChoices(), 'comments' => $comments, 'comments_count' => count( $comments ), 'token' => $token, ]; - return $this->json( $returnedPoll, - 200 ); + $jsonResponse = $serializer->serialize($returnedPoll, 'json'); + + $response = new Response($jsonResponse); + $response->headers->set('Content-Type', 'application/json'); + $response->setStatusCode(200); + + return $response; } return $this->json( [ diff --git a/src/Controller/VoteController.php b/src/Controller/VoteController.php index 41fb41d..1b4687e 100644 --- a/src/Controller/VoteController.php +++ b/src/Controller/VoteController.php @@ -11,8 +11,10 @@ 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 @@ -21,18 +23,23 @@ use Symfony\Component\HttpFoundation\Request; */ class VoteController extends FramadateController { - /** - * add a vote stack on a poll - * @Post( - * path = "/poll/{id}/vote", - * name = "new_vote_stack", - * requirements = {"content"="\w+", "poll_id"="\d+"} - * ) - */ - public - function newVoteStackAction( - Poll $poll, - Request $request + /** + * add a vote stack on a poll + * @Post( + * path = "/poll/{id}/vote", + * name = "new_vote_stack", + * requirements = {"content"="\w+", "poll_id"="\d+"} + * ) + * @param SerializerInterface $serializer + * @param Poll $poll + * @param Request $request + * + * @return JsonResponse|Response + */ + public function newVoteStackAction( + SerializerInterface $serializer, + Poll $poll, + Request $request ) { if ( ! $poll ) { return $this->json( [ 'message' => 'poll not found' ], 404 ); @@ -106,55 +113,57 @@ class VoteController extends FramadateController { if ( $existingOwner ) { $precision = ' from an existing owner : ' . $foundOwner->getEmail(); } - $comments = []; - $stacks = []; - $choices = []; - foreach ( $poll->getComments() as $c ) { - $comments[] = $c->display(); - } - foreach ( $poll->getStacksOfVotes() as $c ) { - $stacks[] = $c->display(); - } - foreach ( $poll->getChoices() as $c ) { - $choices[] = $c->display(); - } - + $stacks = $poll->getStacksOfVotes(); if($poll->getMailOnVote()){ $this->sendVoteNotificationAction($stack->getOwner(), $stack); } + $returnedVoteStack = [ + 'message' => 'you created a vote stack' . $precision, + 'poll' => $poll, + 'vote_stack' => $stack, + 'stacks' => $stacks, + 'comments' => $poll->getComments(), + 'choices' => $poll->getChoices(), + 'choices_count' => $poll->computeAnswers(), + 'vote_count' => count( $stacks ), + 'owner' => $stack->getOwner(), + 'owner_modifier_token' => $stack->getOwner()->getModifierToken(), + 'admin_key' => $poll->getAdminKey(), + 'json_you_sent' => $data, + ]; + + $jsonResponse = $serializer->serialize($returnedVoteStack, 'json'); + + $response = new Response($jsonResponse); + $response->headers->set('Content-Type', 'application/json'); + $response->setStatusCode(200); + + return $response; - return $this->json( [ - 'message' => 'you created a vote stack' . $precision, - 'poll' => $poll, - 'vote_stack' => $stack->display(), - 'stacks' => $stacks, - 'comments' => $comments, - 'choices' => $choices, - 'choices_count' => $poll->computeAnswers(), - 'vote_count' => count( $poll->getStacksOfVotes() ), - 'owner' => $stack->getOwner(), - 'owner_modifier_token' => $stack->getOwner()->getModifierToken(), - 'admin_key' => $poll->getAdminKey(), - 'json_you_sent' => $data, - ], - 201 ); } - /** - * update vote stack - * @Patch( - * path = "/vote-stack/{id}/token/{modifierToken}", - * name = "update_vote_stack", - * requirements = { "id"="\d+"} - * ) - */ + /** + * 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( - StackOfVotes $id, - $modifierToken, - Request $request + SerializerInterface $serializer, + StackOfVotes $id, + $modifierToken, + Request $request ) { $voteStack = $id; if ( ! $voteStack ) { @@ -173,12 +182,18 @@ class VoteController extends FramadateController { // update answers // save evrything - return $this->json( [ - 'message' => 'ok', - 'modifier_token' => $voteStack->getOwner()->getModifierToken(), - 'vote_stack' => $voteStack->display(), - ], - 200 ); + + $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; }