Commit 167c2c86 authored by Mat's avatar Mat
Browse files

complète le CRUD Lettre

parent 0703b246
......@@ -44,17 +44,65 @@ class LettreAPIController extends MasterAPIController
}
/**
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function readOneAction($id)
{
$em = $this->getDoctrine()->getManager();
$lettre = $em->getRepository('PotageBundle:Lettre')->findOneForAPIRead($id);
if ($lettre === null) {
return $this->api('Not found', Response::HTTP_NOT_FOUND);
}
return $this->api($lettre);
}
/**
* @param Request $request
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$lettre = $em->getRepository('PotageBundle:Lettre')->findOneForAPIUpdate($id);
if ($lettre === null) {
return $this->api('Not found', Response::HTTP_NOT_FOUND);
}
$form = $this->createForm(LettreAPIType::class, $lettre);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$em->flush();
return $this->api($lettre);
}
return $this->api($form, Response::HTTP_BAD_REQUEST);
}
/**
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function deleteAction($id)
{
$em = $this->getDoctrine()->getManager();
$lettre = $em->getRepository('PotageBundle:Lettre')->findOneForAPIDelete($id);
if ($lettre !== null)
{
$em->remove($lettre);
$em->flush();
}
return $this->api([]);
}
}
......@@ -162,6 +162,30 @@ api_lettre_create:
_controller: APIBundle:LettreAPI:create
methods: [ POST ]
api_lettre_read_one:
path: /lettre/{id}
requirements:
id: \d+
defaults:
_controller: APIBundle:LettreAPI:readOne
methods: [ GET ]
api_lettre_update:
path: /lettre/{id}
requirements:
id: \d+
defaults:
_controller: APIBundle:LettreAPI:update
methods: [ POST ]
api_lettre_delete:
path: /lettre/{id}
requirements:
id: \d+
defaults:
_controller: APIBundle:LettreAPI:delete
methods: [ DELETE ]
## CRUD InfoAPI
......
......@@ -20,4 +20,46 @@ class LettreRepository extends \Doctrine\ORM\EntityRepository
->orderBy('l.id', 'DESC');
return $qb->getQuery()->getResult();
}
/**
* @param $id
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForAPIRead($id)
{
$qb = $this->createQueryBuilder('l')
->where('l.id = :id')
->setParameter(':id', $id);
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @param $id
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForAPIUpdate($id)
{
$qb = $this->createQueryBuilder('l')
->where('l.id = :id')
->setParameter(':id', $id);
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @param $id
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForAPIDelete($id)
{
$qb = $this->createQueryBuilder('l')
->where('l.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