Pārlūkot izejas kodu

Se agrega RESTController.

Maximiliano Schvindt 8 gadi atpakaļ
vecāks
revīzija
9e46456547
2 mainītis faili ar 103 papildinājumiem un 17 dzēšanām
  1. 0 17
      Controller/DefaultController.php
  2. 103 0
      Controller/RESTController.php

+ 0 - 17
Controller/DefaultController.php

@@ -1,17 +0,0 @@
-<?php
-
-namespace WebserviceBundle\Controller;
-
-use Symfony\Bundle\FrameworkBundle\Controller\Controller;
-use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
-
-class DefaultController extends Controller
-{
-    /**
-     * @Route("/")
-     */
-    public function indexAction()
-    {
-        return $this->render('WebserviceBundle:Default:index.html.twig');
-    }
-}

+ 103 - 0
Controller/RESTController.php

@@ -0,0 +1,103 @@
+<?php
+
+namespace WebserviceBundle\Controller;
+
+use FOS\RestBundle\Controller\Annotations\QueryParam;
+use FOS\RestBundle\Controller\Annotations\RouteResource;
+use FOS\RestBundle\Controller\Annotations\View;
+use FOS\RestBundle\Request\ParamFetcherInterface;
+use FOS\RestBundle\Util\Codes;
+use FOS\RestBundle\View\View as FOSView;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\Form\Form;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+
+use Voryx\RESTGeneratorBundle\Controller\VoryxController;
+
+class RESTController extends VoryxController
+{
+    var $repository = 'ClientBundle:Client';
+    
+    /**
+     * Get all entities.
+     *
+     * @View(serializerEnableMaxDepthChecks=true)
+     *
+     * @param ParamFetcherInterface $paramFetcher
+     * @param string $repository
+     *
+     * @return Response
+     *
+     * @QueryParam(name="offset", requirements="\d+", nullable=true, description="Offset from which to start listing notes.")
+     * @QueryParam(name="limit", requirements="\d+", default="20", description="How many notes to return.")
+     * @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")
+     * @QueryParam(name="filters", nullable=true, array=true, description="Filter by fields. Must be an array ie. &filters[id]=3")
+     * &filters[qb-criteria] => Utilizará el matching con criteria donde los parámetros filters realizarán "field like %value%".
+     */
+    public function cgetAction(ParamFetcherInterface $paramFetcher)
+    {
+        $em = $this->getDoctrine()->getManager();
+        
+        $filters = !is_null($paramFetcher->get('filters')) ? $paramFetcher->get('filters') : array();
+        if(isset($filters['qb-criteria'])) {
+            try {
+
+                unset($filters['qb-criteria']);
+                $criteria = new \Doctrine\Common\Collections\Criteria();
+
+                foreach($filters as $field => $value) {
+                    $criteria->andWhere($criteria->expr()->contains("$field", "$value"));
+                }
+
+                if(!is_null($paramFetcher->get('offset'))) {
+                    $criteria->setFirstResult($paramFetcher->get('offset'));
+                }
+
+                if(!is_null($paramFetcher->get('limit'))) {
+                    $criteria->setMaxResults($paramFetcher->get('limit'));
+                }
+
+                if($paramFetcher->get('order_by')) {
+                    $order_by = $paramFetcher->get('order_by');
+                    $orderBy = array();
+                    foreach($order_by as $field => $order) {
+                        $orderBy[$field] = $order;
+                    }
+                    $criteria->orderBy($orderBy);
+                }
+
+                $clients = $em->getRepository($this->repository);
+                $entities = $clients->matching($criteria)->toArray();
+
+                if ($entities) {
+                    return $entities;
+                }
+            
+            } catch (\Exception $e) {
+                return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
+            }
+            
+        } else {
+            try {
+                $offset = $paramFetcher->get('offset');
+                $limit = $paramFetcher->get('limit');
+                $order_by = $paramFetcher->get('order_by');
+                $entities = $em->getRepository($this->repository)->findBy($filters, $order_by, $limit, $offset);
+
+                if ($entities) {
+                    return $entities;
+                }
+
+                return FOSView::create('Not Found', Codes::HTTP_NO_CONTENT);
+            } catch (\Exception $e) {
+                return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
+            }
+
+        }
+
+         return array();
+    }
+    
+}