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

356 lines
9.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Controller;
2019-11-06 12:27:46 +01:00
use App\Entity\Owner;
2019-11-05 16:31:27 +01:00
use App\Entity\Poll;
2019-11-12 11:44:09 +01:00
use FOS\RestBundle\Controller\Annotations\Delete;
use FOS\RestBundle\Controller\Annotations\Get;
2019-11-05 16:31:27 +01:00
use FOS\RestBundle\Controller\Annotations\Post;
2019-11-12 11:44:09 +01:00
use FOS\RestBundle\Controller\Annotations\Put;
2019-11-05 16:31:27 +01:00
use FOS\RestBundle\Controller\Annotations\Route;
2019-11-06 12:27:46 +01:00
use JMS\Serializer\SerializerBuilder;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2019-11-06 11:23:43 +01:00
use Symfony\Component\HttpFoundation\Request;
/**
* Class DefaultController
* @package App\Controller
2019-11-06 11:23:43 +01:00
* @Route("/api/v1",name="api_")
*/
class DefaultController extends AbstractController {
/**
* @Get(path ="/",
2019-11-05 16:31:27 +01:00
* name = "get_default")
*/
public function index() {
return $this->json( [
'message' => 'Welcome to your new controller!',
'path' => 'src/Controller/DefaultController.php',
] );
}
/**
* @Get(
* path = "/my-polls",
2019-11-05 16:31:27 +01:00
* name = "get_my_polls",
* requirements = {"access_token"="\w+"}
* )
*/
public function showMyPollsAction() {
return $this->json( [
'message' => 'here are your polls',
2019-11-05 16:31:27 +01:00
'data' => new Poll(),
] );
}
2019-11-05 16:31:27 +01:00
/**
* @Get(
* path = "/polls",
2019-11-05 16:31:27 +01:00
* name = "get_all_polls"
* )
*/
public function getAllPollsAction() {
$repository = $this->getDoctrine()->getRepository( Poll::class );
2019-11-05 17:31:07 +01:00
$data = $repository->findall();
2019-11-05 16:31:27 +01:00
return $this->json( [
'message' => 'here are your polls',
2019-11-05 17:31:07 +01:00
'data' => $data,
2019-11-12 11:44:09 +01:00
],
200 );
2019-11-05 16:31:27 +01:00
}
/**
* @Post(
* path = "/poll",
2019-11-05 16:31:27 +01:00
* name = "new_polls",
* requirements = {"creator"="\w+"}
* )
2019-11-06 11:23:43 +01:00
* @param Request $request
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
2019-11-05 16:31:27 +01:00
*/
2019-11-06 11:23:43 +01:00
public function newPollAction( Request $request ) {
$data = $request->getContent();
2019-11-05 16:31:27 +01:00
2019-11-06 12:27:46 +01:00
$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 );
2019-11-06 12:27:46 +01:00
$em = $this->getDoctrine()->getRepository( Owner::class );
$foundOwner = $em->findOneBy( [ 'email' => $data[ 'owner' ][ 'email' ] ] );
2019-11-06 12:27:46 +01:00
$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 = $this->getDoctrine()->getManager();
2019-11-06 12:27:46 +01:00
$em->persist( $newpoll );
$em->persist( $foundOwner );
2019-11-05 16:31:27 +01:00
$em->flush();
2019-11-06 12:27:46 +01:00
$precision = '';
if ( $userWasFound ) {
$precision = 'from an existing user : ' . $foundOwner->getEmail();
}
2019-11-05 16:31:27 +01:00
2019-11-06 11:23:43 +01:00
return $this->json( [
2019-11-06 12:27:46 +01:00
'message' => 'you created a poll ' . $precision,
'data' => $newpoll,
2019-11-06 12:27:46 +01:00
2019-11-06 11:23:43 +01:00
],
2019-11-12 11:44:09 +01:00
201 );
2019-11-05 16:31:27 +01:00
}
2019-11-06 14:54:04 +01:00
/**
* @Get(
* path = "/poll/{id}/comments",
* name = "get_poll_comment",
* requirements = {"poll_id"="\d+"}
2019-11-06 14:54:04 +01:00
* )
*/
public function getPollCommentsAction( Poll $poll ) {
return $this->json( [
'message' => 'here are your comments of the poll',
'data' => $poll->getComments(),
2019-11-12 11:44:09 +01:00
],
200 );
2019-11-06 14:54:04 +01:00
}
2019-11-05 16:31:27 +01:00
/**
* @Get(
* path = "/poll/{id}",
* name = "get_poll",
* requirements = {"poll_id"="\d+"}
2019-11-05 16:31:27 +01:00
* )
*/
public function getPollConfig( Poll $poll ) {
return $this->json( [
'message' => 'your poll config',
'data' => $poll,
2019-11-12 11:44:09 +01:00
],
200 );
2019-11-05 16:31:27 +01:00
}
/**
2019-11-12 11:44:09 +01:00
* @Put(
* path = "/poll/{id}",
* name = "update_poll",
* requirements = {"content"="\w+", "poll_id"="\d+"}
2019-11-05 16:31:27 +01:00
* )
*/
2019-11-12 11:44:09 +01:00
public function updatePollConfig( Poll $poll, Request $request ) {
// TODO check validity of request
// update only if we have the admin key
2019-11-12 11:44:09 +01:00
$em = $this->getDoctrine()->getManager();
$em->persist( $poll );
$em->flush();
2019-11-05 16:31:27 +01:00
return $this->json( [
'message' => 'you updated the poll ' . $poll->getTitle(),
2019-11-05 16:31:27 +01:00
] );
}
/**
2019-11-12 11:52:09 +01:00
* add a comment on a poll
2019-11-05 16:31:27 +01:00
* @Post(
2019-11-12 11:44:09 +01:00
* path = "poll/{id}/comment",
2019-11-05 16:31:27 +01:00
* name = "new_comment",
* requirements = {"content"="\w+", "poll_id"="\d+"}
2019-11-05 16:31:27 +01:00
* )
*/
2019-11-12 11:44:09 +01:00
public function newCommentAction( Poll $poll, Request $request ) {
if ( ! $poll ) {
return $this->json( [ 'message' => 'poll not found' ], 404 );
}
$data = $request->getContent();
$serializer = SerializerBuilder::create()->build();
$comment = $serializer->deserialize( $data, 'App\Entity\Comment', 'json' );
2019-11-12 11:44:09 +01:00
2019-11-12 11:52:09 +01:00
$em = $this->getDoctrine()->getRepository( Owner::class );
$data = json_decode( $data, true );
$foundOwner = $em->findByEmail( $data[ 'owner' ][ 'email' ] );
// manage existing or new Owner
if ( ! $foundOwner ) {
$foundOwner = new Owner();
$foundOwner->setPseudo( $data[ 'owner' ][ 'email' ] )
->setEmail( $data[ 'owner' ][ 'email' ] )
->setModifierToken( uniqid() );
}
$comment->setOwner( $foundOwner )
2019-11-12 11:44:09 +01:00
->setPoll( $poll );
2019-11-12 11:52:09 +01:00
$foundOwner->addComment( $comment );
$em = $this->getDoctrine()->getManager();
2019-11-12 11:52:09 +01:00
$em->persist( $foundOwner );
$em->persist( $comment );
$em->flush();
2019-11-05 16:31:27 +01:00
return $this->json( [
'message' => 'you created a comment',
2019-11-12 11:44:09 +01:00
],
201 );
2019-11-05 16:31:27 +01:00
}
2019-11-12 17:26:56 +01:00
/**
* add a comment 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 ) {
// if ( ! $poll ) {
// return $this->json( [ 'message' => 'poll not found' ], 404 );
// }
// $data = $request->getContent();
//
// $serializer = SerializerBuilder::create()->build();
// $comment = $serializer->deserialize( $data, 'App\Entity\Comment', 'json' );
//
// $em = $this->getDoctrine()->getRepository( Owner::class );
//
// $data = json_decode( $data, true );
//
// $foundOwner = $em->findByEmail( $data[ 'owner' ][ 'email' ] );
// // manage existing or new Owner
// if ( ! $foundOwner ) {
// $foundOwner = new Owner();
// $foundOwner->setPseudo( $data[ 'owner' ][ 'email' ] )
// ->setEmail( $data[ 'owner' ][ 'email' ] )
// ->setModifierToken( uniqid() );
// }
// $comment->setOwner( $foundOwner )
// ->setPoll( $poll );
// $foundOwner->addComment( $comment );
//
// $em = $this->getDoctrine()->getManager();
// $em->persist( $foundOwner );
// $em->persist( $comment );
// $em->flush();
return $this->json( [
'message' => 'you created a comment',
],
201 );
}
function newVoteAction( Poll $poll ) {
return $this->json( [
'message' => 'you voted on the poll',
],
201 );
}
2019-11-05 17:22:30 +01:00
2019-11-12 11:44:09 +01:00
/**
* @Delete(
* path = "/poll/{id}",
* name = "poll_delete",
* requirements = {"accessToken"="\w+", "poll_id"="\d+"}
* )
* @param Poll $poll
* @param $accessToken
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function deletePollAction( Poll $poll, $accessToken ) {
if ( $accessToken == $poll->getAdminKey() ) {
$em = $this->getDoctrine()->getManager();
$em->remove( $poll );
$em->flush();
return $this->json( [
'message' => 'boom! le sondage et ses objets assocités a été supprimé',
] );
} else {
return $this->json( [
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage',
] );
}
2019-11-05 17:22:30 +01:00
}
2019-11-12 11:44:09 +01:00
/**
* Erase all comments of a poll
* @Delete(
* path = "/poll/{id}/comments",
* name = "poll_comments_delete",
* requirements = {"accessToken"="\w+", "poll_id"="\d+"}
* )
*
* @param Poll $poll
* @param $accessToken
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function deletePollCommentsAction( Poll $poll, $accessToken ) {
if ( $accessToken == $poll->getAdminKey() ) {
$em = $this->getDoctrine()->getManager();
$length = count( $poll->getComments() );
$em->remove( $poll->getComments() );
$em->flush();
return $this->json( [
'message' => 'boom! les ' . $length . ' commentaires du sondage ont été supprimés',
] );
} else {
return $this->json( [
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage',
] );
}
2019-11-05 17:22:30 +01:00
}
2019-11-12 11:44:09 +01:00
/**
* @Delete(
* path = "/poll/{id}/votes",
* name = "poll_votes_delete",
* requirements = {"accessToken"="\w+", "poll_id"="\d+"}
* )
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function deletePollVotesAction( Poll $poll, $accessToken ) {
if ( $accessToken == $poll->getAdminKey() ) {
$em = $this->getDoctrine()->getManager();
$length = count( $poll->getVotes() );
$em->remove( $poll->getVotes() );
$em->flush();
return $this->json( [
'message' => 'boom! les ' . $length . ' votes du sondage ont été supprimés',
] );
} else {
return $this->json( [
'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage',
] );
}
2019-11-05 17:22:30 +01:00
}
}