AECC database project.

fix!: Changed api to better specifi IDs.

+56 -10
+32 -4
api/v1/create/index.php
··· 2 2 print header('Content-Type: application/json'); 3 3 include("../../../lib/header.php"); 4 4 5 + $thing = file_get_contents("php://input"); 6 + 7 + var_dump($thing); 8 + 5 9 $_POST = json_decode(file_get_contents("php://input"), true); 6 10 7 11 if (isset($_POST["t"])) { 8 12 $type = validate_input($_POST["t"]); 9 13 print match ($type) { 10 14 "product" => product(), 15 + "transaction" => transaction(), 11 16 default => header('HTTP/1.1 400 Bad Request: type not found') 12 17 }; 13 18 } else { 14 - print header('HTTP/1.1 400 Bad Request: type not specified'); 19 + print header('HTTP/1.1 420 Bad Request: type not specified'); 15 20 print json_encode(err_msg(1)); 16 21 } 17 22 ··· 26 31 $out = ""; 27 32 if ($stmt -> execute()) { 28 33 $result = $stmt -> get_result(); 29 - $id = $stmt -> insert_id; 34 + $p_id = $stmt -> insert_id; 30 35 print header("HTTP/1.1 201 Created"); 31 - print header("Location: https://ada.uprrp.edu/~diego.estrada1/CCOM/4027/db/api/v1/read.php?t=product&p_id=${id}"); 32 - $out .= json_encode(array("id" => $id, "cents" => $cents, "description" => $description)); 36 + print header("Location: https://ada.uprrp.edu/~diego.estrada1/CCOM/4027/db/api/v1/read.php?t=product&p_id=${p_id}"); 37 + $out .= json_encode(array("p_id" => $p_id, "cents" => $cents, "description" => $description)); 33 38 } else { 34 39 print header("HTTP/1.1 500 Something happened ???"); 35 40 } ··· 39 44 } 40 45 41 46 function transaction() { 47 + include("../../../lib/db.php"); 48 + $out = ""; 49 + 50 + if (!(isset($_POST["type"]) || isset($_POST["date"]) || isset($_POST["quantity"]) || isset($_POST["p_id"]))) { 51 + $out .= header('HTTP/1.1 400 Bad Request. You must supply `type`, `date`, `quantity` and `p_id`'); 52 + } else { 53 + $stmt = $db -> prepare("INSERT INTO transaction (type, date, quantity, p_id) values (?, ?, ?, ?);"); 54 + $stmt -> bind_param("ssii", $type, $date, $quantity, $p_id); 55 + $type = validate_input($_POST["type"]); 56 + $date = validate_input($_POST["date"]); 57 + $quantity = validate_input($_POST["quantity"]); 58 + $p_id = validate_input($_POST["p_id"]); 59 + 60 + if ($stmt -> execute()) { 61 + $result = $stmt -> get_result(); 62 + $t_id = $stmt -> insert_id; 63 + print header("HTTP/1.1 201 Created"); 64 + print header("Location: https://ada.uprrp.edu/~diego.estrada1/CCOM/4027/db/api/v1/read.php?t=transaction&t_id=${t_id}"); 65 + $out .= json_encode(array("t_id" => $t_id, "type" => $type, "date" => $date, "quantity" => $quantity, "p_id" => $p_id)); 66 + } else { 67 + print header("HTTP/1.1 500 Something happened ???"); 68 + } 69 + } 42 70 } 43 71 ?>
+24 -6
api/v1/read/index.php
··· 1 1 <?php 2 2 print header('Content-Type: application/json'); 3 - 4 3 include("../../../lib/header.php"); 5 4 6 5 if (isset($_GET["t"])) { 7 6 $type = validate_input($_GET["t"]); 8 7 print match($type) { 9 8 "product" => product(), 9 + "transaction" => transaction(), 10 10 default => json_encode(err_msg(3)) 11 11 }; 12 12 } else { ··· 16 16 function product() { 17 17 include("../../../lib/db.php"); 18 18 19 - if (isset($_GET["id"])) { 19 + $out = ""; 20 + 21 + if (isset($_GET["p_id"])) { 20 22 $stmt = $db -> prepare("SELECT * FROM product WHERE p_id = ?;"); 21 - $stmt -> bind_param("i", $id); 22 - $id = validate_input(isset($_GET["id"]) ? $_GET["id"] : ""); 23 + $stmt -> bind_param("i", $p_id); 24 + $p_id = validate_input($_GET["p_id"]); 23 25 $stmt -> execute(); 24 26 } else { 25 27 $stmt = $db -> prepare("SELECT * FROM product WHERE MATCH (description) AGAINST (? WITH QUERY EXPANSION) or ? = \"\";"); 26 28 $stmt -> bind_param("ss", $q, $q); 27 - $q = validate_input(isset($_GET["q"]) ? $_GET["q"] : ""); 29 + $q = isset($_GET["q"]) ? validate_input($_GET["q"]) : ""; 28 30 $stmt -> execute(); 29 31 } 30 32 31 33 32 - $out = json_encode($stmt -> get_result() -> fetch_all()); 34 + $out .= header("HTTP/1.1 201 Successfully got products."); 35 + $out .= json_encode($stmt -> get_result() -> fetch_all()); 33 36 34 37 $db->close(); 35 38 return $out; 36 39 } 37 40 38 41 function transaction() { 42 + include("../../../lib/db.php"); 39 43 $out = ""; 40 44 45 + if (isset($_GET["t_id"])) { 46 + $stmt = $db -> prepare("SELECT * FROM transaction WHERE t_id = ?;"); 47 + $tmt -> bind_param("i", $t_id); 48 + $t_id = validate_input($_GET["t_id"]); 49 + $stmt -> execute(); 50 + } else { 51 + $stmt = $db -> prepare("SELECT * FROM transaction;"); 52 + $stmt -> execute(); 53 + } 54 + 55 + $out .= header("HTTP/1.1 201 Succesfully got transactions."); 56 + $out .= json_encode($stmt -> get_result() -> fetch_all()); 57 + 58 + $db->close(); 41 59 return $out; 42 60 } 43 61 ?>