RESTController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace WebserviceBundle\Controller;
  3. use FOS\RestBundle\Controller\Annotations\QueryParam;
  4. use FOS\RestBundle\Controller\Annotations\RouteResource;
  5. use FOS\RestBundle\Controller\Annotations\View;
  6. use FOS\RestBundle\Request\ParamFetcherInterface;
  7. use FOS\RestBundle\Util\Codes;
  8. use FOS\RestBundle\View\View as FOSView;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  10. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  11. use Symfony\Component\Form\Form;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Voryx\RESTGeneratorBundle\Controller\VoryxController;
  15. class RESTController extends VoryxController
  16. {
  17. var $repository = 'ClientBundle:Client';
  18. /**
  19. * Get all entities.
  20. *
  21. * @View(serializerEnableMaxDepthChecks=true)
  22. *
  23. * @param ParamFetcherInterface $paramFetcher
  24. * @param string $repository
  25. *
  26. * @return Response
  27. *
  28. * @QueryParam(name="offset", requirements="\d+", nullable=true, description="Offset from which to start listing notes.")
  29. * @QueryParam(name="limit", requirements="\d+", default="20", description="How many notes to return.")
  30. * @QueryParam(name="order_by", nullable=true, array=true, description="Order by fields. Must be an array ie. &order_by[name]=ASC&order_by[description]=DESC")
  31. * @QueryParam(name="filters", nullable=true, array=true, description="Filter by fields. Must be an array ie. &filters[id]=3")
  32. * &filters[qb-criteria] => Utilizará el matching con criteria donde los parámetros filters realizarán "field like %value%".
  33. */
  34. public function cgetAction(ParamFetcherInterface $paramFetcher)
  35. {
  36. $em = $this->getDoctrine()->getManager();
  37. $filters = !is_null($paramFetcher->get('filters')) ? $paramFetcher->get('filters') : array();
  38. if(isset($filters['qb-criteria'])) {
  39. try {
  40. unset($filters['qb-criteria']);
  41. $criteria = new \Doctrine\Common\Collections\Criteria();
  42. foreach($filters as $field => $value) {
  43. $criteria->andWhere($criteria->expr()->contains("$field", "$value"));
  44. }
  45. if(!is_null($paramFetcher->get('offset'))) {
  46. $criteria->setFirstResult($paramFetcher->get('offset'));
  47. }
  48. if(!is_null($paramFetcher->get('limit'))) {
  49. $criteria->setMaxResults($paramFetcher->get('limit'));
  50. }
  51. if($paramFetcher->get('order_by')) {
  52. $order_by = $paramFetcher->get('order_by');
  53. $orderBy = array();
  54. foreach($order_by as $field => $order) {
  55. $orderBy[$field] = $order;
  56. }
  57. $criteria->orderBy($orderBy);
  58. }
  59. $repo = $em->getRepository($this->repository);
  60. $entities = $repo->matching($criteria)->toArray();
  61. if ($entities) {
  62. return $entities;
  63. }
  64. } catch (\Exception $e) {
  65. return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
  66. }
  67. } else {
  68. try {
  69. $offset = $paramFetcher->get('offset');
  70. $limit = $paramFetcher->get('limit');
  71. $order_by = $paramFetcher->get('order_by');
  72. $entities = $em->getRepository($this->repository)->findBy($filters, $order_by, $limit, $offset);
  73. if ($entities) {
  74. return $entities;
  75. }
  76. return FOSView::create('Not Found', Codes::HTTP_NO_CONTENT);
  77. } catch (\Exception $e) {
  78. return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
  79. }
  80. }
  81. return array();
  82. }
  83. }