AECC database project.

feat(delete): Finished delete api.

+47 -9
+47 -9
api/v1/delete/index.php
··· 3 3 include("../../../lib/header.php"); 4 4 $_POST = json_decode(file_get_contents("php://input"), true); 5 5 6 + function general_delete($table, $id_name) { 7 + include("../../../lib/db.php"); 8 + $out = ""; 9 + if (isset($_POST[$id_name])) { 10 + $stmt = $db -> prepare("DELETE FROM ${table} WHERE ${id_name} = ?;"); 11 + $stmt -> bind_param("i", $id); 12 + $id = validate_input($_POST[$id_name]); 13 + 14 + if ($stmt -> execute()) { 15 + print header("HTTP/1.1 204 Succesfully deleted item in ${table} table."); 16 + } else { 17 + print header("HTTP/1.1 500 Internal server error ocurred while deleting item in ${table} table"); 18 + } 19 + } else { 20 + print header("HTTP/1.1 400 Missing ${id_name}"); 21 + $out = json_encode(err_msg(3)); 22 + } 23 + $db->close(); 24 + 25 + return $out; 26 + } 27 + 6 28 if (isset($_POST["t"])) { 7 29 $type = validate_input($_POST["t"]); 8 30 print match ($type) { 9 - "product" => product(), 31 + "activity" => general_delete("activity", "a_id"), 32 + "activity_transaction" => activity_transaction(), 33 + "board_member" => general_delete("board_member", "m_id"), 34 + "member" => general_delete("member", "m_id"), 35 + "product" => general_delete("product", "p_id"), 36 + "transaction" => general_delete("transaction", "t_id"), 10 37 default => header("HTTP/1.1 400 Incorrect table type") 11 38 }; 12 39 } else { ··· 14 41 print json_encode(err_msg(1)); 15 42 } 16 43 17 - function product() { 44 + function activity_transaction() { 18 45 include("../../../lib/db.php"); 19 46 $out = ""; 20 - if (isset($_POST["p_id"])) { 21 - $stmt = $db -> prepare("DELETE FROM product WHERE p_id = ?;"); 22 - $stmt -> bind_param("i", $id); 23 - $id = validate_input($_POST["p_id"]); 47 + if (isset($_POST["a_id"]) && isset($_POST["t_id"])) { 48 + $stmt = $db -> prepare("DELETE FROM activity_transaction WHERE a_id = ? and t_id = ?;"); 49 + $stmt -> bind_param("ii", $a_id, $t_id); 50 + $a_id = validate_input($_POST["a_id"]); 51 + $t_id = validate_input($_POST["t_id"]); 24 52 25 53 if ($stmt -> execute()) { 26 - print header("HTTP/1.1 204 Succesfully deleted product"); 54 + print header("HTTP/1.1 204 Succesfully deleted item in activity_transaction table."); 27 55 } else { 28 - print header("HTTP/1.1 500 Internal server error ocurred while deleting product"); 56 + print header("HTTP/1.1 500 Internal server error ocurred while deleting item in activity_transaction table"); 29 57 } 30 58 } else { 31 - print header("HTTP/1.1 400 Missing p_id"); 59 + $msg = "HTTP/1.1 400 Missing "; 60 + if (!isset($_POST["a_id"])) { 61 + if (!isset($_POST["t_id"])) { 62 + $msg = "a_id and t_id"; 63 + } else { 64 + $msg = "a_id"; 65 + } 66 + } else { 67 + $msg = "t_id"; 68 + } 69 + print header($msg); 32 70 $out = json_encode(err_msg(3)); 33 71 } 34 72 $db->close();