From 2ebae1bf0e7f600ee35798a61d4dbfbfedc77899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20MARQUET?= Date: Wed, 3 Apr 2024 16:33:02 +0200 Subject: [PATCH] Search with rest API --- Request_Test/testHTTP.http | 4 ++++ src/API/requests.php | 23 +++++++++++++++++++++++ src/php/db/Search.php | 24 ++++++++++-------------- 3 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 Request_Test/testHTTP.http diff --git a/Request_Test/testHTTP.http b/Request_Test/testHTTP.http new file mode 100644 index 0000000..4286906 --- /dev/null +++ b/Request_Test/testHTTP.http @@ -0,0 +1,4 @@ +GET http://serveur-projet-s4.felix/src/API/requests.php/api/search?type=Généraliste +Accept: application/json + +### diff --git a/src/API/requests.php b/src/API/requests.php index 54fa3e5..83b4d2b 100644 --- a/src/API/requests.php +++ b/src/API/requests.php @@ -1,6 +1,13 @@ GET('/api/requests', ["test"], function($test){ echo json_encode($test); }); +$router->GET('/api/search', ["type"], function($type){ + global $pdo; + searchDoctor($pdo, $type); +}); + +$router->GET('/api/search', ["type", "location"], function($type, $location){ + global $pdo; + searchDoctorByLocation($pdo, $location, $type); +}); + +$router->GET('/api/search', ["location"], function($location){ + global $pdo; + searchDoctorByLocation($pdo, $location); +}); + + $router->POST('/api/requests', ["test"], function($test){ echo json_encode($test); }); diff --git a/src/php/db/Search.php b/src/php/db/Search.php index b9724e7..8a4a294 100644 --- a/src/php/db/Search.php +++ b/src/php/db/Search.php @@ -1,4 +1,6 @@ prepare("SELECT m_name, m_surname, m_postal, m_specialty, m_phone FROM medecin WHERE m_specialty = :type"); $query->bindParam(':type', $type); @@ -251,25 +253,21 @@ function searchDoctor($pdo, $type): bool|string } // If the query returned no result return an 404 error if($count == 0){ - http_response_code(404); - // Return the json_encode array - return json_encode(array("message" => "Aucun résultat")); + Response::HTTP404(['error' => "Aucun résultat"]); } // Else return the result with a 200 code else { - http_response_code(200); - // Return the json_encode array - return json_encode($result); + Response::HTTP200($result); } } else { - http_response_code(200); - return json_encode($result); + Response::HTTP200($result); } + } // searchDoctorByLocation search doctor by location and if provided type and return the result in JSON format -function searchDoctorByLocation($pdo, $location, $type = null): bool|string +function searchDoctorByLocation($pdo, $location, $type = null): void { $postal = $location; @@ -292,11 +290,9 @@ function searchDoctorByLocation($pdo, $location, $type = null): bool|string $result = $query->fetchAll(); if(empty($result)){ - http_response_code(404); - return json_encode(array("message" => "Aucun résultat")); + Response::HTTP404(['error' => "Aucun résultat"]); } else { - http_response_code(200); - return json_encode($result); + Response::HTTP200($result); } }