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:
parent
324f3e7f32
commit
f3d7b0773d
@ -4,8 +4,10 @@ namespace App\Controller;
|
|||||||
|
|
||||||
use App\Entity\Owner;
|
use App\Entity\Owner;
|
||||||
use App\Entity\Poll;
|
use App\Entity\Poll;
|
||||||
|
use FOS\RestBundle\Controller\Annotations\Delete;
|
||||||
use FOS\RestBundle\Controller\Annotations\Get;
|
use FOS\RestBundle\Controller\Annotations\Get;
|
||||||
use FOS\RestBundle\Controller\Annotations\Post;
|
use FOS\RestBundle\Controller\Annotations\Post;
|
||||||
|
use FOS\RestBundle\Controller\Annotations\Put;
|
||||||
use FOS\RestBundle\Controller\Annotations\Route;
|
use FOS\RestBundle\Controller\Annotations\Route;
|
||||||
use JMS\Serializer\SerializerBuilder;
|
use JMS\Serializer\SerializerBuilder;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
@ -56,7 +58,8 @@ class DefaultController extends AbstractController {
|
|||||||
return $this->json( [
|
return $this->json( [
|
||||||
'message' => 'here are your polls',
|
'message' => 'here are your polls',
|
||||||
'data' => $data,
|
'data' => $data,
|
||||||
] );
|
],
|
||||||
|
200 );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -114,7 +117,7 @@ class DefaultController extends AbstractController {
|
|||||||
'data' => $newpoll,
|
'data' => $newpoll,
|
||||||
|
|
||||||
],
|
],
|
||||||
203 );
|
201 );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,7 +132,8 @@ class DefaultController extends AbstractController {
|
|||||||
return $this->json( [
|
return $this->json( [
|
||||||
'message' => 'here are your comments of the poll',
|
'message' => 'here are your comments of the poll',
|
||||||
'data' => $poll->getComments(),
|
'data' => $poll->getComments(),
|
||||||
] );
|
],
|
||||||
|
200 );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -143,17 +147,24 @@ class DefaultController extends AbstractController {
|
|||||||
return $this->json( [
|
return $this->json( [
|
||||||
'message' => 'your poll config',
|
'message' => 'your poll config',
|
||||||
'data' => $poll,
|
'data' => $poll,
|
||||||
] );
|
],
|
||||||
|
200 );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Post(
|
* @Put(
|
||||||
* path = "/poll/{id}/up",
|
* path = "/poll/{id}",
|
||||||
* name = "up_poll",
|
* name = "update_poll",
|
||||||
* requirements = {"content"="\w+"}
|
* 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( [
|
return $this->json( [
|
||||||
'message' => 'you updated the poll',
|
'message' => 'you updated the poll',
|
||||||
] );
|
] );
|
||||||
@ -161,50 +172,119 @@ class DefaultController extends AbstractController {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @Post(
|
* @Post(
|
||||||
* path = "/comment/new",
|
* path = "poll/{id}/comment",
|
||||||
* name = "new_comment",
|
* name = "new_comment",
|
||||||
* requirements = {"content"="\w+", "poll_id"="\d+"}
|
* 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();
|
$data = $request->getContent();
|
||||||
|
|
||||||
$serializer = SerializerBuilder::create()->build();
|
$serializer = SerializerBuilder::create()->build();
|
||||||
$comment = $serializer->deserialize( $data, 'App\Entity\Comment', 'json' );
|
$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 );
|
$em = $this->getDoctrine()->getRepository( Owner::class );
|
||||||
$foundOwner = $em->find( 10 );
|
$foundOwner = $em->find( 10 );
|
||||||
$comment->setOwner( $foundOwner )
|
$comment->setOwner( $foundOwner )
|
||||||
->setPoll( $foundPoll );
|
->setPoll( $poll );
|
||||||
|
|
||||||
$em = $this->getDoctrine()->getManager();
|
$em = $this->getDoctrine()->getManager();
|
||||||
$em->persist( $comment );
|
$em->persist( $comment );
|
||||||
// $em->persist( $foundOwner );
|
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
return $this->json( [
|
return $this->json( [
|
||||||
'message' => 'you created a comment',
|
'message' => 'you created a comment',
|
||||||
] );
|
],
|
||||||
|
201 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function deletePollAction() {
|
/**
|
||||||
|
* @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( [
|
return $this->json( [
|
||||||
'message' => 'boom',
|
'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() {
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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( [
|
return $this->json( [
|
||||||
'message' => 'boom',
|
'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',
|
||||||
] );
|
] );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function deletePollVotesAction() {
|
|
||||||
return $this->json( [
|
|
||||||
'message' => 'boom',
|
|
||||||
] );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user