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

update fixtures

This commit is contained in:
Tykayn 2021-04-26 13:47:44 +02:00 committed by tykayn
parent 2634228af0
commit dd1de88635
3 changed files with 75 additions and 56 deletions

View File

@ -39,40 +39,43 @@ class PollController extends EmailsController {
$polls = $data;
$titles=[];
$titles = [];
$pollData = [
'message' => 'here are your polls',
'count' => count($polls),
'count' => count( $polls ),
];
$debug=1;
$debug = 1;
if($debug){
if ( $debug ) {
foreach ( $polls as $poll ) {
$titles[] = ['title' => $poll->getTitle(),
'slug' => $poll->getCustomUrl()
$titles[] = [
'title' => $poll->getTitle(),
'slug' => $poll->getCustomUrl(),
];
}
$pollData['polls'] = $titles;
$pollData[ 'polls' ] = $titles;
}
return $this->json( $pollData);
return $this->json( $pollData );
}
/**
* @param $id
* message when the poll is not found
*
* @return JsonResponse
*/
public function notFoundPoll($id): Response{
public function notFoundPoll( $id ): Response {
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(
@ -89,7 +92,7 @@ class PollController extends EmailsController {
SerializerInterface $serializer,
$customUrl,
Request $request
): Response {
) {
$repository = $this->getDoctrine()->getRepository( Poll::class );
$poll = $repository->findOneByCustomUrl( $customUrl );
@ -119,13 +122,15 @@ class PollController extends EmailsController {
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. ' ,
'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 );
// return $this->returnPollData( $poll, $serializer );
// return $this->json($returnedPoll);
return $this->json($poll);
}
}
@ -142,12 +147,12 @@ class PollController extends EmailsController {
*
* @return JsonResponse|Response
*/
function getProtectedPoll($customUrl,$md5, SerializerInterface $serializer){
function getProtectedPoll( $customUrl, $md5, SerializerInterface $serializer ) {
$repository = $this->getDoctrine()->getRepository( Poll::class );
$poll = $repository->findOneByCustomUrl( $customUrl );
if ( ! $poll ) {
return $this->notFoundPoll($customUrl);
return $this->notFoundPoll( $customUrl );
}
if ( $poll->getPassword() === $md5 ) {
@ -156,8 +161,8 @@ class PollController extends EmailsController {
} 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),
'message' => 'this is protected by a password, your password hash "' . $md5 . '" is wrong, and you should feel bad',
'md5' => md5( $md5 ),
'data' => null,
],
403 );
@ -203,6 +208,7 @@ class PollController extends EmailsController {
return $this->json( [
'message' => 'you updated the poll ' . $poll->getTitle(),
"poll" => $poll,
],
200 );
}

View File

@ -14,6 +14,9 @@ use Doctrine\Persistence\ObjectManager;
class AppPollFixtures extends Fixture {
public const POLL_FIXTURE_ONE = 'citron-poll-fixture';
public const POLL_FIXTURE_TWO = 'aujourdhui-ou-demain';
public const POLL_FIXTURE_THREE = 'citron';
public const POLL_FIXTURE_FOUR = 'demo';
public function load( ObjectManager $manager ) {
@ -41,7 +44,6 @@ class AppPollFixtures extends Fixture {
$pollCitronOrange->setTitle( 'citron ou orange' )
->setCustomUrl('citron')
->setDescription( 'votre sorbert préféré' )
->setAdminKey( uniqid() )
->setModificationPolicy( 'nobody' )
->setPassword('le pass woute woute');
@ -120,7 +122,6 @@ class AppPollFixtures extends Fixture {
->setCustomUrl('demo')
->setDescription( 'description du sondage 2' );
$pollCitronOrange->setAdminKey( uniqid() );
$pollCitronOrange->setModificationPolicy( 'self' );
$pollCitronOrange->setMailOnComment( true );
@ -154,16 +155,16 @@ class AppPollFixtures extends Fixture {
->addChoice( $choice )
->addChoice( $choice2 )
->addChoice( $choice3 )
->setModificationPolicy( 'self' );
->setModificationPolicy( 'everybody' );
$manager->persist( $pollCitronOrange );
// poll with cartoon choices
$pollCitronOrange = new Poll();
$pollCitronOrange->setTitle( 'dessin animé préféré' )
$pollCartoons = new Poll();
$pollCartoons->setTitle( 'dessin animé préféré' )
->setCustomUrl('dessin-anime')
->setDescription( 'choisissez votre animé préféré' )
->setOwner( $owner )
->setModificationPolicy( 'self' )
->setModificationPolicy( 'nobody' )
->addTextChoiceArray( [
"Vic le viking",
"Boumbo petite automobile",
@ -179,61 +180,72 @@ class AppPollFixtures extends Fixture {
->setPseudo('un gens qui commente')
->setText( "allez boumbo!" )
->setOwner( $commenterMan );
$pollCitronOrange->addComment( $someoneComment );
$pollCartoons->addComment( $someoneComment );
$someoneComment2 = new Comment();
$someoneComment2
->setPseudo('un gens qui commente')
->setText( "je suis pour la team rocket de digimon" )
->setOwner( $owner );
$pollCitronOrange->addComment( $someoneComment2 );
$pollCartoons->addComment( $someoneComment2 );
$manager->persist( $pollCitronOrange );
$manager->persist( $pollCartoons );
$stack = new StackOfVotes();
$stack->setPseudo( 'Wulfila' );
$stack
->setPoll( $pollCitronOrange )
->setPoll( $pollCartoons )
->setOwner( $voter );
$pollCartoons->addStackOfVote($stack);
$vote = new Vote();
$vote
->setPoll( $pollCitronOrange )
->setPoll( $pollCartoons )
->setStacksOfVotes( $stack )
->setValue( "yes" )
->setChoice( $pollCitronOrange->getChoices()[ 2 ] );
->setChoice( $pollCartoons->getChoices()[ 2 ] );
$pollCartoons->addVote($vote);
$vote = new Vote();
$vote
->setPoll( $pollCitronOrange )
->setPoll( $pollCartoons )
->setStacksOfVotes( $stack )
->setValue( "maybe" )
->setChoice( $pollCitronOrange->getChoices()[ 1 ] );
->setChoice( $pollCartoons->getChoices()[ 1 ] );
$pollCartoons->addVote($vote);
$manager->persist( $stack );
$stack = new StackOfVotes();
$stack->setPseudo( 'Tykayn' );
$stack
->setPoll( $pollCitronOrange )
->setPoll( $pollCartoons )
->setOwner( $voter );
$vote = new Vote();
$vote
->setPoll( $pollCitronOrange )
->setPoll( $pollCartoons )
->setStacksOfVotes( $stack )
->setValue( "yes" )
->setChoice( $pollCitronOrange->getChoices()[ 1 ] );
->setChoice( $pollCartoons->getChoices()[ 1 ] );
$pollCartoons->addVote($vote);
$vote = new Vote();
$vote
->setPoll( $pollCitronOrange )
->setPoll( $pollCartoons )
->setStacksOfVotes( $stack )
->setValue( "yes" )
->setChoice( $pollCitronOrange->getChoices()[ 2 ] );
->setChoice( $pollCartoons->getChoices()[ 2 ] );
$pollCartoons->addVote($vote);
$vote = new Vote();
$vote
->setPoll( $pollCitronOrange )
->setPoll( $pollCartoons )
->setStacksOfVotes( $stack )
->setValue( "no" )
->setChoice( $pollCitronOrange->getChoices()[ 2 ] );
->setChoice( $pollCartoons->getChoices()[ 2 ] );
$pollCartoons->addVote($vote);
$manager->persist( $pollCartoons );
$manager->persist( $stack );

View File

@ -79,7 +79,7 @@ class Poll {
* @Serializer\Type("boolean")
* @Serializer\Expose()
*/
public $votesAllowed;
public $votesAllowed = true;
/**
* max number of stack of votes possible.
* limits the number of people who can answer. as long as you trust the people to give only one answer with a reliable system.
@ -87,7 +87,7 @@ class Poll {
* @Serializer\Type("smallint")
* @Serializer\Expose()
*/
public $votesMax;
public $votesMax = 1024;
/**
* max number of choices people can answer in a stack of vote. for text polls only, not date kind.
@ -97,7 +97,7 @@ class Poll {
* @Serializer\Type("smallint")
* @Serializer\Expose()
*/
public $choicesMax;
public $choicesMax = -1;
/**
* people can add comments
@ -105,7 +105,7 @@ class Poll {
* @Serializer\Type("boolean")
* @Serializer\Expose()
*/
public $commentsAllowed;
public $commentsAllowed = true;
/**
* kind of way the people can modify the poll
@ -116,35 +116,35 @@ class Poll {
* @Serializer\Type("string")
* @Serializer\Expose()
*/
public $modificationPolicy = 'nobody';
public $modificationPolicy = 'everybody';
/**
* send a mail on a new comment
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
* @Serializer\Expose()
*/
public $mailOnComment;
public $mailOnComment = true;
/**
* send a mail on a new vote
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
* @Serializer\Expose()
*/
public $mailOnVote;
public $mailOnVote = false;
/**
* hide publicly results
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
* @Serializer\Expose()
*/
public $hideResults;
public $hideResults = false;
/**
* show publicly results even if there is a password to access the vote
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
* @Serializer\Expose()
*/
public $showResultEvenIfPasswords;
public $showResultEvenIfPasswords = false;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="poll", orphanRemoval=true, cascade={"persist", "remove"})
* @Serializer\Type("ArrayCollection<App\Entity\Vote>")
@ -247,22 +247,23 @@ class Poll {
}
public function __construct() {
$this->votes = new ArrayCollection();
$this->stacksOfVotes = new ArrayCollection();
$this->choices = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->initiate();
}
private function initiate() {
$this->adminKey = $this->generateAdminKey();
$this->votes = new ArrayCollection();
$this->stacksOfVotes = new ArrayCollection();
$this->choices = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->setAdminKey( $this->generateAdminKey() );
$this->setCreationDate( new \DateTime() );
$this->setExpiracyDate( $this->addDaysToDate(
new \DateTime(),
$this->defaultExpiracyDaysFromNow
) );
$this->setAllowedAnswers( [ 'yes' ] );
$this->setAllowedAnswers( [ 'yes', 'maybe', 'no' ] );
}
@ -324,7 +325,7 @@ class Poll {
}
public function addDaysToDate( \DateTime $date, int $days ) {
$st = strtotime( $date->getTimestamp() . ' + ' . $days . ' days' );
$st = strtotime( $date->getTimestamp() );
return new \DateTime( $st );
}