add all routes existing
parent
a8c70f3a5c
commit
e1ee9c9ac8
@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Comment;
|
||||
use App\Entity\Owner;
|
||||
use App\Entity\Poll;
|
||||
use DateTime;
|
||||
use FOS\RestBundle\Controller\Annotations\Delete;
|
||||
use FOS\RestBundle\Controller\Annotations\Get;
|
||||
use FOS\RestBundle\Controller\Annotations\Post;
|
||||
use FOS\RestBundle\Controller\Annotations\Route;
|
||||
use JMS\Serializer\SerializerBuilder;
|
||||
use JMS\Serializer\SerializerInterface;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Class DefaultController
|
||||
* @package App\Controller
|
||||
* @Route("/api/v1",name="api_")
|
||||
*/
|
||||
class CommentController extends FramadateController {
|
||||
|
||||
/**
|
||||
* @Get(
|
||||
* path = "/poll/{id}/comments",
|
||||
* name = "get_poll_comment",
|
||||
* requirements = {"id"="\d+"}
|
||||
* )
|
||||
*/
|
||||
public
|
||||
function getPollCommentsAction(
|
||||
SerializerInterface $serializer,
|
||||
Poll $poll
|
||||
) {
|
||||
$jsonResponse = $serializer->serialize([
|
||||
'message' => 'here are your comments of the poll',
|
||||
'data' => $poll->getComments()], 'json');
|
||||
|
||||
$response = new Response($jsonResponse);
|
||||
$response->headers->set('Content-Type', 'application/json');
|
||||
$response->setStatusCode(200);
|
||||
|
||||
return $response;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* add a comment on a poll
|
||||
* @Post(
|
||||
* path = "/poll/{id}/comment",
|
||||
* name = "new_comment",
|
||||
* requirements = {"content"="\w+", "id"="\d+"}
|
||||
* )
|
||||
*/
|
||||
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' );
|
||||
|
||||
$em = $this->getDoctrine()->getRepository( Owner::class );
|
||||
|
||||
$data = json_decode( $data, true );
|
||||
if(!isset($data['email'])) {
|
||||
return $this->json(["message" => "Incorrect JSON in request"], 400);
|
||||
}
|
||||
|
||||
$foundOwner = $em->findOneByEmail( $data[ 'email' ] );
|
||||
// manage existing or new Owner
|
||||
if ( ! $foundOwner ) {
|
||||
$foundOwner = new Owner();
|
||||
$foundOwner->setPseudo( $data[ 'email' ] )
|
||||
->setEmail( $data[ 'email' ] )
|
||||
->setModifierToken( uniqid( '', true ) );
|
||||
}
|
||||
// anti flood
|
||||
$seconds_limit_lastpost = 5;
|
||||
$emComment = $this->getDoctrine()->getRepository( Comment::class );
|
||||
$lastCommentOfOwner = $emComment->findBy( [ 'owner' => $foundOwner ], [ 'id' => 'desc' ] );
|
||||
|
||||
// TODO anti flood by session / IP
|
||||
|
||||
if ( $lastCommentOfOwner ) {
|
||||
|
||||
|
||||
// check time of last comment
|
||||
$now = new DateTime();
|
||||
$now = $now->format( 'Y-m-d H:i:s' );
|
||||
$date_first = strtotime( $lastCommentOfOwner[ 0 ]->getCreatedAt()->format( 'Y-m-d H:i:s' ) );
|
||||
$date_second = strtotime( $now );
|
||||
|
||||
if ( ( $date_second - $date_first ) < $seconds_limit_lastpost ) {
|
||||
return $this->json( [
|
||||
'message' => 'anti flood déclenché',
|
||||
'details' => 'votre deriner commentaire a été envoyé il y a moins de ' . $seconds_limit_lastpost . ' secondes',
|
||||
],
|
||||
403 );
|
||||
}
|
||||
|
||||
// check similar text content
|
||||
if ( $lastCommentOfOwner[ 0 ]->getText() == $comment->getText() ) {
|
||||
return $this->json( [
|
||||
'message' => 'anti flood déclenché',
|
||||
'details' => 'votre deriner commentaire a exactement le même contenu que celui ci, il n\'a donc pas été créé',
|
||||
],
|
||||
403 );
|
||||
}
|
||||
}
|
||||
$comment->setOwner( $foundOwner )
|
||||
->setCreatedAt( new DateTime() )
|
||||
->setPoll( $poll );
|
||||
$foundOwner->addComment( $comment );
|
||||
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$em->persist( $foundOwner );
|
||||
$em->persist( $comment );
|
||||
$em->flush();
|
||||
|
||||
if($poll->getMailOnComment()){
|
||||
$this->sendCommentNotificationAction($foundOwner, $comment);
|
||||
}
|
||||
|
||||
|
||||
return $this->json( [
|
||||
'message' => 'you created a comment',
|
||||
'data' => [
|
||||
'your_comment' => $comment->display(),
|
||||
],
|
||||
],
|
||||
201 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Erase all comments of a poll
|
||||
* @Delete(
|
||||
* path = "/poll/{id}/comments",
|
||||
* name = "poll_comments_delete",
|
||||
* requirements = {"accessToken"="\w+", "id"="\d+"}
|
||||
* )
|
||||
*
|
||||
* @param Poll $poll
|
||||
* @param $accessToken
|
||||
*
|
||||
* @return 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',
|
||||
] );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Poll;
|
||||
use App\Form\PollType;
|
||||
use App\Repository\PollRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
||||
|
||||
/**
|
||||
* @Route("/poll")
|
||||
*/
|
||||
class PollController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @Route("/", name="poll_index", methods={"GET"})
|
||||
*/
|
||||
public function index(PollRepository $pollRepository): Response
|
||||
{
|
||||
return $this->render('poll/index.html.twig', [
|
||||
'polls' => count($pollRepository->findAll()),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/new", name="poll_new", methods={"GET","POST"})
|
||||
*/
|
||||
public function new(Request $request): Response
|
||||
{
|
||||
$poll = new Poll();
|
||||
$form = $this->createForm(PollType::class, $poll);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$entityManager = $this->getDoctrine()->getManager();
|
||||
$entityManager->persist($poll);
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('poll_index');
|
||||
}
|
||||
|
||||
return $this->render('poll/new.html.twig', [
|
||||
'poll' => $poll,
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* on cherche un sondage par son url personnalisée
|
||||
* @Route("/{id}", name="poll_show", methods={"GET"})
|
||||
*/
|
||||
public function show($id): Response
|
||||
{
|
||||
$repository = $this->getDoctrine()->getRepository(Poll::class);
|
||||
$foundPoll = $repository->findOneByCustomUrl($id);
|
||||
if(!$foundPoll){
|
||||
return $this->json([
|
||||
'message' => $id.' : not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
return $this->render('poll/show.html.twig', [
|
||||
'poll' => $foundPoll,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/{id}/edit", name="poll_edit", methods={"GET","POST"})
|
||||
*/
|
||||
public function edit(Request $request, Poll $poll): Response
|
||||
{
|
||||
$form = $this->createForm(PollType::class, $poll);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$this->getDoctrine()->getManager()->flush();
|
||||
|
||||
return $this->redirectToRoute('poll_index');
|
||||
}
|
||||
|
||||
return $this->render('poll/edit.html.twig', [
|
||||
'poll' => $poll,
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/{id}", name="poll_delete", methods={"DELETE"})
|
||||
*/
|
||||
public function delete(Request $request, Poll $poll): Response
|
||||
{
|
||||
if ($this->isCsrfTokenValid('delete'.$poll->getId(), $request->request->get('_token'))) {
|
||||
$entityManager = $this->getDoctrine()->getManager();
|
||||
$entityManager->remove($poll);
|
||||
$entityManager->flush();
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('poll_index');
|
||||
}
|
||||
}
|
@ -0,0 +1,474 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\api;
|
||||
|
||||
use App\Controller\FramadateController;
|
||||
use App\Entity\Choice;
|
||||
use App\Entity\Owner;
|
||||
use App\Entity\Poll;
|
||||
use JMS\Serializer\Exception\RuntimeException;
|
||||
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;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
||||
|
||||
use FOS\RestBundle\Controller\Annotations\Get;
|
||||
use FOS\RestBundle\Controller\Annotations\Put;
|
||||
use FOS\RestBundle\Controller\Annotations\Delete;
|
||||
use FOS\RestBundle\Controller\Annotations\Post;
|
||||
use FOS\RestBundle\Controller\Annotations\Route;
|
||||
|
||||
/**
|
||||
* Class DefaultController
|
||||
* @package App\Controller
|
||||
* @Route("/api/v1/poll",name="api_")
|
||||
*/
|
||||
class PollController extends FramadateController {
|
||||
/**
|
||||
* @Get(
|
||||
* path = "/",
|
||||
* name = "get_all_polls"
|
||||
* )
|
||||
*/
|
||||
public function getAllPollsAction() {
|
||||
$repository = $this->getDoctrine()->getRepository( Poll::class );
|
||||
$data = $repository->findAll();
|
||||
|
||||
|
||||
return $this->json( [
|
||||
'message' => 'here are your polls',
|
||||
'poll' => count( $data ),
|
||||
] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* message when the poll is not found
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function notFoundPoll($id){
|
||||
return $this->json( [
|
||||
'message' => $id . ' : poll not found',
|
||||
],
|
||||
404 );
|
||||
}
|
||||
/**
|
||||
* get a poll config by its custom URL, we do not want polls to be reachable by their numeric id
|
||||
* @Get(
|
||||
* path = "/{id}",
|
||||
* name = "get_poll",
|
||||
* requirements = {"id"="\w+"}
|
||||
* )
|
||||
*
|
||||
* @param SerializerInterface $serializer
|
||||
* @param Request $request
|
||||
*
|
||||
* @return JsonResponse|Response
|
||||
*/
|
||||
public function getPollConfig(
|
||||
SerializerInterface $serializer,
|
||||
$id,
|
||||
Request $request
|
||||
) {
|
||||
$repository = $this->getDoctrine()->getRepository( Poll::class );
|
||||
$poll = $repository->findOneByCustomUrl( $id );
|
||||
|
||||
if ( ! $poll ) {
|
||||
return $this->notFoundPoll($id);
|
||||
}
|
||||
|
||||
$comments = $poll->getComments();
|
||||
$pass = $poll->getPassword();
|
||||
|
||||
$returnedPoll = [
|
||||
'message' => 'your poll config for ' . $poll->getTitle(),
|
||||
'password_protected' => $pass ? 'yes' : 'no',
|
||||
// TODO do not render sub objects of owner, it returns too many things
|
||||
'poll' => $poll,
|
||||
'stacks_count' => count( $poll->getStacksOfVotes() ),
|
||||
'stacks' => $poll->getStacksOfVotes(),
|
||||
'choices_count' => $poll->computeAnswers(),
|
||||
'choices' => $poll->getChoices(),
|
||||
'comments' => $comments,
|
||||
'comments_count' => count( $comments ),
|
||||
];
|
||||
|
||||
/**
|
||||
* password protected content
|
||||
*/
|
||||
if ( $pass ) {
|
||||
// no password possibly given by this route
|
||||
return $this->json( [
|
||||
'message' => 'this is protected by a password,but you did not provide the encoded password parameter, and you should feel bad. ' ,
|
||||
],
|
||||
403 );
|
||||
|
||||
} else {
|
||||
// free access to poll
|
||||
return $this->returnPollData( $poll, $serializer );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* get a poll config by its custom URL, we do not want polls to be reachable by their numeric id
|
||||
* @Get(
|
||||
* path = "/{id}/pass/{md5}",
|
||||
* name = "get_protected_poll",
|
||||
* requirements = {"id"="\w+"}
|
||||
* )
|
||||
*
|
||||
* @param SerializerInterface $serializer
|
||||
* @param Request $request
|
||||
*
|
||||
* @return JsonResponse|Response
|
||||
*/
|
||||
function getProtectedPoll($id,$md5, SerializerInterface $serializer){
|
||||
$repository = $this->getDoctrine()->getRepository( Poll::class );
|
||||
$poll = $repository->findOneByCustomUrl( $id );
|
||||
|
||||
if ( ! $poll ) {
|
||||
return $this->notFoundPoll($id);
|
||||
}
|
||||
|
||||
if ( $poll->getPassword() === $md5 ) {
|
||||
// good matching pass
|
||||
return $this->returnPollData( $poll, $serializer );
|
||||
} else {
|
||||
// wrong pass
|
||||
return $this->json( [
|
||||
'message' => 'this is protected by a password, your password "' . $md5 . '" is wrong, and you should feel bad',
|
||||
'md5' => md5($md5),
|
||||
'data' => null,
|
||||
],
|
||||
403 );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
function returnPollData( $poll, $serializer ) {
|
||||
$jsonResponse = $serializer->serialize( $poll, 'json' );
|
||||
|
||||
$response = new Response( $jsonResponse );
|
||||
$response->headers->set( 'Content-Type', 'application/json' );
|
||||
$response->setStatusCode( 200 );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Put(
|
||||
* path = "/{id}/{token}",
|
||||
* name = "update_poll",
|
||||
* requirements = {"content"="\w+", "poll_id"="\d+"}
|
||||
* )
|
||||
*/
|
||||
public function updatePollConfig(
|
||||
Poll $poll,
|
||||
string $token,
|
||||
Request $request
|
||||
) {
|
||||
if ( $poll->getAdminKey() !== $token ) {
|
||||
return $this->json( [
|
||||
'message' => 'you are NOT allowed to update the poll ' . $poll->getTitle(),
|
||||
],
|
||||
403 );
|
||||
}
|
||||
|
||||
// TODO check validity of request
|
||||
// update only if we have the admin key
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$em->persist( $poll );
|
||||
$em->flush();
|
||||
|
||||
return $this->json( [
|
||||
'message' => 'you updated the poll ' . $poll->getTitle(),
|
||||
],
|
||||
200 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @Post(
|
||||
* path = "/",
|
||||
* name = "new_poll",
|
||||
* requirements = {"creator"="\w+"}
|
||||
* )
|
||||
* @param Request $request
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function newPollAction( Request $request ) {
|
||||
|
||||
$data = $request->getContent();
|
||||
|
||||
$serializer = SerializerBuilder::create()->build();
|
||||
try {
|
||||
$newpoll = $serializer->deserialize( $data, 'App\Entity\Poll', 'json' );
|
||||
} catch ( RuntimeException $e ) {
|
||||
return $this->json( [ "message" => "Incorrect JSON in request" ], 400 );
|
||||
}
|
||||
$newpoll
|
||||
->setAdminKey( $newpoll->generateAdminKey() )
|
||||
->setCreationDate( new DateTime() )
|
||||
->setModificationPolicy( 'nobody' );
|
||||
$timeStamp = time() + ( 3600 * 24 * 90 ); // 90 days by default
|
||||
$newpoll->setExpiracyDate( ( new DateTime() )->setTimestamp( $timeStamp ),
|
||||
new DateTimeZone( 'Europe/Paris' ) );
|
||||
$data = json_decode( $data, true );
|
||||
$em = $this->getDoctrine()->getRepository( Owner::class );
|
||||
$foundOwner = $em->findOneBy( [ 'email' => $data[ 'owner' ][ 'email' ] ] );
|
||||
|
||||
|
||||
$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();
|
||||
$em->persist( $newpoll );
|
||||
$em->persist( $foundOwner );
|
||||
|
||||
// emails
|
||||
$newpoll->setMailOnComment( true );
|
||||
$newpoll->setMailOnVote( true );
|
||||
$newpoll->setHideResults( false );
|
||||
// possible answers
|
||||
$newpoll->setAllowedAnswers( [ 'yes' ] );
|
||||
if ( $data[ 'voteChoices' ] ) {
|
||||
switch ( $data[ 'voteChoices' ] ) {
|
||||
case "only_yes":
|
||||
default:
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
// setup the password, converting the raw with md5 hash
|
||||
if ( $data[ 'password' ] ) {
|
||||
$newpoll->setPassword( $data[ 'password' ] );
|
||||
}
|
||||
// manage choices
|
||||
// text kind of answers, dates are below
|
||||
if ( $data[ 'pollType' ] == 'classic' ) {
|
||||
$choices = $data[ 'dateList' ];
|
||||
foreach ( $choices as $c ) {
|
||||
$newChoice = new Choice();
|
||||
$newChoice
|
||||
->setPoll( $newpoll )
|
||||
// ->setUrl( $c[ 'url' ] )
|
||||
->setName( $c[ 'literal' ] );
|
||||
$em->persist( $newChoice );
|
||||
// TODO add also choices for each time range in a day
|
||||
}
|
||||
} elseif ( $data[ 'pollType' ] == 'dates' ) {
|
||||
if ( $data[ 'allowSeveralHours' ] == true ) {
|
||||
// different hours spans
|
||||
$choices = $data[ 'dateList' ];
|
||||
} else {
|
||||
//TODO (Sébastien) I assume this shouldn't be empty ?
|
||||
// all days have the same hour spans
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
$em->persist( $newpoll );
|
||||
$em->flush();
|
||||
$precision = '';
|
||||
if ( $userWasFound ) {
|
||||
$precision = 'from an existing user : ' . $foundOwner->getEmail();
|
||||
}
|
||||
|
||||
$this->sendCreationMailAction( $foundOwner, $newpoll );
|
||||
|
||||
return $this->json( [
|
||||
'message' => 'you created a poll ' . $precision,
|
||||
'poll' => $newpoll,
|
||||
'password_protected' => is_string( $newpoll->getPassword() ),
|
||||
'admin_key' => $newpoll->getAdminKey(),
|
||||
'owner_modifier_token' => $foundOwner->getModifierToken(),
|
||||
|
||||
],
|
||||
201 );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @Get(
|
||||
* path = "/mail/test-mail-poll/{emailChoice}",
|
||||
* name = "test-mail-poll",
|
||||
* )
|
||||
*
|
||||
* send the creation mail to owner
|
||||
*
|
||||
* @param Owner $admin_user
|
||||
* @param Poll $poll
|
||||
* @param Swift_Mailer $mailer
|
||||
*
|
||||
* @return int
|
||||
* not that the email tktest_commentateur@tktest.com does not really exist
|
||||
*/
|
||||
// public function sendCreationMailAction( Owner $admin_user, Poll $poll, \Swift_Mailer $mailer) {
|
||||
public function testSendCreationMailAction(
|
||||
$emailChoice = 'tktest_commentateur@tktest.com'
|
||||
) {
|
||||
$em = $this->getDoctrine()->getRepository( Owner::class );
|
||||
$foundOwner = $em->findOneByEmail( $emailChoice );
|
||||
if ( $foundOwner ) {
|
||||
$poll = $foundOwner->getPolls()[ 0 ];
|
||||
$comment = $foundOwner->getComments()[ 0 ];
|
||||
|
||||
$sent = $this->sendOwnerPollsAction( $foundOwner, $poll );
|
||||
if ( $sent ) {
|
||||
return $this->json( [ "message" => "test email sent to " . $foundOwner->getEmail() . "!" ], 200 );
|
||||
}
|
||||
}
|
||||
|
||||
return $this->json( [ "message" => "user with this email was not found" ], 400 );
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Delete(
|
||||
* path = "/{id}",
|
||||
* name = "poll_delete",
|
||||
* requirements = {"accessToken"="\w+", "poll_id"="\d+"}
|
||||
* )
|
||||
* @param Poll $poll
|
||||
* @param $accessToken
|
||||
*
|
||||
* @return 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',
|
||||
] );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a slug is already taken by a poll
|
||||
* @Get(
|
||||
* path = "/slug/{slug}",
|
||||
* name = "check_slug_is_unique",
|
||||
* )
|
||||
*/
|
||||
public function checkSlugIsUniqueAction( string $slug ) {
|
||||
$emPoll = $this->getDoctrine()->getRepository( Poll::class );
|
||||
$found = $emPoll->findOneByCustomUrl( $slug );
|
||||
$elaborated_message_version = false;
|
||||
|
||||
if ( $found ) {
|
||||
if ( ! $elaborated_message_version ) {
|
||||
return $this->json( null,
|
||||
204 );
|
||||
}
|
||||
|
||||
// we should use an other slug
|
||||
return $this->json( [
|
||||
'message' => ' NO, this slug is already taken on this Framadate instance ',
|
||||
'data' => [
|
||||
'slug' => $slug,
|
||||
],
|
||||
],
|
||||
204 );
|
||||
}
|
||||
if ( ! $elaborated_message_version ) {
|
||||
return $this->json( null,
|
||||
404 );
|
||||
}
|
||||
|
||||
return $this->json( [
|
||||
'message' => ' yes this slug is available on this Framadate instance ',
|
||||
'data' => [
|
||||
'slug' => $slug,
|
||||
],
|
||||
],
|
||||
404 );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Admin poll config
|
||||
* @Get(
|
||||
* path = "/admin/{token}",
|
||||
* name = "get_admin_config",
|
||||
* )
|
||||
*
|
||||
* @param SerializerInterface $serializer
|
||||
* @param $token
|
||||
*
|
||||
* @return JsonResponse|Response
|
||||
*/
|
||||
public function getAdministrationConfig( SerializerInterface $serializer, $token ) {
|
||||
$emPoll = $this->getDoctrine()->getRepository( Poll::class );
|
||||
$pollFound = $emPoll->findOneByAdminKey( $token );
|
||||
|
||||
if ( $pollFound ) {
|
||||
|
||||
$poll = $pollFound;
|
||||
$comments = $poll->getComments();
|
||||
$stacks = $poll->getStacksOfVotes();
|
||||
|
||||
$returnedPoll = [
|
||||
'message' => 'your poll config',
|
||||
'poll' => $poll,
|
||||
'stacks_count' => count( $stacks ),
|
||||
'stacks' => $stacks,
|
||||
'choices_count' => $poll->computeAnswers(),
|
||||
'choices' => $poll->getChoices(),
|
||||
'comments' => $comments,
|
||||
'comments_count' => count( $comments ),
|
||||
'token' => $token,
|
||||
];
|
||||
|
||||
$jsonResponse = $serializer->serialize( $returnedPoll, 'json' );
|
||||
|
||||
$response = new Response( $jsonResponse );
|
||||
$response->headers->set( 'Content-Type', 'application/json' );
|
||||
$response->setStatusCode( 200 );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
return $this->json( [
|
||||
'message' => 'You are not allowed to do anything with this token',
|
||||
'data' => [
|
||||
'token' => $token,
|
||||
],
|
||||
],
|
||||
403 );
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,145 @@
|
||||
#!/bin/bash
|
||||
# script d'update de framadate api, fait pour fonctionner avec le sous module git funky framadate
|
||||
|
||||
|
||||
# bash colors
|
||||
cecho() {
|
||||
local code="\033["
|
||||
case "$1" in
|
||||
black | bk) color="${code}0;30m";;
|
||||
red | r) color="${code}1;31m";;
|
||||
green | g) color="${code}1;32m";;
|
||||
yellow | y) color="${code}1;33m";;
|
||||
blue | b) color="${code}1;34m";;
|
||||
purple | p) color="${code}1;35m";;
|
||||
cyan | c) color="${code}1;36m";;
|
||||
gray | gr) color="${code}0;37m";;
|
||||
*) local text="$1"
|
||||
esac
|
||||
[ -z "$text" ] && local text="$color$2${code}0m"
|
||||
echo -e "$text"
|
||||
}
|
||||
|
||||
cecho g "######################"
|
||||
cecho g " time to update the framadate setup"
|
||||
cecho g "######################"
|
||||
|
||||
COMMAND_BUILD="build:prod:demobliss"
|
||||
# git reset --hard
|
||||
git pull origin master
|
||||
|
||||
composer install
|
||||
php bin/console doctrine:schema:update --force
|
||||
|
||||
cecho g "######################"
|
||||
cecho g " update the funky frontend submodule "
|
||||
cecho g "############################################"
|
||||
cecho g " verification des besoins de l'application "
|
||||
cecho g "############################################"
|
||||
cecho g " yarn, git, php, node et une base de données"
|
||||
cecho g "############################################${reset}"
|
||||
# True if $1 is an executable in $PATH
|
||||
# Works in both {ba,z}sh
|
||||
function is_bin_in_path {
|
||||
if [[ -n $ZSH_VERSION ]]; then
|
||||
builtin whence -p "$1" &> /dev/null
|
||||
else # bash:
|
||||
builtin type -P "$1" &> /dev/null
|
||||
fi
|
||||
}
|
||||
|
||||
if [ ! type yarn &> /dev/null ]; then
|
||||
cecho red " ❌ la commande yarn est introuvable"
|
||||
exit 1
|
||||
fi
|
||||
cecho g " ✅ yarn"
|
||||
if [ ! type node &> /dev/null ]; then
|
||||
cecho red " ❌ la commande node est introuvable"
|
||||
exit 1
|
||||
fi
|
||||
cecho g " ✅ node"
|
||||
if [ ! type git &> /dev/null ]; then
|
||||
cecho g " ❌ la commande git est introuvable"
|
||||
exit 1
|
||||
fi
|
||||
cecho g " ✅ git"
|
||||
|
||||
if [ ! -f .env ]; then
|
||||
cecho g " ❌ fichier d'environnement de symfony /.env"
|
||||
exit 1
|
||||
fi
|
||||
cecho g " ✅ fichier d'environnement de symfony /.env ou /.env.local"
|
||||
|
||||
if [ ! -f bin/console ]; then
|
||||
cecho g " ❌ fichier console de symfony"
|
||||
exit 1
|
||||
fi
|
||||
cecho g " ✅ fichier console de symfony /bin/console "
|
||||
|
||||
|
||||
if [ ! -d "funky-framadate-front" ]; then
|
||||
# initiate sub directory for funky front
|
||||
git submodule add -f https://framagit.org/framasoft/framadate/funky-framadate-front
|
||||
git submodule init
|
||||
fi
|
||||
|
||||
git config --global diff.submodule log
|
||||
git submodule update
|
||||
cecho g "######################"
|
||||
cecho g " check dependencies of the frontend with yarn "
|
||||
cecho g "######################"
|
||||
cd funky-framadate-front
|
||||
cecho g "######################"
|
||||
cecho g " debug info : version of programs"
|
||||
cecho g " "
|
||||
cecho b " git current branch "
|
||||
git show-branch
|
||||
cecho b " node version "
|
||||
node -v
|
||||
cecho b " yarn version "
|
||||
yarn -v
|
||||
cecho g " "
|
||||
cecho b "##############################################"
|
||||
cecho b " update of the funky part, clean of local repo"
|
||||
cecho g " "
|
||||
git reset --hard
|
||||
git remote -v
|
||||
git fetch
|
||||
git pull
|
||||
yarn
|
||||
cecho g "######################"
|
||||
cecho g " building the frontend, should take around 20 seconds "
|
||||
cecho g " "
|
||||
cecho y " npm run $COMMAND_BUILD "
|
||||
cecho g " "
|
||||
cecho y " start: $(date) "
|
||||
cecho g "######################"
|
||||
npm run $COMMAND_BUILD
|
||||
|
||||
cecho g "######################"
|
||||
cecho g " copying built files in the public folder of the symfony project "
|
||||
cecho g "######################"
|
||||
cd ../public
|
||||
rm ./*.js
|
||||
rm ./*.css
|
||||
cd ../funky-framadate-front
|
||||
cp -r dist/framadate/* ../public/
|
||||
COUNT_FILES=$(ls -larth ../public |wc -l)
|
||||
cd ..
|
||||
cecho b " $COUNT_FILES fichiers de build copiés dans /public"
|
||||
cecho g "##################################################################"
|
||||
cecho b " renaming unieque name of JS chunks to common names for templates "
|
||||
cd public
|
||||
|
||||
mv runtime* runtime.js
|
||||
mv main* main.js
|
||||
mv polyfills-es5* es5-polyfills.js
|
||||
mv polyfills* other-polyfills.js
|
||||
mv scripts* scripts.js
|
||||
mv styles* styles.css
|
||||
cecho b " finished at ------- $(date) ------- "
|
||||
cecho g "##################################################################"
|
||||
cecho g " "
|
||||
cecho b " done. you can now see your homepage updated "
|
||||
cecho g " "
|
||||
cecho g "##################################################################"
|
Loading…
Reference in New Issue