From 80ec112fadb2fb94341049775f12fd5496662129 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 3 Jul 2018 23:50:23 +0200 Subject: [PATCH] Add a person search REST ressource. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- README | 1 + src/Controller/PersonController.php | 28 +++++++++++++++++++++++++++- tests/curl.txt | 3 +++ 3 files changed, 31 insertions(+), 1 deletion(-) 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 -- 2.34.1