Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.20 KB | None | 0 0
  1. <?php
  2.  
  3. namespace BlogBundle\Controller\Api;
  4.  
  5. use BlogBundle\Entity\Auteur;
  6. use BlogBundle\Entity\Article;
  7.  
  8.  
  9. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15.  
  16. class ArticleController extends Controller
  17. {
  18.     /**
  19.      * Créer un nouvel article
  20.      *
  21.      * @Route("/api/article")
  22.      * @Method("POST")
  23.      */
  24.     public function newAction(Request $request)
  25.     {
  26.         $data = json_decode($request->getContent(), true);
  27.         $auteur = $this->getDoctrine()
  28.                   ->getRepository('BlogBundle:Auteur')
  29.                   ->find($data['auteur']);
  30.         $date = new \DateTime();
  31.         $article = new Article($data['title'], $data['content'], $date, $auteur);
  32.  
  33.         $em = $this->getDoctrine()->getManager();
  34.         $em->persist($article);
  35.         $em->flush();
  36.  
  37.         $data = $this->serializeArticle($article);
  38.         $response = new JsonResponse($data, 201);
  39.         $showURL = $this->generateUrl('api_article_id', ['id' => $article->getid()]);
  40.         $response->headers->set('Location', $showURL);
  41.         return $response;
  42.     }
  43.  
  44.     /**
  45.      * Récupérer un article par ID
  46.      *
  47.      * @Route("/api/article/{id}", requirements={"id"="\d+"}, name="api_article_id")
  48.      * @Method("GET")
  49.      */
  50.     public function showAction($id)
  51.     {
  52.         $article = $this->getDoctrine()
  53.                   ->getRepository('BlogBundle:Article')
  54.                   ->find($id);
  55.  
  56.         if(!$article) {
  57.             throw $this->createNotFoundException(
  58.                 sprintf('Il n\'existe pas d\'article avec l\'ID "%d"', $id));
  59.         }
  60.  
  61.         $response = new JsonResponse($this->serializeArticle($article), 200);
  62.         return $response;
  63.     }
  64.  
  65.     /**
  66.      * Récupérer plusieurs articles
  67.      *
  68.      * @Route("/api/article")
  69.      * @Method("GET")
  70.      */
  71.     public function listAction()
  72.     {
  73.         $articles = $this->getDoctrine()
  74.                   ->getRepository('BlogBundle:Article')
  75.                   ->findBy([], ['id' => 'DESC']);
  76.  
  77.         $data = array();
  78.         foreach($articles as $article) {
  79.             $data[] = $this->serializeArticle($article);
  80.         }
  81.  
  82.         $response = new JsonResponse($data, 200);
  83.         return $response;
  84.     }
  85.  
  86.     /**
  87.      * Modifier un article
  88.      *
  89.      * @Route("/api/article/{id}", requirements={"id"="\d+"})
  90.      * @Method("PUT")
  91.      */
  92.     public function updateAction($id, Request $request)
  93.     {
  94.         $data = json_decode($request->getContent(), true);
  95.  
  96.  
  97.         $article = $this->getDoctrine()
  98.           ->getRepository('BlogBundle:Article')
  99.           ->find($id);
  100.  
  101.         if(!$article) {
  102.             throw $this->createNotFoundException(
  103.                 sprintf('Il n\'existe pas d\'article avec l\'ID "%d"', $id));
  104.         }
  105.  
  106.         $article->setTitle($data['title']);
  107.         $article->setContent($data['content']);
  108.  
  109.         $auteur = $this->getDoctrine()
  110.                   ->getRepository('BlogBundle:Auteur')
  111.                   ->find($data['auteur']['id']);
  112.         $article->setAuteur($auteur);
  113.  
  114.         $em = $this->getDoctrine()->getManager();
  115.         $em->persist($article);
  116.         $em->flush();
  117.  
  118.         $data = $this->serializeArticle($article);
  119.         $response = new JsonResponse($data, 200);
  120.         return $response;
  121.  
  122. /*        ;
  123.         $article->setContent($data['content']);
  124.         $article->setDate($data['date']);
  125.         //$article->setAuteur($data['auteur']);
  126.  
  127.         $em = $this->getDoctrine()->getManager();
  128.         $em->persist($article);
  129.         $em->flush();
  130.  
  131.         $data = $this->serializeArticle($article);
  132.         $response = new JsonResponse($data, 200);
  133.         return $response;*/
  134.     }
  135.  
  136.     /**
  137.      * Supprimer un article
  138.      *
  139.      * @Route("/api/article/{id}", requirements={"id"="\d+"})
  140.      * @Method("DELETE")
  141.      */
  142.     public function deleteAction($id, Request $request)
  143.     {
  144.         $article = $this->getDoctrine()
  145.           ->getRepository('BlogBundle:Article')
  146.           ->find($id);
  147.  
  148.         if(!$article) {
  149.             throw $this->createNotFoundException(
  150.                 sprintf('Il n\'existe pas d\'article avec l\'ID "%d"', $id));
  151.         }
  152.  
  153.         $em = $this->getDoctrine()->getManager();
  154.         $em->remove($article);
  155.         $em->flush();
  156.  
  157.         return new Response(null, 204);
  158.  
  159.     }
  160.  
  161.  
  162.     private function serializeArticle(Article $article)
  163.     {
  164.         return array(
  165.             'id' => $article->getId(),
  166.             'title' => $article->getTitle(),
  167.             'content' => $article->getContent(),
  168.             'date' => $article->getDate(),
  169.             'auteur' => $this->serializeAuteur($article->getAuteur()),
  170.         );
  171.     }
  172.  
  173.     private function serializeAuteur(Auteur $auteur)
  174.     {
  175.         return array(
  176.             'id' => $auteur->getId(),
  177.             'nom' => $auteur->getNom(),
  178.             'prenom' => $auteur->getPrenom(),
  179.             'email' => $auteur->getEmail(),
  180.         );
  181.     }
  182.  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement