tangled
alpha
login
or
join now
stau.space
/
aecc-db
0
fork
atom
AECC database project.
0
fork
atom
overview
issues
pulls
pipelines
feat(delete): Finished delete api.
Diego A. Estrada Rivera
10 months ago
bcef292d
be677b5e
+47
-9
1 changed file
expand all
collapse all
unified
split
api
v1
delete
index.php
+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
6
+
function general_delete($table, $id_name) {
7
7
+
include("../../../lib/db.php");
8
8
+
$out = "";
9
9
+
if (isset($_POST[$id_name])) {
10
10
+
$stmt = $db -> prepare("DELETE FROM ${table} WHERE ${id_name} = ?;");
11
11
+
$stmt -> bind_param("i", $id);
12
12
+
$id = validate_input($_POST[$id_name]);
13
13
+
14
14
+
if ($stmt -> execute()) {
15
15
+
print header("HTTP/1.1 204 Succesfully deleted item in ${table} table.");
16
16
+
} else {
17
17
+
print header("HTTP/1.1 500 Internal server error ocurred while deleting item in ${table} table");
18
18
+
}
19
19
+
} else {
20
20
+
print header("HTTP/1.1 400 Missing ${id_name}");
21
21
+
$out = json_encode(err_msg(3));
22
22
+
}
23
23
+
$db->close();
24
24
+
25
25
+
return $out;
26
26
+
}
27
27
+
6
28
if (isset($_POST["t"])) {
7
29
$type = validate_input($_POST["t"]);
8
30
print match ($type) {
9
9
-
"product" => product(),
31
31
+
"activity" => general_delete("activity", "a_id"),
32
32
+
"activity_transaction" => activity_transaction(),
33
33
+
"board_member" => general_delete("board_member", "m_id"),
34
34
+
"member" => general_delete("member", "m_id"),
35
35
+
"product" => general_delete("product", "p_id"),
36
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
17
-
function product() {
44
44
+
function activity_transaction() {
18
45
include("../../../lib/db.php");
19
46
$out = "";
20
20
-
if (isset($_POST["p_id"])) {
21
21
-
$stmt = $db -> prepare("DELETE FROM product WHERE p_id = ?;");
22
22
-
$stmt -> bind_param("i", $id);
23
23
-
$id = validate_input($_POST["p_id"]);
47
47
+
if (isset($_POST["a_id"]) && isset($_POST["t_id"])) {
48
48
+
$stmt = $db -> prepare("DELETE FROM activity_transaction WHERE a_id = ? and t_id = ?;");
49
49
+
$stmt -> bind_param("ii", $a_id, $t_id);
50
50
+
$a_id = validate_input($_POST["a_id"]);
51
51
+
$t_id = validate_input($_POST["t_id"]);
24
52
25
53
if ($stmt -> execute()) {
26
26
-
print header("HTTP/1.1 204 Succesfully deleted product");
54
54
+
print header("HTTP/1.1 204 Succesfully deleted item in activity_transaction table.");
27
55
} else {
28
28
-
print header("HTTP/1.1 500 Internal server error ocurred while deleting product");
56
56
+
print header("HTTP/1.1 500 Internal server error ocurred while deleting item in activity_transaction table");
29
57
}
30
58
} else {
31
31
-
print header("HTTP/1.1 400 Missing p_id");
59
59
+
$msg = "HTTP/1.1 400 Missing ";
60
60
+
if (!isset($_POST["a_id"])) {
61
61
+
if (!isset($_POST["t_id"])) {
62
62
+
$msg = "a_id and t_id";
63
63
+
} else {
64
64
+
$msg = "a_id";
65
65
+
}
66
66
+
} else {
67
67
+
$msg = "t_id";
68
68
+
}
69
69
+
print header($msg);
32
70
$out = json_encode(err_msg(3));
33
71
}
34
72
$db->close();