BrasserieController.php 2.83 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;
Mat's avatar
Mat committed
10
use Symfony\Component\HttpFoundation\Request;
11 12 13 14 15 16 17 18 19 20 21 22 23 24

class BrasserieController extends FOSRestController
{
    
    /**
     * @Rest\Get(path="/brasserie")
     * @Rest\View(serializerGroups={"getBrasserie"})
     */
    public function getAction()
    {
        return $this->getDoctrine()
            ->getRepository(Brasserie::class)
            ->findAll();
    }
Mat's avatar
Mat committed
25 26 27 28
    
    
    /**
     * @param Request $request
Mat's avatar
Mat committed
29
     * @param Brasserie $id
Mat's avatar
Mat committed
30 31
     * @Rest\Put(path="/brasserie/{id}")
     * @Rest\View()
32
     * @return Brasserie|\Symfony\Component\Form\FormInterface
Mat's avatar
Mat committed
33 34 35 36
     */
    public function putAction(Request $request, Brasserie $id)
    {
        // le fait de typer $id fait qu'il retourne un objet !
Mat's avatar
Mat committed
37
        
Mat's avatar
Mat committed
38 39 40 41 42
        $form = $this->createForm(BrasserieType::class, $id,
            array(
                'method' => 'put'
            )
        );
Mat's avatar
Mat committed
43 44 45 46
        $form->handleRequest($request);
        
        if ($form->isSubmitted() && $form->isValid())
        {
Mat's avatar
Mat committed
47 48 49
            $em = $this->getDoctrine()->getManager();
            $em->flush();
            return $id;
Mat's avatar
Mat committed
50
        }
Mat's avatar
Mat committed
51 52 53 54 55 56
        return $form;
    }
    
    
    /**
     * @param Brasserie $id
Mat's avatar
hop  
Mat committed
57
     * @Rest\Get(path="/brasserie/{brasserieId}")
Mat's avatar
Mat committed
58 59 60
     * @Rest\View()
     * @return Brasserie
     */
Mat's avatar
hop  
Mat committed
61
    public function getByIdAction(Brasserie $brasserieId)
Mat's avatar
Mat committed
62
    {
Mat's avatar
hop  
Mat committed
63
       return $brasserieId;
Mat's avatar
Mat committed
64 65 66 67 68 69 70
    }
    
    
    /**
     * @param Request $request
     * @Rest\Post(path="/brasserie")
     * @Rest\View()
71
     * @return Brasserie|\Symfony\Component\Form\FormInterface
Mat's avatar
Mat committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
     */
    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
87
    }
88 89 90 91 92 93 94 95 96 97 98 99
    
    /**
     * @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
100 101
    
    /**
102 103
     * @param Brasserie $brasserieId
     * @Rest\Delete(path="/brasserie/{brasserieId}")
Mat's avatar
Mat committed
104
     * @Rest\View()
105
     * @return Brasserie
Mat's avatar
Mat committed
106
     */
107
    public function deleteAction(Brasserie $brasserieId)
Mat's avatar
Mat committed
108
    {
109 110 111 112
        $em = $this->getDoctrine()->getManager();
        $em->remove($brasserieId);
        $em->flush();
        return $brasserieId;
Mat's avatar
Mat committed
113
    }
114
}