📖 some doc and start to create a vote

This commit is contained in:
Baptiste Lemoine 2019-11-27 20:55:59 +01:00
parent 5d74fb9780
commit 6dd7b46e7a
7 changed files with 121 additions and 34 deletions

View File

@ -1,38 +1,51 @@
#Funky Framadate API
Experimental REST backend in symfony 4 for Funky framadate.
Experimental REST backend in symfony 4 for Funky framadate frontend.
https://framagit.org/framasoft/framadate/funky-framadate-front
***
# Dev
# Development
install dependencies with Composer
launch local server with
there are examples of request to make it all work.
## Getting started
### install the vendors
```bash
composer install
```
### initiate the database
```bash
php bin/console doctrine:schema:create
```
### launch local server with
```bash
php bin/console server:run
```
# Production
set a virtual host on your server, configure CORS access to have the API to work.
configure database access in a .env.local file , replace variables
DATABASE_URL=mysql://database_user:db_user_password@127.0.0.1:3306/database_name
this file is not versionned
initiate the database
```bash
php bin/console doctrine:schema:create
```
## cronjob to delete expired polls
add this line in your crontab to run the clearance of expired polls everyday at 0h00.
```
0 0 * * * wget http://MYWEBSITE/api/v1/clean-polls
```
you can open your crontabl in command line with
you can open your crontabl in command line with :
```
crontab -e
```
# About
made by B. Lemoine, aka Tykayn, for the framadate funky front end project, a polling libre software.
## contacts
contact@cipherbliss.com
https://mastodon.cipherbliss.com/@tykayn
https://keybase.io/tykayn
https://twitter.com/tykayn
https://cipherbliss.com
* contact@cipherbliss.com
* https://mastodon.cipherbliss.com/@tykayn
* https://keybase.io/tykayn
* https://twitter.com/tykayn
* https://cipherbliss.com

26
examples.md Normal file
View File

@ -0,0 +1,26 @@
# API example
after having setup your project and database, and having a local server running you can rest these commands
```
POST
http://127.0.0.1:8000/api/v1/poll/98/vote
Content-Type:"pplication/json
{
"pseudo": "Mario Bros",
"email": "MarioBros@tktest.com",
"votes": [{
"choice_id": 287,
"value": "yes"
},
{
"choice_id": 288,
"value": "no"
},
{
"choice_id": 289,
"value": "maybe"
}]
}
```

View File

@ -16,7 +16,7 @@ use Symfony\Component\HttpFoundation\Request;
/**
* Class DefaultController
* @package App\Controller
* @Route("/api/v1",name="api_")
* @Route("/api/v1/",name="api_")
*/
class DefaultController extends AbstractController {
/**
@ -260,9 +260,9 @@ class DefaultController extends AbstractController {
* )
*/
public function newVoteStackAction( Poll $poll, Request $request ) {
// if ( ! $poll ) {
// return $this->json( [ 'message' => 'poll not found' ], 404 );
// }
if ( ! $poll ) {
return $this->json( [ 'message' => 'poll not found' ], 404 );
}
// $data = $request->getContent();
//
// $serializer = SerializerBuilder::create()->build();

View File

@ -28,10 +28,10 @@ class AppPollFixtures extends Fixture {
->setModifierToken( uniqid() );
$poll = new Poll();
$poll->setTitle( 'citron ou orange' );
$poll->setDescription( 'votre sorbert préféré' );
$poll->setAdminKey( uniqid() );
$poll->setModificationPolicy( 'nobody' );
$poll->setTitle( 'citron ou orange' )
->setDescription( 'votre sorbert préféré' )
->setAdminKey( uniqid() )
->setModificationPolicy( 'nobody' );
$poll->setMailOnVote( true );
$poll->setOwner( $owner );
$owner->addPoll( $poll );
@ -58,13 +58,13 @@ class AppPollFixtures extends Fixture {
$voteA
->setPoll( $poll )
->setStackOfVotes( $stack1 )
->setValue( [ "yes" ] )
->setValue( "yes" )
->setChoice( $choiceA );
$voteB = new Vote();
$voteB
->setPoll( $poll )
->setStackOfVotes( $stack1 )
->setValue( [ "yes" ] )
->setValue( "yes" )
->setChoice( $choiceB );
$stack1->setPseudo( 'chuck norris' );
@ -84,7 +84,7 @@ class AppPollFixtures extends Fixture {
$voteA
->setPoll( $poll )
->setStackOfVotes( $stack2 )
->setValue( [ "maybe" ] )
->setValue( "maybe" )
->setChoice( $choiceA );
$manager->persist( $stack2 );
@ -201,6 +201,54 @@ class AppPollFixtures extends Fixture {
$manager->persist( $poll );
$stack = new StackOfVotes();
$stack->setPseudo( 'Wulfila' );
$stack
->setPoll( $poll )
->setOwner( $voter );
$vote = new Vote();
$vote
->setPoll( $poll )
->setStackOfVotes( $stack )
->setValue( "yes" )
->setChoice( $poll->getChoices()[ 2 ] );
$vote = new Vote();
$vote
->setPoll( $poll )
->setStackOfVotes( $stack )
->setValue( "maybe" )
->setChoice( $poll->getChoices()[ 1 ] );
$manager->persist( $stack );
$stack = new StackOfVotes();
$stack->setPseudo( 'Tykayn' );
$stack
->setPoll( $poll )
->setOwner( $voter );
$vote = new Vote();
$vote
->setPoll( $poll )
->setStackOfVotes( $stack )
->setValue( "yes" )
->setChoice( $poll->getChoices()[ 1 ] );
$vote = new Vote();
$vote
->setPoll( $poll )
->setStackOfVotes( $stack )
->setValue( "yes" )
->setChoice( $poll->getChoices()[ 2 ] );
$vote = new Vote();
$vote
->setPoll( $poll )
->setStackOfVotes( $stack )
->setValue( "no" )
->setChoice( $poll->getChoices()[ 2 ] );
$manager->persist( $stack );
$manager->persist( $commenterMan );
$manager->flush();

View File

@ -24,13 +24,13 @@ class Choice {
* @ORM\Column(type="string", length=255, nullable=true)
* @Serializer\Type("string")
*/
private $name;
public $name;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Serializer\Type("datetime")
*/
private $dateTime;
public $dateTime;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Poll", mappedBy="choices")
@ -41,13 +41,13 @@ class Choice {
* @ORM\OneToMany(targetEntity="App\Entity\StackOfVotes", mappedBy="choice")
* @Serializer\Type("App\Entity\StackOfVotes")
*/
private $stackOfVotes;
public $stackOfVotes;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="choice")
* @Serializer\Type("App\Entity\Vote")
*/
private $votes;
public $votes;
public function __construct() {
$this->poll = new ArrayCollection();

View File

@ -138,7 +138,7 @@ class Poll {
* number of days from now for default expiracy date
* @var int
*/
private $defaultExpiracyDaysFromNow = 60;
public $defaultExpiracyDaysFromNow = 60;
public function __construct() {

View File

@ -13,10 +13,10 @@ class Vote {
/**
* for a text kind of choice: could be "yes" "no" "maybe" and emptu.
* for a date kind, the choice linked is equivalent to the value selected
* @ORM\Column(type="array", length=255, nullable=true)
* @Serializer\Type("array")
* @ORM\Column(type="string", length=255, nullable=true)
* @Serializer\Type("string")
*/
public $value = [];
public $value;
/**
* @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"})
* @Serializer\Type("datetime")
@ -83,7 +83,7 @@ class Vote {
return $this->value;
}
public function setValue( ?array $value ): self {
public function setValue( ?string $value ): self {
$this->value = $value;
return $this;