Add a person search REST ressource.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 3 Jul 2018 21:50:23 +0000 (23:50 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 3 Jul 2018 21:50:23 +0000 (23:50 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
README
src/Controller/PersonController.php
tests/curl.txt

diff --git a/README b/README
index 0a3e81ebc88bd2c9add76360fc9be3e58e1c1f81..3310259125864810a31c86ed01c338530c333905 100644 (file)
--- a/README
+++ b/README
@@ -27,6 +27,7 @@ Virtual localhost configuration:
         AllowOverride None
         Order Allow,Deny
         Allow from All
+        Require all granted
 
         <IfModule mod_rewrite.c>
             Options -MultiViews
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);
     }
index c597b4d83956604d842d95200f2d00d55e3b4f24..7e147312b7fe458a7968f2ae43dd22790dbae3f5 100644 (file)
@@ -17,6 +17,9 @@ curl --request POST http://localhost:8000/api/person/1/localisation --data "{ \"
 curl --request PUT http://localhost:8000/api/person/1/online
 curl --request PUT http://localhost:8000/api/person/1/offline
 
+* Search a user with a keyword:
+curl --request POST http://localhost:8000/api/person/search --data "{ \"keyword\": \"isabelle\"}" --header "Content-Type: application/json"
+
 * Show a user with id 1:
 curl --request GET http://localhost:8000/api/person/1