Commit 23a68193 authored by Mat's avatar Mat

routes, controller et repository pour les fonctions du CRUD Membre

parent ec0fd458
......@@ -6,11 +6,34 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use AssoBundle\Entity\Membre;
use APIBundle\Form\Membre\MembreAPIType;
class MembreAPIController extends MasterAPIController
{
/**
* @param Request
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function createAction(Request $request)
{
$membre = new Membre();
$form = $this->createForm(MembreAPIType::class, $membre);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->persist($membre);
$em->flush();
return $this->api($membre);
}
return $this->api($form, Response::HTTP_BAD_REQUEST);
}
/**
* @param Request
* @return \Symfony\Component\HttpFoundation\JsonResponse
......@@ -30,4 +53,58 @@ class MembreAPIController extends MasterAPIController
return $this->api($membres);
}
/**
* @param Integer $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function readOneAction($id)
{
$em = $this->getDoctrine()->getManager();
$membre = $em->getRepository('AssoBundle:Membre')->findOneForAPIRead($id);
if ($membre === null) {
throw $this->api('Not found', Response::HTTP_NOT_FOUND);
}
return $this->api($membre);
}
/**
* @param Request $request
* @param Integer $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$membre = $em->getRepository('AssoBundle:Membre')->findOneForAPIUpdate($id);
if ($membre === null) {
throw $this->api('Not found', Response::HTTP_NOT_FOUND);
}
$form = $this->createForm(membreAPIBundle::class, $membre);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->flush()
return $this->api($membre);
}
return $this->api($form, Response::HTTP_BAD_REQUEST);
}
/**
* @param Integer $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function deleteAction($id)
{
$em = $this->getDoctrine()->getManager();
$membre = $em->getRepository('AssoBundle:Membre')->findOneForAPIDelete($id);
if ($membre !== null) {
$em->remove();
$em->flush;
}
return $this->api([]);
}
}
......@@ -80,12 +80,41 @@ api_image_delete_one:
## MembreAPI
api_membre_create:
path: /membre/add
defaults:
_controller: APIBundle:MembreAPI:create
methods: [ POST ]
api_membre_read_paginate:
path: /membres/page
defaults:
_controller: APIBundle:MembreAPI:readPaginate
methods: [ GET ]
api_membre_readOne:
path: /membre/{id}
requirements:
id: \d+
defaults:
_controller: APIBundle:MembreAPI:readOne
methods: [ GET ]
api_membre_update:
path: /membre/{id}
requirements:
id: \d+
defaults:
_controller: APIBundle:MembreAPI:update
methods: [ POST ]
api_membre_delete:
path: /membre/{id}
requirements:
id: \d+
defaults:
_controller: APIBundle:MembreAPI:delete
methods: [ DELETE ]
##
## Note:
......
......@@ -104,4 +104,53 @@ class MembreRepository extends \Doctrine\ORM\EntityRepository
return $qb->getQuery()->getResult();
}
/**
* @param Integer $id
* @return Membre
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForAPIRead($id)
{
$qb = $this->createQueryBuilder('m')
->join('m.association', 'a')
->addSelect('a')
->where('m.id = :id')
->setParameter(':id', $id);
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @param Integer $id
* @return Membre
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForAPIUpdate($id)
{
$qb = $this->createQueryBuilder('m')
->join('m.association', 'a')
->addSelect('a')
->where('m.id = :id')
->setParameter(':id', $id);
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @param Integer $id
* @return Membre
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForAPIDelete($id)
{
$qb = $this->createQueryBuilder('m')
->join('m.association', 'a')
->addSelect('a')
->where('m.id = :id')
->setParameter(':id', $id);
return $qb->getQuery()->getOneOrNullResult();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment