Created
November 26, 2014 10:38
-
-
Save saro0h/7a80a983cdfb931032da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Smoovio\Bundle\ApiBundle\Controller; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\ParameterBag; | |
use Smoovio\Bundle\CoreBundle\Entity\Genre; | |
use Smoovio\Bundle\CoreBundle\Entity\Movie; | |
class ApiController extends Controller | |
{ | |
/** | |
* @Route("/movies/{slug}", methods={"POST"}) | |
*/ | |
public function movieAction(Request $request) | |
{ | |
$request->request = new ParameterBag(json_decode($request->getContent(), true)); | |
$movie = $this | |
->get('smoovio_core.repository.movie') | |
->findOneBySlug($request->get('slug')) | |
; | |
if (!$movie) { | |
throw $this->createNotFoundException('movie not found'); | |
} | |
if ('show' === $request->get('action')) { | |
return $this->render('SmoovioApiBundle:Endpoint:showMovie.json.twig', | |
['movie' => $movie]) | |
; | |
} | |
$em = $this->getDoctrine()->getManager(); | |
if ('delete' === $request->get('action')) { | |
foreach ($movie->getRoles() as $role) { | |
$em->remove($role); | |
} | |
$em->remove($movie); | |
$em->flush(); | |
} | |
if ('update' === $request->get('action')) { | |
$data = $request->request->get('movie'); | |
$movie->setTitle($data['title']); | |
$movie->setDescription($data['description']); | |
$em->persist($movie); | |
$em->flush(); | |
} | |
} | |
/** | |
* @Route("/genres", methods={"POST"}) | |
*/ | |
public function genreAction(Request $request) | |
{ | |
$request->request = new ParameterBag(json_decode($request->getContent(), true)); | |
if ('create' === $request->get('action')) { | |
$data = $request->request->get('genre'); | |
$genre = new Genre($data['title'], $data['slug']); | |
$em = $this->getDoctrine()->getManager(); | |
$em->persist($genre); | |
$em->flush(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment