Only expose relevant attributes.
[Project_proches_de_moi-server.git] / src / Controller / PersonController.php
index d4bd7ff374204dc15b14c68f3ec21e7f6d096bd3..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;
@@ -16,7 +15,7 @@ class PersonController extends Controller
 {
     /**
      * @Rest\Post(
-     *     path = "/person/inscription",
+     *     path = "/api/person/inscription",
      *     name = "create_person"
      * )
      * @Rest\View(StatusCode = Response::HTTP_CREATED)
@@ -75,23 +74,32 @@ 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}/localisations")
+     * @Rest\Get("/api/person/{id}/localisation")
+     * @Rest\View()
      */
-    public function getLocalisationsAction(Request $request)
+    public function getLocalisationAction(Request $request)
     {
         $em = $this->getDoctrine()->getManager();
-        $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
+        $localisations = $em->getRepository('App:Localisation')->findOneBy(['person' => $request->get('id')]);
 
         if (empty($localisations)) {
-            return $this->PersonLocalisationsNotFound();
+            return $this->PersonLocalisationNotFound();
         }
 
         return $localisations;
@@ -188,7 +196,7 @@ class PersonController extends Controller
        $persons = $em->getRepository('App:Person')->findAll();
 
        if (empty($persons)) {
-           return $this->PersonNotFound();
+           return $this->PersonsNotFound();
        }
 
        return $persons;
@@ -218,6 +226,14 @@ class PersonController extends Controller
      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);
+ }
+
  private function PersonLocalisationsNotFound() {
      return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND);
  }