[FEAT] Add API to read the list of scores

This commit is contained in:
2020-11-12 22:21:52 +01:00
parent cf38fa26d7
commit 1579e54c09
5 changed files with 142 additions and 0 deletions

10
.vscode/sftp.json vendored Normal file
View File

@@ -0,0 +1,10 @@
{
"name": "FTP Perso",
"host": "ftpperso.free.fr",
"protocol": "ftp",
"port": 21,
"username": "ohmj2",
"password": "2j9eywip",
"remotePath": "/legacy/",
"uploadOnSave": true
}

4
README.md Normal file
View File

@@ -0,0 +1,4 @@
https://codeofaninja.com/2017/02/create-simple-rest-api-in-php.html

28
api/config/database.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
class Database{
// specify your own database credentials
private $host = "localhost";
private $db_name = "ohmj2";
private $username = "ohmj2";
private $password = "2j9eywip";
private $connect_db;
public $conn;
public function connect ()
{
$this->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);
}
}

74
api/objects/score.php Normal file
View File

@@ -0,0 +1,74 @@
<?php
class Score
{
// database connection and table name
private $conn;
private $score_path;
// constructor with $db as database connection
public function __construct($db, $path)
{
$this->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;
}
}

26
api/score/read.php Normal file
View File

@@ -0,0 +1,26 @@
<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
// include database and object files
include_once '../config/database.php';
include_once '../objects/score.php';
// instantiate database and score object
$db = new Database();
// initialize object
$score = new Score($db, "../../Scores/");
// read score will be here
// query scores
$scores_arr = $score->read_score_list();
// set response code - 200 OK
http_response_code(200);
// show scores data in json format
echo json_encode($scores_arr);
?>