Commit 72155699 authored by Mat's avatar Mat

complète le CRUD OffreLegumes

parent d90b5477
......@@ -3,31 +3,105 @@
namespace APIBundle\Controller;
use APIBundle\Form\OffreLegumesAPIType;
use PotageBundle\Entity\OffreLegumes;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class OffreLegumesAPIController extends MasterAPIController
{
/**
* @param $id
* @return \Symfony\Component\HttpFoundation\Response
* @throws \Doctrine\ORM\NonUniqueResultException
* @param Request $request
* @param $id_offre
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function readAction($id)
public function createAction(Request $request, $id_offre)
{
$legume = new OffreLegumes($id_offre);
$form = $this->createForm(OffreLegumesAPIType::class, $legume);
$form->handleRequest($request);
if ($form->isSubmitted() && $form-> isValid())
{
$em = $this->getDoctrine()->getManager();
$em->persist($legume);
$em->flush();
return $this->api($legume);
}
return $this->api($form, Response::HTTP_BAD_REQUEST);
}
/**
* @param $id_offre
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function readAction($id_offre)
{
$em = $this->getDoctrine()->getManager();
// Récupère l'ordre de tri
$sorted = $em->getRepository('PotageBundle:Offre')->findOneForAPIRead($id)->getSorted();
$sorted = $em->getRepository('PotageBundle:Offre')->findOneForAPIRead($id_offre)->getSorted();
if (!empty($sorted))
{
$legumes = $em->getRepository('PotageBundle:OffreLegumes')->findAllForSortedRead($id, $sorted);
$legumes = $em->getRepository('PotageBundle:OffreLegumes')->findAllForSortedRead($id_offre, $sorted);
}
else {
$legumes = $em->getRepository('PotageBundle:OffreLegumes')->findAllForRead($id);
$legumes = $em->getRepository('PotageBundle:OffreLegumes')->findAllForRead($id_offre);
}
return $this->api($legumes);
}
/**
* @param Request $request
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$legume = $em->getRepository('PotageBundle:OffreLegumes')->findOneForUpdate($id);
if ($legume === null)
{
return $this->api('Not found', Response::HTTP_NOT_FOUND);
}
$form = $this->createForm(OffreLegumesAPIType::class, $legume);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$em->flush();
return $this->api($legume);
}
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();
$legume = $em->getRepository('PotageBundle:OffreLegumes')->findOneForDelete($id);
if ($legume !== null)
{
$em->remove($legume);
$em->flush();
}
return $this->api([]);
}
}
......@@ -116,13 +116,37 @@ api_offre_delete:
## CRUD OffreLegumesAPI
api_offre_legumes_read:
path: /offre/{id}/legumes
path: /offre/{id_offre}/legumes
requirements:
id: \d+
id_offre: \d+
defaults:
_controller: APIBundle:OffreLegumesAPI:read
methods: [ GET ]
api_offre_legumes_create:
path: /offre/{id_offre}/legumes/ajouter
requirements:
id_offre: \d+
defaults:
_controller: APIBundle:OffreLegumesAPI:create
methods: [ POST ]
api_offre_legumes_update:
path: /offre/legume/{id}
requirements:
id: \d+
defaults:
_controller: APIBundle:OffreLegumesAPI:update
methods: [ POST ]
api_offre_legumes_delete:
path: /offre/legume/{id}
requirements:
id: \d+
defaults:
_controller: APIBundle:OffreLegumesAPI:delete
methods: [ DELETE ]
## CRUD LettreAPI
......
......@@ -159,9 +159,12 @@ class OffreLegumes
/**
* OffreLegumes constructor.
*
* @param $id_offre
*/
public function __construct()
public function __construct($id_offre)
{
$this->offre = $id_offre;
$this->isPromo = false;
$this->fruit = false;
$this->revente = false;
......
......@@ -62,5 +62,29 @@ class OffreLegumesRepository extends \Doctrine\ORM\EntityRepository
return $qb->getQuery()->getResult();
}
/**
* @param $id
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForUpdate($id)
{
$qb = $this->createQueryBuilder('ol')
->where('ol.id = :id')
->setParameter(':id', $id);
return $qb->getQuery()->getOneOrNullResult();
}
/**
* @param $id
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForDelete($id)
{
$qb = $this->createQueryBuilder('ol')
->where('ol.id = :id')
->setParameter(':id', $id);
return $qb->getQuery()->getOneOrNullResult();
}
}
......@@ -3,7 +3,7 @@ potage_homepage:
defaults:
_controller: PotageBundle:Default:index
### les routes des fonctions qui interrogent l'APIBundle
### les routes des pages qui interrogent l'APIBundle
potage_legume_ajax_display:
path: /admin/legumes
......
......@@ -130,7 +130,7 @@
let sortable = grid.querySelector('div#sortable');
sortable.innerHTML = null;
let URL = Routing.generate('api_offre_legumes_read', {'id': id_offre });
let URL = Routing.generate('api_offre_legumes_read', {'id_offre': id_offre });
AJAX('GET', URL, function(request)
{
......
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