From 6969ff9818c163eb3dc8708a64fcb49b9e72b412 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Mon, 20 Jan 2020 17:27:59 +0100 Subject: [PATCH] :zap: comment data in get poll by id --- README.md | 2 +- src/Controller/DefaultController.php | 20 +++++++++++++++++--- src/Entity/Comment.php | 10 ++++++++++ src/Entity/Vote.php | 4 ++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d483294..992a6c9 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ composer install ```bash php bin/console doctrine:schema:drop --force php bin/console doctrine:schema:create -php bin/console doctrine:fixtures:load --no-interaction +php bin/console doctrine:fixtures:load --no-interaction --purge-with-truncate ``` ### launch local server with ```bash diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index f2fd3c3..3e56280 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -225,10 +225,19 @@ class DefaultController extends AbstractController { $stacks = []; $choices = []; foreach ( $poll->getComments() as $c ) { - $comments[] = $c; + $comments[] = [ + 'pseudo' => $c->getOwner()->getPseudo(), + 'date' => $c->getCreatedAt(), + 'text' => $c->getText(), + ]; } foreach ( $poll->getStacksOfVotes() as $c ) { - $stacks[] = $c; + $stacks[] = + [ + "id" => $c->getId(), + "pseudo" => $c->getOwner()->getPseudo(), + "votes" => $c->getVotes(), + ]; } foreach ( $poll->getChoices() as $c ) { $choices[] = $c; @@ -343,7 +352,7 @@ class DefaultController extends AbstractController { $data = json_decode( $data, true ); - $foundOwner = $em->findByEmail( $data[ 'owner' ][ 'email' ] ); + $foundOwner = $em->findOneByEmail( $data[ 'email' ] ); // manage existing or new Owner if ( ! $foundOwner ) { $foundOwner = new Owner(); @@ -352,6 +361,7 @@ class DefaultController extends AbstractController { ->setModifierToken( uniqid() ); } $comment->setOwner( $foundOwner ) + ->setCreatedAt( new \DateTime() ) ->setPoll( $poll ); $foundOwner->addComment( $comment ); @@ -362,6 +372,10 @@ class DefaultController extends AbstractController { return $this->json( [ 'message' => 'you created a comment', + 'data' => [ + 'your_comment' => $comment, + 'poll_comments' => $poll->getComments(), + ], ], 201 ); } diff --git a/src/Entity/Comment.php b/src/Entity/Comment.php index 713de7d..0730efe 100644 --- a/src/Entity/Comment.php +++ b/src/Entity/Comment.php @@ -4,9 +4,11 @@ namespace App\Entity; use DateTimeInterface; use Doctrine\ORM\Mapping as ORM; +use JMS\Serializer\Annotation as Serializer; /** * @ORM\Entity(repositoryClass="App\Repository\CommentRepository") + * @Serializer\ExclusionPolicy("all") */ class Comment { /** @@ -18,21 +20,29 @@ class Comment { /** * @ORM\ManyToOne(targetEntity="App\Entity\Owner", inversedBy="comments") + * @Serializer\Type("App\Entity\Owner") + * @Serializer\Expose() */ private $owner; /** * @ORM\Column(type="text") + * @Serializer\Type("string") + * @Serializer\Expose() */ private $text; /** * @ORM\Column(type="datetime") + * @Serializer\Type("datetime") + * @Serializer\Expose() */ private $createdAt; /** * @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="comments") + * @Serializer\Type("App\Entity\Poll") + * @Serializer\Expose() */ private $poll; diff --git a/src/Entity/Vote.php b/src/Entity/Vote.php index 92e290c..707c403 100644 --- a/src/Entity/Vote.php +++ b/src/Entity/Vote.php @@ -15,17 +15,20 @@ class Vote { * for a date kind, the choice linked is equivalent to the value selected * @ORM\Column(type="string", length=255, nullable=true) * @Serializer\Type("string") + * @Serializer\Expose() */ public $value; /** * @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"}) * @Serializer\Type("datetime") + * @Serializer\Expose() */ public $creationDate; /** * @ORM\ManyToOne(targetEntity="App\Entity\Choice", inversedBy="votes", cascade={"persist"}) * @ORM\JoinColumn(nullable=false) * @Serializer\Type("App\Entity\choice") + * @Serializer\Expose() */ public $choice; /** @@ -33,6 +36,7 @@ class Vote { * @ORM\GeneratedValue() * @ORM\Column(type="integer") * @Serializer\Type("integer") + * @Serializer\Expose() */ private $id; /**