[FEAT] Score API should be functional

This commit is contained in:
2020-11-13 12:49:32 +01:00
parent 1579e54c09
commit 6eb0cf9b83
2 changed files with 119 additions and 30 deletions

View File

@@ -5,12 +5,14 @@ class Score
private $conn;
private $score_path;
private $root_path;
// constructor with $db as database connection
public function __construct($db, $path)
public function __construct($db, $path, $root)
{
$this->conn = $db;
$this->score_path = $path;
$this->root_path = $root;
}
// read score list
@@ -30,8 +32,7 @@ class Score
sort($the_directory);
foreach ($the_directory as $entry_name) {
if (($entry_name != ".") && ($entry_name != "..")) {
$query = "SELECT * from repertoire where numero=$entry_name";
$query = "select * from repertoire where numero=$entry_name";
$resultat = $this->conn->execute($query);
$row = mysql_fetch_row($resultat);
@@ -45,30 +46,109 @@ class Score
array_push($scores_arr["scores"], $score_item);
}
}
$this->conn->close();
return $scores_arr;
}
// read products
public function read()
public function read_score($id)
{
$this->conn->connect();
// 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";
$query = "select * from repertoire where numero=$id";
$resultat = $this->conn->execute($query);
$row = mysql_fetch_row($resultat);
// prepare query statement
$stmt = $this->conn->prepare($query);
// row 2: nom
// row 3: compo
// execute query
$stmt->execute();
$name = $row[2];
$compositor = $row[3];
$instruments = array();
return $stmt;
$the_score_path = $this->score_path . $id;
$the_directory = opendir($the_score_path);
while ($entry_name = readdir($the_directory)) {
if (($entry_name != ".") && ($entry_name != "..")) {
$title = $this->get_entry_name($entry_name);
$the_partition_dir = opendir($the_score_path . "/" . $entry_name);
$number = array();
while ($the_num = readdir($the_partition_dir)) {
if (($the_num != ".") && ($the_num != "..")) {
$the_page_dir = opendir($the_score_path . "/" . $entry_name . "/" . $the_num);
$the_num_string = $the_num;
$pages = array();
while ($the_page = readdir($the_page_dir)) {
if (($the_page != ".") && ($the_page != "..")) {
$the_name = substr($the_page, 0, strpos($the_page, '.'));
$uri = $this->root_path . "/" . $id . "/" . $entry_name . "/" . $the_num . "/" . $the_page;
$page_item = array(
"name" => $the_name,
"uri" => $uri
);
array_push($pages, $page_item);
}
}
closedir($the_page_dir);
$number_item = array(
"id" => $the_num_string,
"pages" => $pages
);
array_push($number, $number_item);
}
}
closedir($the_partition_dir);
// Save instrument
$instrument_item = array(
"id" => $entry_name,
"title" => $title,
"number" => $number,
);
array_push($instruments, $instrument_item);
}
}
$result_item = array(
"id" => $id,
"name" => $row[2],
"compositor" => $row[3],
"instruments" => $instruments
);
return $result_item;
}
private function get_entry_name($name)
{
if ($name == "cla") return "Clarinette";
else if ($name == "flu") return "Flute";
else if ($name == "trb") return "Trombone";
else if ($name == "pic") return "Piccolo";
else if ($name == "per") return "Percussions";
else if ($name == "htb") return "Hautbois";
else if ($name == "trp") return "Trompette";
else if ($name == "dir") return "Direction";
else if ($name == "sax") return "Sax Alto";
else if ($name == "sat") return "Sax Ténor";
else if ($name == "sab") return "Sax Baryton";
else if ($name == "cor") return "Cor";
else if ($name == "eup") return "Euphonium";
else if ($name == "bas") return "Basson";
else if ($name == "cba") return "Contrebasse";
else if ($name == "crn") return "Cornet";
else if ($name == "coa") return "Cor Anglais";
else if ($name == "clb") return "Clarinette Basse";
else if ($name == "har") return "Harpe";
else if ($name == "pia") return "Piano";
else if ($name == "tub") return "Tuba";
else if ($name == "sup") return "Parties supplementaires";
else if ($name == "par") return "Parties";
else return $name;
}
}

View File

@@ -11,16 +11,25 @@ include_once '../objects/score.php';
$db = new Database();
// initialize object
$score = new Score($db, "../../Scores/");
$score = new Score($db, "../../Scores/", "http://ohmj2.free.fr/legacy/Scores");
$id = isset($_GET['id']) ? $_GET['id']: -1;
// read score will be here
// query scores
$scores_arr = $score->read_score_list();
if ($id == -1)
{
$payload = $score->read_score_list();
}
else
{
$payload = $score->read_score($id);
}
// set response code - 200 OK
http_response_code(200);
// show scores data in json format
echo json_encode($scores_arr);
echo json_encode($payload);
?>