mirror of
https://github.com/BreizhHardware/ProjetS4COMWEB.git
synced 2026-01-18 16:47:35 +01:00
Search with rest API
This commit is contained in:
4
Request_Test/testHTTP.http
Normal file
4
Request_Test/testHTTP.http
Normal file
@@ -0,0 +1,4 @@
|
||||
GET http://serveur-projet-s4.felix/src/API/requests.php/api/search?type=Généraliste
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
@@ -1,6 +1,13 @@
|
||||
<?php
|
||||
|
||||
require_once 'src/router.php';
|
||||
require_once '../php/constants.php';
|
||||
require_once '../php/db/dbconnect.php';
|
||||
require_once '../php/db/Search.php';
|
||||
ini_set('display_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
$pdo = dbConnect();
|
||||
|
||||
|
||||
$router = new Router();
|
||||
|
||||
@@ -8,6 +15,22 @@ $router->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);
|
||||
});
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<?php
|
||||
|
||||
require_once '../API/src/response.php';
|
||||
/*
|
||||
function search($pdo, $nom, $postal){
|
||||
if($_POST["nom"] == null && $_POST["postal"] == null){
|
||||
@@ -229,7 +231,7 @@ function search($pdo, $nom, $postal){
|
||||
|
||||
*/
|
||||
// searchDoctor search doctor by name or specialty and return the result in JSON format
|
||||
function searchDoctor($pdo, $type): bool|string
|
||||
function searchDoctor($pdo, $type): void
|
||||
{
|
||||
$query = $pdo->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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user