1
0
mirror of https://framagit.org/tykayn/date-poll-api synced 2023-08-25 08:23:11 +02:00
This commit is contained in:
tykayn 2020-04-24 14:02:31 +02:00
commit 742bd5b093
5 changed files with 249 additions and 86 deletions

1
.env
View File

@ -15,6 +15,7 @@
###> symfony/framework-bundle ###
APP_ENV=dev
ADMIN_TOKEN=erfd456ref4ety4h56jy4i5opuoipm564iyuyn312b1s6er78g897ryjt7thsb32d1gfb
APP_SECRET=597b0529ac702d27dcb9089f7e69c362
# Base website url, should contain https:// and having no trailing slash. example: BASE_URL=https://framadate.org
BASE_URL=https://framadate-api.cipherbliss.com

View File

@ -0,0 +1,71 @@
<?php
namespace App\Controller;
use App\Entity\Poll;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Route;
/**
* Class DefaultController
* @package App\Controller
* @Route("/admin",name="admin_homepage")
*/
class AdminController extends FramadateController {
/**
* @Get(path ="/",
* name = "_get_default")
*/
public function indexAction() {
return $this->json( [ "message" => "welcome to the framadate admin api, ask /api/v1/doc.json for endpoints" ],
200 );
}
/**
* Delete all expired polls and their children
* @Get(
* path = "/polls/clean/{token}",
* name = "_clean_expired_polls",
* )
* token is set up in the main env file
*/
public
function cleanExpiredPolls(
string $token
) {
if ( $this->getParameter( 'ADMIN_TOKEN' ) !== $token ) {
return $this->json( [
'message' => 'clean routine can NOT be done, your admin token is bad, and you should feel bad.',
],
403 );
}
$em = $this->getDoctrine()->getManager();
$emPoll = $this->getDoctrine()->getRepository( Poll::class );
$queryFind = $em->createQuery(
'SELECT p
FROM App\Entity\Poll p
WHERE p.expiracyDate < CURRENT_DATE()'
);
$queryDelete = $em->createQuery(
'DELETE
FROM App\Entity\Poll p
WHERE p.expiracyDate < CURRENT_DATE()'
);
$foundPolls = $queryFind->getResult();
$em->flush();
return $this->json( [
'message' => 'clean routine has been done, here are the numbers of polls deleted: ' . count( $foundPolls ),
'data' => [
'count' => count( $foundPolls ),
],
],
200 );
}
}

View File

@ -16,65 +16,7 @@ use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
* @Route("/api/v1",name="api_")
*/
class DefaultController extends FramadateController {
/**
* @var MailService
*/
protected $mail_service;
/**
* Send a mail with all the data to one user
* @Get(
* path = "/send-polls-to-user/{email}",
* name = "send_user_polls"
* )
*
* @param string $email
*
* @return JsonResponse
*/
public function sendPollsToUserAction( string $email ) {
$repository = $this->getDoctrine()->getRepository( Owner::class );
// find user by email
$owner = $repository->findOneByEmail($email);
if ( $owner ) {
$templateVars = [
'owner' => $owner,
'polls' => $owner->getPolls(),
'title' => 'Mes sondages - ' . $owner->getEmail(),
];
// send email
$mailSent = 0;
try {
$mailSent = $this->sendOwnerPollsAction( $owner );
} catch ( Exception $e ) {
} catch ( TransportExceptionInterface $e ) {
}
if ( $mailSent ) {
return $this->json( [
'message' => 'mail succefully sent to user ' . $owner->getEmail(),
'data' => '',
],
200 );
}
return $this->json( [
'message' => 'no sucess sending email ' . $owner->getEmail(),
'data' => '',
],
400 );
}
return $this->json( [
'message' => 'no user found for email ' . $email,
'data' => '',
],
400 );
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace App\Controller;
use App\Entity\Owner;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Route;
use JMS\Serializer\Type\Exception\Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
/**
* Class DefaultController
* @package App\Controller
* @Route("/user",name="user_homepage")
*/
class OwnerController extends FramadateController {
/**
* @Get(path ="/",
* name = "get_default")
*/
public function indexAction() {
return $this->json( [ "message" => "welcome to the framadate user api, ask /api/v1/doc.json for endpoints" ],
200 );
}
/**
* Send a mail with all the data to one user
* @Get(
* path = "/{email}/polls/send-by-email",
* name = "_polls_send_by_email"
* )
*
* @param string $email
*
* @return JsonResponse
*/
public function sendPollsToUserAction( string $email ) {
$repository = $this->getDoctrine()->getRepository( Owner::class );
// find user by email
$owner = $repository->findOneByEmail( $email );
if ( $owner ) {
$templateVars = [
'owner' => $owner,
'polls' => $owner->getPolls(),
'title' => 'Mes sondages - ' . $owner->getEmail(),
];
// send email
$mailSent = 0;
try {
$mailSent = $this->sendOwnerPollsAction( $owner );
} catch ( Exception $e ) {
} catch ( TransportExceptionInterface $e ) {
}
if ( $mailSent ) {
return $this->json( [
'message' => 'mail succefully sent to user ' . $owner->getEmail(),
'data' => '',
],
200 );
}
return $this->json( [
'message' => 'no sucess sending email ' . $owner->getEmail(),
'data' => '',
],
400 );
}
return $this->json( [
'message' => 'no user found for email ' . $email,
'data' => '',
],
400 );
}
}

View File

@ -5,7 +5,6 @@ namespace App\Controller;
use App\Entity\Choice;
use App\Entity\Owner;
use App\Entity\Poll;
use App\Service\MailService;
use FOS\RestBundle\Controller\Annotations\Delete;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Post;
@ -107,7 +106,7 @@ class PollController extends FramadateController {
/**
* @Put(
* path = "/{id}",
* path = "/{id}/{token}",
* name = "update_poll",
* requirements = {"content"="\w+", "poll_id"="\d+"}
* )
@ -115,8 +114,15 @@ class PollController extends FramadateController {
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
@ -126,7 +132,8 @@ class PollController extends FramadateController {
return $this->json( [
'message' => 'you updated the poll ' . $poll->getTitle(),
] );
],
200 );
}
/**
@ -268,7 +275,7 @@ class PollController extends FramadateController {
$sent = $this->sendOwnerPollsAction( $foundOwner, $poll );
if ( $sent ) {
return $this->json( [ "message" => "test email sent to ".$foundOwner->getEmail()."!" ], 200 );
return $this->json( [ "message" => "test email sent to " . $foundOwner->getEmail() . "!" ], 200 );
}
}
@ -311,40 +318,97 @@ class PollController extends FramadateController {
}
/**
* Delete all expired polls and their children
* Check is a slug is already taken by a poll
* @Get(
* path = "/clean-polls",
* name = "clean_expired_polls",
* path = "/slug/{slug}",
* name = "check_slug_is_unique",
* )
*/
public
function cleanExpiredPolls() {
$em = $this->getDoctrine()->getManager();
$emPoll = $this->getDoctrine()->getRepository( Poll::class );
public function checkSlugIsUniqueAction( string $slug ) {
$emPoll = $this->getDoctrine()->getRepository( Poll::class );
$found = $emPoll->findOneByCustomUrl( $slug );
$elaborated_message_version = false;
$queryFind = $em->createQuery(
'SELECT p
FROM App\Entity\Poll p
WHERE p.expiracyDate < CURRENT_DATE()'
);
$queryDelete = $em->createQuery(
'DELETE
FROM App\Entity\Poll p
WHERE p.expiracyDate < CURRENT_DATE()'
);
$foundPolls = $queryFind->getResult();
$em->flush();
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' => 'clean routine has been done, here are the numbers of polls deleted: ' . count( $foundPolls ),
'message' => ' yes this slug is available on this Framadate instance ',
'data' => [
'count' => count( $foundPolls ),
'slug' => $slug,
],
],
200 );
404 );
}
/**
* Get Admin poll config
* @Get(
* path = "/admin/{token}",
* name = "get_admin_config",
* )
*/
public function getAdministrationConfig( $token ) {
$emPoll = $this->getDoctrine()->getRepository( Poll::class );
$pollFound = $emPoll->findOneByAdminKey( $token );
if ( $pollFound ) {
$poll = $pollFound;
$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();
}
$returnedPoll = [
'message' => 'your poll config',
'poll' => $poll,
'stacks_count' => count( $poll->getStacksOfVotes() ),
'stacks' => $stacks,
'choices_count' => $poll->computeAnswers(),
'choices' => $choices,
'comments' => $comments,
'comments_count' => count( $comments ),
'token' => $token,
];
return $this->json( $returnedPoll,
200 );
}
return $this->json( [
'message' => 'You are not allowed to do anything with this token',
'data' => [
'token' => $token,
],
],
403 );
}
}