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

Fix serialization of entities using JMS Serializer, done for PollController.

This commit is contained in:
Sébastien Touzé 2020-04-26 12:33:11 +02:00
parent 8d831ba98c
commit 1605754a63
5 changed files with 44 additions and 54 deletions

View File

@ -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;
}
}

View File

@ -41,8 +41,6 @@ class Comment {
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="comments")
* @Serializer\Type("App\Entity\Poll")
* @Serializer\Expose()
*/
private $poll;

View File

@ -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<App\Entity\Vote>")
* @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<App\Entity\Choice>")
*/
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<App\Entity\Comment>")
*/
public $comments;
/**

View File

@ -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;

View File

@ -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;
/**