[FEAT] Add API to read the list of scores
This commit is contained in:
10
.vscode/sftp.json
vendored
Normal file
10
.vscode/sftp.json
vendored
Normal 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
4
README.md
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
|
||||
https://codeofaninja.com/2017/02/create-simple-rest-api-in-php.html
|
||||
|
||||
28
api/config/database.php
Normal file
28
api/config/database.php
Normal 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
74
api/objects/score.php
Normal 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
26
api/score/read.php
Normal 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);
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user