浏览代码

Se subieron metodos genericos al RESTController

gabriel 7 年之前
父节点
当前提交
31b9027d56
共有 1 个文件被更改,包括 136 次插入0 次删除
  1. 136 0
      Controller/RESTController.php

+ 136 - 0
Controller/RESTController.php

@@ -2,12 +2,14 @@
 
 namespace WebserviceBundle\Controller;
 
+use ClientBundle\Form\ClientType;
 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 ReflectionClass;
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\Form\Form;
@@ -23,6 +25,18 @@ abstract class RESTController extends VoryxController
      */
     abstract public function getRepository();
 
+    /**
+     * @return string Retorna el tipo de la clase.
+     */
+//    abstract public function getFormEntityType();
+    /**
+     * @return string Retorna el tipo de la clase.
+     */
+    public function getFormEntityType()
+    {
+        return get_class(new ClientType());
+    }
+
     /**
      * Get all entities.
      *
@@ -107,4 +121,126 @@ abstract class RESTController extends VoryxController
         return array();
     }
 
+    /**
+     * @return object Retorna el nombre de la Entity de trabajo.
+     */
+    public function getObject()
+    {
+        $obj = $this->getDoctrine()->getManager()->getMetadataFactory()->getMetadataFor($this->getRepository())->getName();
+        $rc = new ReflectionClass($obj);
+        return $rc->newInstance();
+    }
+
+    /**
+     * Create a Client entity.
+     *
+     * @View(statusCode=201, serializerEnableMaxDepthChecks=true)
+     *
+     * @param Request $request Contiene el request.
+     *
+     * @return FOSView|mixed Retorna el FOSView o la entidad.
+     *
+     */
+    public function postAction(Request $request)
+    {
+//        file_put_contents("/tmp/t", "LLEGO\n",8);
+        $entity = $this->getObject();
+        $form = $this->createForm($this->getFormEntityType(), $entity, array("method" => $request->getMethod()));
+        $this->removeExtraFields($request, $form);
+        $form->handleRequest($request);
+
+        if ($form->isSubmitted() && $form->isValid()) {
+            $em = $this->getDoctrine()->getManager();
+            $em->persist($entity);
+            $em->flush();
+
+            return $entity;
+        }
+        return FOSView::create(array('errors' => $form->getErrors()), Codes::HTTP_INTERNAL_SERVER_ERROR);
+    }
+
+    /**
+     * Update a Client entity.
+     *
+     * @View(serializerEnableMaxDepthChecks=true)
+     *
+     * @param Request $request Contiene el request.
+     * @param mixed $entity Contiene la entidad
+     *
+     * @return FOSView|mixed Retorna el FOSView o la entidad.
+     */
+    public function putAction(Request $request, $entity = null)
+    {
+        try {
+            $em = $this->getDoctrine()->getManager();
+            $request->setMethod('PATCH'); //Treat all PUTs as PATCH
+            $form = $this->createForm($this->getFormEntityType(), $entity, array("method" => $request->getMethod()));
+            $this->removeExtraFields($request, $form);
+            $form->handleRequest($request);
+            if ($form->isValid()) {
+                $em->flush();
+
+                return $entity;
+            }
+
+            return FOSView::create(array('errors' => $form->getErrors()), Codes::HTTP_INTERNAL_SERVER_ERROR);
+        } catch (\Exception $e) {
+            return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
+        }
+    }
+
+    /**
+     * Partial Update to a Client entity.
+     *
+     * @View(serializerEnableMaxDepthChecks=true)
+     *
+     * @param Request $request Contiene el request.
+     * @param mixed $entity Contiene la entidad.
+     *
+     * @return Response Retorna un response.
+     */
+    public function patchAction(Request $request, $entity)
+    {
+        return $this->putAction($request, $entity);
+    }
+
+    /**
+     * Delete a Client entity.
+     *
+     * @View(statusCode=204)
+     *
+     * @param Request $request Contiene el request.
+     * @param mixed $entity Contiene la entidad a borrar.
+     *
+     * @return FOSView Retorna el FSOView.
+     */
+    public function deleteAction(Request $request, $entity)
+    {
+        try {
+            $em = $this->getDoctrine()->getManager();
+            $em->remove($entity);
+            $em->flush();
+
+            return null;
+        } catch (\Exception $e) {
+            return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
+        }
+    }
+
+    /**
+     * Get a Client entity
+     *
+     * @View(serializerEnableMaxDepthChecks=true)
+     *
+     * @param mixed $entity Contiene la entidad.
+     *
+     * @return Response
+     *
+     */
+    public function getAction($entity)
+    {
+        return $entity;
+    }
+
+
 }