BrasserieController.php 3.08 KB
Newer Older
1 2 3 4 5
<?php

namespace Api2Bundle\Controller;

use Api2Bundle\Entity\Brasserie;
6
use Api2Bundle\Entity\Pays;
Mat's avatar
Mat committed
7
use Api2Bundle\Form\BrasserieType;
8 9
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
Mat's avatar
Mat committed
11
use Symfony\Component\HttpFoundation\Request;
12

13 14 15 16 17 18
/**
 * Class BrasserieController
 *
 * //@Security("has_role('ROLE_USER')")
 * @package Api2Bundle\Controller
 */
19 20 21 22 23 24 25
class BrasserieController extends FOSRestController
{
    
    /**
     * @Rest\Get(path="/brasserie")
     * @Rest\View(serializerGroups={"getBrasserie"})
     */
Mat's avatar
test  
Mat committed
26
    public function getAction(Request $request)
27
    {
Mat's avatar
test  
Mat committed
28
        dump($request); die;
29 30 31 32
        return $this->getDoctrine()
            ->getRepository(Brasserie::class)
            ->findAll();
    }
Mat's avatar
Mat committed
33 34 35 36
    
    
    /**
     * @param Request $request
Mat's avatar
Mat committed
37
     * @param Brasserie $id
Mat's avatar
Mat committed
38 39
     * @Rest\Put(path="/brasserie/{id}")
     * @Rest\View()
40
     * @return Brasserie|\Symfony\Component\Form\FormInterface
Mat's avatar
Mat committed
41 42 43 44
     */
    public function putAction(Request $request, Brasserie $id)
    {
        // le fait de typer $id fait qu'il retourne un objet !
Mat's avatar
Mat committed
45
        
Mat's avatar
Mat committed
46 47 48 49 50
        $form = $this->createForm(BrasserieType::class, $id,
            array(
                'method' => 'put'
            )
        );
Mat's avatar
Mat committed
51 52 53 54
        $form->handleRequest($request);
        
        if ($form->isSubmitted() && $form->isValid())
        {
Mat's avatar
Mat committed
55 56 57
            $em = $this->getDoctrine()->getManager();
            $em->flush();
            return $id;
Mat's avatar
Mat committed
58
        }
Mat's avatar
Mat committed
59 60 61 62 63 64
        return $form;
    }
    
    
    /**
     * @param Brasserie $id
Mat's avatar
hop  
Mat committed
65
     * @Rest\Get(path="/brasserie/{brasserieId}")
Mat's avatar
Mat committed
66 67 68
     * @Rest\View()
     * @return Brasserie
     */
Mat's avatar
hop  
Mat committed
69
    public function getByIdAction(Brasserie $brasserieId)
Mat's avatar
Mat committed
70
    {
Mat's avatar
hop  
Mat committed
71
       return $brasserieId;
Mat's avatar
Mat committed
72 73 74 75 76 77 78
    }
    
    
    /**
     * @param Request $request
     * @Rest\Post(path="/brasserie")
     * @Rest\View()
79
     * @return Brasserie|\Symfony\Component\Form\FormInterface
Mat's avatar
Mat committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
     */
    public function newAction(Request $request)
    {
        $brasserie = new Brasserie();
        $form = $this->createForm(BrasserieType::class, $brasserie);
        $form->handleRequest($request);
    
        if ($form->isSubmitted() && $form->isValid())
        {
            $em = $this->getDoctrine()->getManager();
            $em->persist($brasserie);
            $em->flush();
            return $brasserie;
        }
        return $form;
Mat's avatar
Mat committed
95
    }
96 97 98 99 100 101 102 103 104 105 106 107
    
    /**
     * @Rest\Get(path="/brasserie/parPays/{paysID}")
     * @Rest\View()
     * @return Brasserie[]
     */
    public function getByCountryAction(Pays $paysID)
    {
        return $this->getDoctrine()
            ->getRepository(Brasserie::class)
            ->findBy(array('pays' => $paysID));
    }
Mat's avatar
Mat committed
108 109
    
    /**
110 111
     * @param $id
     * @Rest\Delete(path="/brasserie/{id}")
Mat's avatar
Mat committed
112
     * @Rest\View()
113
     * @return Brasserie
Mat's avatar
Mat committed
114
     */
115
    public function deleteAction($id)
Mat's avatar
Mat committed
116
    {
117
        $em = $this->getDoctrine()->getManager();
118 119 120
        $brasserie = $em->getRepository(Brasserie::class)
            ->find($id);
        $em->remove($brasserie);
121
        $em->flush();
122
        return $brasserie;
Mat's avatar
Mat committed
123
    }
124
}