json( [ 'message' => 'Welcome to your new controller!', 'path' => 'src/Controller/DefaultController.php', ] ); } /** * @Get( * path = "/my-polls", * name = "get_my_polls", * requirements = {"access_token"="\w+"} * ) */ public function showMyPollsAction() { return $this->json( [ 'message' => 'here are your polls', 'data' => new Poll(), ] ); } /** * @Get( * path = "/polls", * name = "get_all_polls" * ) */ public function getAllPollsAction() { $repository = $this->getDoctrine()->getRepository( Poll::class ); $data = $repository->findall(); return $this->json( [ 'message' => 'here are your polls', 'data' => $data, ], 200 ); } /** * @Post( * path = "/poll", * name = "new_polls", * requirements = {"creator"="\w+"} * ) * @param Request $request * * @return \Symfony\Component\HttpFoundation\JsonResponse */ public function newPollAction( Request $request ) { $data = $request->getContent(); $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 ); $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 ); $em->flush(); $precision = ''; if ( $userWasFound ) { $precision = 'from an existing user : ' . $foundOwner->getEmail(); } return $this->json( [ 'message' => 'you created a poll ' . $precision, 'data' => $newpoll, ], 201 ); } /** * @Get( * path = "/poll/{id}/comments", * name = "get_poll_comment", * requirements = {"poll_id"="\d+"} * ) */ public function getPollCommentsAction( Poll $poll ) { return $this->json( [ 'message' => 'here are your comments of the poll', 'data' => $poll->getComments(), ], 200 ); } /** * @Get( * path = "/poll/{id}", * name = "get_poll", * requirements = {"poll_id"="\d+"} * ) */ public function getPollConfig( Poll $poll ) { return $this->json( [ 'message' => 'your poll config', 'data' => $poll, ], 200 ); } /** * Delete all expired polls and their children * @Get( * path = "/clean-polls", * name = "clean_expired_polls", * ) */ public function cleanExpiredPolls() { $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(); var_dump( count( $foundPolls ) ); $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 ); } /** * @Put( * path = "/poll/{id}", * name = "update_poll", * requirements = {"content"="\w+", "poll_id"="\d+"} * ) */ public function updatePollConfig( Poll $poll, Request $request ) { // 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(), ] ); } /** * add a comment on a poll * @Post( * path = "poll/{id}/comment", * name = "new_comment", * requirements = {"content"="\w+", "poll_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 ); $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 ); } /** * 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(); $data = json_decode( $data, true ); $newOwner = new Owner(); $newOwner ->setEmail( $data[ 'email' ] ) ->setPseudo( $data[ 'pseudo' ] ); $stack = new StackOfVotes(); foreach ( $data[ 'votes' ] as $voteInfo ) { $vote = new Vote(); $foundChoice = $poll->findChoiceById( $voteInfo[ 'choice_id' ] ); $vote->setPoll( $poll ) ->setChoice( $foundChoice ) ->setValue( $voteInfo[ 'value' ] ); $stack->addVote( $vote ); } // find poll from choices $poll->addStackOfVote( $stack ); // // // $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( $stack ); // $em->persist( $comment ); $em->flush(); return $this->json( [ 'message' => 'you created a comment', 'json_you_sent' => $data, ], 201 ); } function newVoteAction( Poll $poll ) { return $this->json( [ 'message' => 'you voted on the poll', ], 201 ); } /** * @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', ] ); } } /** * 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', ] ); } } /** * @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', ] ); } } }