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

Fix serialization with JMS Serializer for remainitn controllers anc methods.

This commit is contained in:
Sébastien Touzé 2020-04-26 13:04:29 +02:00
parent 1605754a63
commit 4bd70f0fc7
3 changed files with 114 additions and 91 deletions

View File

@ -11,8 +11,10 @@ use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Post; use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations\Route; use FOS\RestBundle\Controller\Annotations\Route;
use JMS\Serializer\SerializerBuilder; use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializerInterface;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/** /**
* Class DefaultController * Class DefaultController
@ -30,13 +32,19 @@ class CommentController extends FramadateController {
*/ */
public public
function getPollCommentsAction( function getPollCommentsAction(
Poll $poll SerializerInterface $serializer,
Poll $poll
) { ) {
return $this->json( [ $jsonResponse = $serializer->serialize([
'message' => 'here are your comments of the poll', 'message' => 'here are your comments of the poll',
'data' => $poll->getComments(), 'data' => $poll->getComments()], 'json');
],
200 ); $response = new Response($jsonResponse);
$response->headers->set('Content-Type', 'application/json');
$response->setStatusCode(200);
return $response;
} }
/** /**

View File

@ -50,6 +50,7 @@ class PollController extends FramadateController {
* @param SerializerInterface $serializer * @param SerializerInterface $serializer
* @param Poll $poll * @param Poll $poll
* @param Request $request * @param Request $request
*
* @return JsonResponse|Response * @return JsonResponse|Response
*/ */
public function getPollConfig( public function getPollConfig(
@ -102,8 +103,7 @@ class PollController extends FramadateController {
* requirements = {"content"="\w+", "poll_id"="\d+"} * requirements = {"content"="\w+", "poll_id"="\d+"}
* ) * )
*/ */
public public function updatePollConfig(
function updatePollConfig(
Poll $poll, Poll $poll,
string $token, string $token,
Request $request Request $request
@ -211,7 +211,7 @@ class PollController extends FramadateController {
// different hours spans // different hours spans
$choices = $data[ 'dateList' ]; $choices = $data[ 'dateList' ];
} else { } else {
//TODO (Sébastien) I assume this shouldn't be empty ?
// all days have the same hour spans // all days have the same hour spans
} }
@ -350,46 +350,46 @@ class PollController extends FramadateController {
} }
/**
/** * Get Admin poll config
* Get Admin poll config * @Get(
* @Get( * path = "/admin/{token}",
* path = "/admin/{token}", * name = "get_admin_config",
* name = "get_admin_config", * )
* ) * @param SerializerInterface $serializer
*/ * @param $token
public function getAdministrationConfig( $token ) { *
* @return JsonResponse|Response
*/
public function getAdministrationConfig(SerializerInterface $serializer, $token ) {
$emPoll = $this->getDoctrine()->getRepository( Poll::class ); $emPoll = $this->getDoctrine()->getRepository( Poll::class );
$pollFound = $emPoll->findOneByAdminKey( $token ); $pollFound = $emPoll->findOneByAdminKey( $token );
if ( $pollFound ) { if ( $pollFound ) {
$poll = $pollFound; $poll = $pollFound;
$comments = []; $comments = $poll->getComments();
$stacks = []; $stacks = $poll->getStacksOfVotes();
$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();
}
$returnedPoll = [ $returnedPoll = [
'message' => 'your poll config', 'message' => 'your poll config',
'poll' => $poll, 'poll' => $poll,
'stacks_count' => count( $poll->getStacksOfVotes() ), 'stacks_count' => count( $stacks ),
'stacks' => $stacks, 'stacks' => $stacks,
'choices_count' => $poll->computeAnswers(), 'choices_count' => $poll->computeAnswers(),
'choices' => $choices, 'choices' => $poll->getChoices(),
'comments' => $comments, 'comments' => $comments,
'comments_count' => count( $comments ), 'comments_count' => count( $comments ),
'token' => $token, 'token' => $token,
]; ];
return $this->json( $returnedPoll, $jsonResponse = $serializer->serialize($returnedPoll, 'json');
200 );
$response = new Response($jsonResponse);
$response->headers->set('Content-Type', 'application/json');
$response->setStatusCode(200);
return $response;
} }
return $this->json( [ return $this->json( [

View File

@ -11,8 +11,10 @@ use FOS\RestBundle\Controller\Annotations\Delete;
use FOS\RestBundle\Controller\Annotations\Patch; use FOS\RestBundle\Controller\Annotations\Patch;
use FOS\RestBundle\Controller\Annotations\Post; use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations\Route; use FOS\RestBundle\Controller\Annotations\Route;
use JMS\Serializer\SerializerInterface;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/** /**
* Class DefaultController * Class DefaultController
@ -21,18 +23,23 @@ use Symfony\Component\HttpFoundation\Request;
*/ */
class VoteController extends FramadateController { class VoteController extends FramadateController {
/** /**
* add a vote stack on a poll * add a vote stack on a poll
* @Post( * @Post(
* path = "/poll/{id}/vote", * path = "/poll/{id}/vote",
* name = "new_vote_stack", * name = "new_vote_stack",
* requirements = {"content"="\w+", "poll_id"="\d+"} * requirements = {"content"="\w+", "poll_id"="\d+"}
* ) * )
*/ * @param SerializerInterface $serializer
public * @param Poll $poll
function newVoteStackAction( * @param Request $request
Poll $poll, *
Request $request * @return JsonResponse|Response
*/
public function newVoteStackAction(
SerializerInterface $serializer,
Poll $poll,
Request $request
) { ) {
if ( ! $poll ) { if ( ! $poll ) {
return $this->json( [ 'message' => 'poll not found' ], 404 ); return $this->json( [ 'message' => 'poll not found' ], 404 );
@ -106,55 +113,57 @@ class VoteController extends FramadateController {
if ( $existingOwner ) { if ( $existingOwner ) {
$precision = ' from an existing owner : ' . $foundOwner->getEmail(); $precision = ' from an existing owner : ' . $foundOwner->getEmail();
} }
$comments = []; $stacks = $poll->getStacksOfVotes();
$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();
}
if($poll->getMailOnVote()){ if($poll->getMailOnVote()){
$this->sendVoteNotificationAction($stack->getOwner(), $stack); $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 * update vote stack
* @Patch( * @Patch(
* path = "/vote-stack/{id}/token/{modifierToken}", * path = "/vote-stack/{id}/token/{modifierToken}",
* name = "update_vote_stack", * name = "update_vote_stack",
* requirements = { "id"="\d+"} * requirements = { "id"="\d+"}
* ) * )
*/ * @param SerializerInterface $serializer
* @param StackOfVotes $id
* @param $modifierToken
* @param Request $request
*
* @return JsonResponse|Response
*/
public public
function updateVoteStackAction( function updateVoteStackAction(
StackOfVotes $id, SerializerInterface $serializer,
$modifierToken, StackOfVotes $id,
Request $request $modifierToken,
Request $request
) { ) {
$voteStack = $id; $voteStack = $id;
if ( ! $voteStack ) { if ( ! $voteStack ) {
@ -173,12 +182,18 @@ class VoteController extends FramadateController {
// update answers // update answers
// save evrything // save evrything
return $this->json( [
'message' => 'ok', $jsonResponse = $serializer->serialize([
'modifier_token' => $voteStack->getOwner()->getModifierToken(), 'message' => 'ok',
'vote_stack' => $voteStack->display(), 'modifier_token' => $voteStack->getOwner()->getModifierToken(),
], 'vote_stack' => $voteStack,
200 ); ], 'json');
$response = new Response($jsonResponse);
$response->headers->set('Content-Type', 'application/json');
$response->setStatusCode(200);
return $response;
} }