From f3d7b0773d3049ab4f6a4e84793b43e2af333a03 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Tue, 12 Nov 2019 11:44:09 +0100 Subject: [PATCH] delete actions and responses codes --- src/Controller/DefaultController.php | 136 +++++++++++++++++++++------ 1 file changed, 108 insertions(+), 28 deletions(-) diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 8fd3a6f..2582301 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -4,8 +4,10 @@ namespace App\Controller; use App\Entity\Owner; use App\Entity\Poll; +use FOS\RestBundle\Controller\Annotations\Delete; use FOS\RestBundle\Controller\Annotations\Get; use FOS\RestBundle\Controller\Annotations\Post; +use FOS\RestBundle\Controller\Annotations\Put; use FOS\RestBundle\Controller\Annotations\Route; use JMS\Serializer\SerializerBuilder; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -56,7 +58,8 @@ class DefaultController extends AbstractController { return $this->json( [ 'message' => 'here are your polls', 'data' => $data, - ] ); + ], + 200 ); } /** @@ -114,7 +117,7 @@ class DefaultController extends AbstractController { 'data' => $newpoll, ], - 203 ); + 201 ); } @@ -129,7 +132,8 @@ class DefaultController extends AbstractController { return $this->json( [ 'message' => 'here are your comments of the poll', 'data' => $poll->getComments(), - ] ); + ], + 200 ); } /** @@ -143,17 +147,24 @@ class DefaultController extends AbstractController { return $this->json( [ 'message' => 'your poll config', 'data' => $poll, - ] ); + ], + 200 ); } /** - * @Post( - * path = "/poll/{id}/up", - * name = "up_poll", - * requirements = {"content"="\w+"} + * @Put( + * path = "/poll/{id}", + * name = "update_poll", + * requirements = {"content"="\w+", "poll_id"="\d+"} * ) */ - public function updatePollConfig( Poll $poll ) { + public function updatePollConfig( Poll $poll, Request $request ) { + + + $em = $this->getDoctrine()->getManager(); + $em->persist( $poll ); + $em->flush(); + return $this->json( [ 'message' => 'you updated the poll', ] ); @@ -161,50 +172,119 @@ class DefaultController extends AbstractController { /** * @Post( - * path = "/comment/new", + * path = "poll/{id}/comment", * name = "new_comment", * requirements = {"content"="\w+", "poll_id"="\d+"} * ) */ - public function newCommentAction( Request $request ) { + 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( Poll::class ); - $foundPoll = $em->findOneBy( [ 'id' => $data[ 'poll_id' ] ] ); + $em = $this->getDoctrine()->getRepository( Owner::class ); $foundOwner = $em->find( 10 ); $comment->setOwner( $foundOwner ) - ->setPoll( $foundPoll ); + ->setPoll( $poll ); $em = $this->getDoctrine()->getManager(); $em->persist( $comment ); -// $em->persist( $foundOwner ); $em->flush(); return $this->json( [ 'message' => 'you created a comment', - ] ); + ], + 201 ); } - public function deletePollAction() { - return $this->json( [ - 'message' => 'boom', - ] ); + /** + * @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', + ] ); + } + } - public function deletePollCommentsAction() { - return $this->json( [ - 'message' => 'boom', - ] ); + /** + * 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', + ] ); + } } - public function deletePollVotesAction() { - return $this->json( [ - 'message' => 'boom', - ] ); + /** + * @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', + ] ); + } + } }