tangled
alpha
login
or
join now
stau.space
/
aecc-db
0
fork
atom
AECC database project.
0
fork
atom
overview
issues
pulls
pipelines
fix!: Changed api to better specifi IDs.
Diego A. Estrada Rivera
10 months ago
516cabca
d99b06cf
+56
-10
2 changed files
expand all
collapse all
unified
split
api
v1
create
index.php
read
index.php
+32
-4
api/v1/create/index.php
···
2
2
print header('Content-Type: application/json');
3
3
include("../../../lib/header.php");
4
4
5
5
+
$thing = file_get_contents("php://input");
6
6
+
7
7
+
var_dump($thing);
8
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
15
+
"transaction" => transaction(),
11
16
default => header('HTTP/1.1 400 Bad Request: type not found')
12
17
};
13
18
} else {
14
14
-
print header('HTTP/1.1 400 Bad Request: type not specified');
19
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
29
-
$id = $stmt -> insert_id;
34
34
+
$p_id = $stmt -> insert_id;
30
35
print header("HTTP/1.1 201 Created");
31
31
-
print header("Location: https://ada.uprrp.edu/~diego.estrada1/CCOM/4027/db/api/v1/read.php?t=product&p_id=${id}");
32
32
-
$out .= json_encode(array("id" => $id, "cents" => $cents, "description" => $description));
36
36
+
print header("Location: https://ada.uprrp.edu/~diego.estrada1/CCOM/4027/db/api/v1/read.php?t=product&p_id=${p_id}");
37
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
47
+
include("../../../lib/db.php");
48
48
+
$out = "";
49
49
+
50
50
+
if (!(isset($_POST["type"]) || isset($_POST["date"]) || isset($_POST["quantity"]) || isset($_POST["p_id"]))) {
51
51
+
$out .= header('HTTP/1.1 400 Bad Request. You must supply `type`, `date`, `quantity` and `p_id`');
52
52
+
} else {
53
53
+
$stmt = $db -> prepare("INSERT INTO transaction (type, date, quantity, p_id) values (?, ?, ?, ?);");
54
54
+
$stmt -> bind_param("ssii", $type, $date, $quantity, $p_id);
55
55
+
$type = validate_input($_POST["type"]);
56
56
+
$date = validate_input($_POST["date"]);
57
57
+
$quantity = validate_input($_POST["quantity"]);
58
58
+
$p_id = validate_input($_POST["p_id"]);
59
59
+
60
60
+
if ($stmt -> execute()) {
61
61
+
$result = $stmt -> get_result();
62
62
+
$t_id = $stmt -> insert_id;
63
63
+
print header("HTTP/1.1 201 Created");
64
64
+
print header("Location: https://ada.uprrp.edu/~diego.estrada1/CCOM/4027/db/api/v1/read.php?t=transaction&t_id=${t_id}");
65
65
+
$out .= json_encode(array("t_id" => $t_id, "type" => $type, "date" => $date, "quantity" => $quantity, "p_id" => $p_id));
66
66
+
} else {
67
67
+
print header("HTTP/1.1 500 Something happened ???");
68
68
+
}
69
69
+
}
42
70
}
43
71
?>
+24
-6
api/v1/read/index.php
···
1
1
<?php
2
2
print header('Content-Type: application/json');
3
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
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
19
-
if (isset($_GET["id"])) {
19
19
+
$out = "";
20
20
+
21
21
+
if (isset($_GET["p_id"])) {
20
22
$stmt = $db -> prepare("SELECT * FROM product WHERE p_id = ?;");
21
21
-
$stmt -> bind_param("i", $id);
22
22
-
$id = validate_input(isset($_GET["id"]) ? $_GET["id"] : "");
23
23
+
$stmt -> bind_param("i", $p_id);
24
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
27
-
$q = validate_input(isset($_GET["q"]) ? $_GET["q"] : "");
29
29
+
$q = isset($_GET["q"]) ? validate_input($_GET["q"]) : "";
28
30
$stmt -> execute();
29
31
}
30
32
31
33
32
32
-
$out = json_encode($stmt -> get_result() -> fetch_all());
34
34
+
$out .= header("HTTP/1.1 201 Successfully got products.");
35
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
42
+
include("../../../lib/db.php");
39
43
$out = "";
40
44
45
45
+
if (isset($_GET["t_id"])) {
46
46
+
$stmt = $db -> prepare("SELECT * FROM transaction WHERE t_id = ?;");
47
47
+
$tmt -> bind_param("i", $t_id);
48
48
+
$t_id = validate_input($_GET["t_id"]);
49
49
+
$stmt -> execute();
50
50
+
} else {
51
51
+
$stmt = $db -> prepare("SELECT * FROM transaction;");
52
52
+
$stmt -> execute();
53
53
+
}
54
54
+
55
55
+
$out .= header("HTTP/1.1 201 Succesfully got transactions.");
56
56
+
$out .= json_encode($stmt -> get_result() -> fetch_all());
57
57
+
58
58
+
$db->close();
41
59
return $out;
42
60
}
43
61
?>