date-poll-api/src/Controller/DefaultController.php

193 lines
4.2 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\Owner;
use App\Entity\Poll;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations\Route;
use JMS\Serializer\SerializerBuilder;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
/**
* Class DefaultController
* @package App\Controller
* @Route("/api/v1",name="api_")
*/
class DefaultController extends AbstractController {
/**
* @Get(path ="/",
* name = "get_default")
*/
public function index() {
return $this->json( [
'message' => 'Welcome to your new controller!',
'path' => 'src/Controller/DefaultController.php',
] );
}
/**
* @Get(
* path = "/my-polls",
* name = "get_my_polls",
* requirements = {"access_token"="\w+"}
* )
*/
public function showMyPollsAction() {
return $this->json( [
'message' => 'here are your polls',
'data' => new Poll(),
] );
}
/**
* @Get(
* path = "/poll/{id}/comments",
* name = "get_poll_comment",
* requirements = {"id"="\d+"}
* )
*/
public function getPollCommentsAction() {
return $this->json( [
'message' => 'here are your comments of the poll',
] );
}
/**
* @Get(
* path = "/poll/all",
* name = "get_all_polls"
* )
*/
public function getAllPollsAction() {
$repository = $this->getDoctrine()->getRepository( Poll::class );
$data = $repository->findall();
return $this->json( [
'message' => 'here are your polls',
'data' => $data,
] );
}
/**
* @Post(
* path = "/poll/new",
* name = "new_polls",
* requirements = {"creator"="\w+"}
* )
* @param Request $request
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function newPollAction( Request $request ) {
$data = $request->getContent();
$serializer = SerializerBuilder::create()->build();
$newpoll = $serializer->deserialize( $data, 'App\Entity\Poll', 'json' );
$newpoll->setAdminKey( uniqid() );
$newpoll->setCreationDate( new \DateTime() );
$newpoll->setModificationPolicy( 'none' );
$timeStamp = time() + ( 3600 * 24 * 90 ); // 90 days by default
$newpoll->setExpiracyDate( ( new \DateTime() )->setTimestamp( $timeStamp ),
new \DateTimeZone( 'Europe/Paris' ) );
$data = json_decode( $data, true );
// die( $data );
$em = $this->getDoctrine()->getRepository( Owner::class );
$foundOwner = $em->findByEmail( $data[ 'owner' ][ 'email' ] );
die( $foundOwner );
$userWasFound = false;
if ( ! $foundOwner ) {
//create a new owner
$owner = new Owner();
$owner->setPseudo( $data[ 'owner' ][ 'pseudo' ] );
$owner->setEmail( $data[ 'owner' ][ 'email' ] );
$foundOwner = $owner;
} else {
$userWasFound = true;
}
// link the owner and the poll
$newpoll->setOwner( $foundOwner );
$foundOwner->addPoll( $newpoll );
$em->persist( $newpoll );
$em->flush();
$precision = '';
if ( $userWasFound ) {
$precision = 'from an existing user : ' . $foundOwner->getEmail();
}
return $this->json( [
'message' => 'you created a poll ' . $precision,
// 'data' => $jsonContent,
],
203 );
}
/**
* @Get(
* path = "/poll/{id}",
* name = "get_poll",
* requirements = {"id"="\d+"}
* )
*/
public function getPollConfig( Poll $poll ) {
return $this->json( [
'message' => 'your poll config',
'data' => $poll,
] );
}
/**
* @Post(
* path = "/poll/{id}/up",
* name = "up_poll",
* requirements = {"content"="\w+"}
* )
*/
public function updatePollConfig( Poll $poll ) {
return $this->json( [
'message' => 'you updated the poll',
] );
}
/**
* @Post(
* path = "/comment/new",
* name = "new_comment",
* requirements = {"content"="\w+"}
* )
*/
public function newCommentAction() {
return $this->json( [
'message' => 'you created a comment',
] );
}
public function deletePollAction() {
return $this->json( [
'message' => 'boom',
] );
}
public function deletePollCommentsAction() {
return $this->json( [
'message' => 'boom',
] );
}
public function deletePollVotesAction() {
return $this->json( [
'message' => 'boom',
] );
}
}