From: Jérôme Benoit Date: Tue, 3 Jul 2018 21:50:23 +0000 (+0200) Subject: Add a person search REST ressource. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=80ec112fadb2fb94341049775f12fd5496662129;p=Project_proches_de_moi-server.git Add a person search REST ressource. Signed-off-by: Jérôme Benoit --- diff --git a/README b/README index 0a3e81e..3310259 100644 --- a/README +++ b/README @@ -27,6 +27,7 @@ Virtual localhost configuration: AllowOverride None Order Allow,Deny Allow from All + Require all granted Options -MultiViews diff --git a/src/Controller/PersonController.php b/src/Controller/PersonController.php index b34bb53..6b41205 100644 --- a/src/Controller/PersonController.php +++ b/src/Controller/PersonController.php @@ -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); } diff --git a/tests/curl.txt b/tests/curl.txt index c597b4d..7e14731 100644 --- a/tests/curl.txt +++ b/tests/curl.txt @@ -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