Add a person search REST ressource.
[Project_proches_de_moi-server.git] / src / Controller / PersonController.php
index b34bb53d6f60aea5e094fee951714ad11962e57f..6b4120545ec9196498d354910457e2de92407f97 100644 (file)
@@ -342,7 +342,9 @@ class PersonController extends FOSRestController
             return $this->PersonNotFound();
         }
 
-        return $person->getFriendsWithMe();
+        $friends_with_me = $person->getFriendsWithMe();
+
+        return $friends_with_me;
     }
 
     /**
@@ -410,6 +412,30 @@ class PersonController extends FOSRestController
         $em->flush();
     }
 
+    /**
+     * @Rest\Post(
+     *     path = "/api/person/search",
+     *     name = "search_person"
+     * )
+     * @Rest\View()
+     */
+    public function searchPerson(Request $request)
+    {
+        $em = $this->getDoctrine()->getManager();
+        $query = $em->createQuery("SELECT DISTINCT p FROM App\Entity\Person p WHERE
+            p.firstname LIKE :keyword or
+            p.lastname LIKE :keyword or
+            p.email LIKE :keyword");
+        $query->setParameter('keyword', '%'.$request->get('keyword').'%');
+        $persons = $query->getResult();
+
+        if (empty($persons)) {
+            return $this->PersonNotFound();
+        }
+
+        return $persons;
+    }
+
     private function PersonNotFound() {
         return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
     }