Only expose relevant attributes.
[Project_proches_de_moi-server.git] / src / Controller / PersonController.php
index 18f4a3ed7777e36c454a9b8d370058e1f4554c7b..734f29c9eca165ade74de8773ffde5970a07cb1a 100644 (file)
@@ -3,7 +3,6 @@ namespace App\Controller;
 
 use App\Entity\Person;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
-#use FOS\RestBundle\Controller\FOSRestController;
 use FOS\RestBundle\Controller\Annotations as Rest;
 use FOS\RestBundle\View\ViewHandler;
 use FOS\RestBundle\View\View;
@@ -61,7 +60,7 @@ class PersonController extends Controller
         $person = $em->getRepository('App:Person')->find($request->get('id'));
 
         if (empty($person)) {
-            return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+            return $this->PersonNotFound();
         }
 
         $person->setFirstName($request->get('firstname'));
@@ -75,11 +74,35 @@ class PersonController extends Controller
     }
 
     /**
-     * @Rest\Get("/api/person/{id}/friends/localisation")
+     * @Rest\Get("/api/person/{id}/localisations")
+     * @Rest\View()
      */
-    public function getFriendsLocalisationAction(Request $request)
+    public function getLocalisationsAction(Request $request)
     {
+        $em = $this->getDoctrine()->getManager();
+        $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
+
+        if (empty($localisations)) {
+            return $this->PersonLocalisationsNotFound();
+        }
 
+        return $localisations;
+    }
+
+    /**
+     * @Rest\Get("/api/person/{id}/localisation")
+     * @Rest\View()
+     */
+    public function getLocalisationAction(Request $request)
+    {
+        $em = $this->getDoctrine()->getManager();
+        $localisations = $em->getRepository('App:Localisation')->findOneBy(['person' => $request->get('id')]);
+
+        if (empty($localisations)) {
+            return $this->PersonLocalisationNotFound();
+        }
+
+        return $localisations;
     }
 
     /**
@@ -114,13 +137,10 @@ class PersonController extends Controller
         $person = $em->getRepository('App:Person')->find($request->get('id'));
 
         if (empty($person)) {
-            return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+            return $this->PersonNotFound();
         }
 
-        $view = View::create($person);
-        $view->setFormat('json');
-
-        return $view;
+        return $person;
    }
 
    /**
@@ -134,16 +154,13 @@ class PersonController extends Controller
   public function showPersonByEmail(Request $request)
   {
         $em = $this->getDoctrine()->getManager();
-        $person = $em->getRepository('App:Person')->find($request->get('email'));
+        $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
 
         if (empty($person)) {
-            return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+            return $this->PersonNotFound();
         }
 
-        $view = View::create($person);
-        $view->setFormat('json');
-
-        return $view;
+        return $person;
   }
 
    /**
@@ -160,15 +177,31 @@ class PersonController extends Controller
         $person = $em->getRepository('App:Person')->find($request->get('id'));
 
         if (empty($person)) {
-            return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+            return $this->PersonNotFound();
         }
 
-        $view = View::create($person->getFriends());
-        $view->setFormat('json');
-
-        return $view;
+        return $person->getFriends();
   }
 
+  /**
+  * @Rest\Get(
+  *     path = "/api/persons",
+  *     name = "show_persons"
+  * )
+  * @Rest\View()
+  */
+ public function showPersons(Request $request)
+ {
+       $em = $this->getDoctrine()->getManager();
+       $persons = $em->getRepository('App:Person')->findAll();
+
+       if (empty($persons)) {
+           return $this->PersonsNotFound();
+       }
+
+       return $persons;
+ }
+
   /**
   * @Rest\Get(
   *     path = "/api/person/{email}/friends",
@@ -180,15 +213,29 @@ class PersonController extends Controller
  public function showPersonFriendsByEmail(Request $request)
  {
         $em = $this->getDoctrine()->getManager();
-        $person = $em->getRepository('App:Person')->find($request->get('email'));
+        $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
 
         if (empty($person)) {
-            return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+            return $this->PersonNotFound();
         }
 
-        $view = View::create($person->getFriends());
-        $view->setFormat('json');
+        return $person->getFriends();
+ }
+
+ private function PersonNotFound() {
+     return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+ }
+
+ private function PersonsNotFound() {
+     return View::create(['message' => 'Persons not found'], Response::HTTP_NOT_FOUND);
+ }
+
+ private function PersonLocalisationNotFound() {
+     return View::create(['message' => 'Person localisation not found'], Response::HTTP_NOT_FOUND);
+ }
 
-        return $view;
+ private function PersonLocalisationsNotFound() {
+     return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND);
  }
+
 }