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

delete actions and responses codes

This commit is contained in:
Baptiste Lemoine 2019-11-12 11:44:09 +01:00
parent 324f3e7f32
commit f3d7b0773d

View File

@ -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',
] );
}
}
}