From 1579e54c0909a1d23a7867c14986aff04adb0a10 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Thu, 12 Nov 2020 22:21:52 +0100 Subject: [PATCH] [FEAT] Add API to read the list of scores --- .vscode/sftp.json | 10 ++++++ README.md | 4 +++ api/config/database.php | 28 ++++++++++++++++ api/objects/score.php | 74 +++++++++++++++++++++++++++++++++++++++++ api/score/read.php | 26 +++++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 .vscode/sftp.json create mode 100644 README.md create mode 100644 api/config/database.php create mode 100644 api/objects/score.php create mode 100644 api/score/read.php diff --git a/.vscode/sftp.json b/.vscode/sftp.json new file mode 100644 index 0000000..1a656e0 --- /dev/null +++ b/.vscode/sftp.json @@ -0,0 +1,10 @@ +{ + "name": "FTP Perso", + "host": "ftpperso.free.fr", + "protocol": "ftp", + "port": 21, + "username": "ohmj2", + "password": "2j9eywip", + "remotePath": "/legacy/", + "uploadOnSave": true +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..5c901dd --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ + + +https://codeofaninja.com/2017/02/create-simple-rest-api-in-php.html + diff --git a/api/config/database.php b/api/config/database.php new file mode 100644 index 0000000..b2a45e4 --- /dev/null +++ b/api/config/database.php @@ -0,0 +1,28 @@ +connect_db = @mysql_connect ($this->host, $this->username, $this->password); + return true; + } + + public function close () + { + MYSQL_CLOSE; + } + + public function execute($query) + { + return mysql_db_query($this->db_name, $query, $this->connect_db); + } +} diff --git a/api/objects/score.php b/api/objects/score.php new file mode 100644 index 0000000..8e22bf2 --- /dev/null +++ b/api/objects/score.php @@ -0,0 +1,74 @@ +conn = $db; + $this->score_path = $path; + } + + // read score list + public function read_score_list() + { + $scores_arr=array(); + $scores_arr["scores"]=array(); + + $this->conn->connect(); + //$theDirectory = opendir($this->mScorePath); + // row 0: id + // row 1: numero + // row 2: nom + // row 3: compo + // row 4: style + $the_directory = scandir($this->score_path); + sort($the_directory); + foreach ($the_directory as $entry_name) { + if (($entry_name != ".") && ($entry_name != "..")) { + + $query = "SELECT * from repertoire where numero=$entry_name"; + $resultat = $this->conn->execute($query); + $row = mysql_fetch_row($resultat); + + $score_item=array( + "id" => $row[1], + "name" => $row[2], + "compositor" => $row[3], + "style" => $row[4] + ); + + array_push($scores_arr["scores"], $score_item); + } + } + return $scores_arr; + } + + // read products + public function read() + { + + // select all query + $query = "SELECT + c.name as category_name, p.id, p.name, p.description, p.price, p.category_id, p.created + FROM + " . $this->table_name . " p + LEFT JOIN + categories c + ON p.category_id = c.id + ORDER BY + p.created DESC"; + + // prepare query statement + $stmt = $this->conn->prepare($query); + + // execute query + $stmt->execute(); + + return $stmt; + } +} diff --git a/api/score/read.php b/api/score/read.php new file mode 100644 index 0000000..ee57845 --- /dev/null +++ b/api/score/read.php @@ -0,0 +1,26 @@ +read_score_list(); + +// set response code - 200 OK +http_response_code(200); + +// show scores data in json format +echo json_encode($scores_arr); + +?>