AECC database project.
1<?php
2print header('Content-Type: application/json');
3include("../../../lib/header.php");
4
5$thing = file_get_contents("php://input");
6
7$_POST = json_decode(file_get_contents("php://input"), true);
8
9if (isset($_POST["t"])) {
10 $type = validate_input($_POST["t"]);
11 print match ($type) {
12 "activity" => activity(),
13 "activity_transaction" => activity_transaction(),
14 "board_member" => board_member(),
15 "member" => member(),
16 "product" => product(),
17 "transaction" => transaction(),
18 default => header('HTTP/1.1 400 Bad Request: type not found')
19 };
20} else {
21 print header('HTTP/1.1 420 Bad Request: type not specified');
22}
23
24function activity() {
25 include("../../../lib/db.php");
26 $out = "";
27
28 if (!(isset($_POST["title"]) || isset($_POST["description"]) || isset($_POST["date"]))) {
29 $out .= header('HTTP/1.1 400 Bad Request. You must supply `title`, `description` and `date`.');
30 } else {
31 $stmt = $db -> prepare("INSERT INTO activity (title, description, date) VALUES (?, ?, ?);");
32 $stmt -> bind_param("sss", $title, $description, $date);
33 $title = validate_input($_POST["title"]);
34 $description = validate_input($_POST["description"]);
35 $date = validate_input($_POST["date"]);
36 if ($stmt -> execute()) {
37 $result = $stmt -> get_result();
38 $a_id = $stmt -> insert_id;
39 print header("HTTP/1.1 201 Created");
40 print header("Location: https://ada.uprrp.edu/~diego.estrada1/CCOM/4027/db/api/v1/read/?t=activity&a_id=${a_id}");
41 $out .= json_encode(array("a_id" => $a_id, "title" => $title, "description" => $description, "date" => $date));
42 } else {
43 print header("HTTP/1.1 500 Something happened ???");
44 }
45 }
46
47 $db -> close();
48 return $out;
49}
50
51function activity_transaction() {
52 include("../../../lib/db.php");
53 $out = "";
54 if (!(isset($_POST["a_id"]) || isset($_POST["t_id"]))) {
55 $out .= header('HTTP/1.1 400 Bad Request. You must supply `a_id` and `t_id`.');
56 } else {
57 $stmt = $db -> prepare("INSERT INTO activity_transaction (a_id, t_id) VALUES (?, ?);");
58 $stmt -> bind_param("ii", $a_id, $t_id);
59 $a_id = validate_input($_POST["a_id"]);
60 $t_id = validate_input($_POST["t_id"]);
61 if ($stmt -> execute()) {
62 print header("HTTP/1.1 201 Created");
63 print header("Location: https://ada.uprrp.edu/~diego.estrada1/CCOM/4027/db/api/v1/read?t=activity_transaction&a_id=${a_id}&t_id=${t_id}");
64 $out .= json_encode(array("a_id" => $a_id, "t_id" => $t_id));
65 } else {
66 print header("HTTP/1.1 500 Something happened ???");
67 }
68 }
69 $db -> close();
70 return $out;
71}
72
73function board_member() {
74 include("../../../lib/db.php");
75 $out = "";
76 if (!(isset($_POST["m_id"]) || isset($_POST["position"]) || isset($_POST["year"]) || isset($_POST["role"]) || isset($_POST["password"]))) {
77 $out .= header('HTTP/1.1 400 Bad Request. You must supply `m_id`, `position`, `year`, `role` and `password`.');
78 } else {
79 $stmt = $db -> prepare("INSERT INTO board_member (m_id, position, year, role, password) VALUES (?, ?, ?, ?, ?);");
80 $stmt -> bind_param("ssiss", $m_id, $position, $year, $role, $password);
81 $m_id = validate_input($_POST["m_id"]);
82 $position = validate_input($_POST["position"]);
83 $year = validate_input($_POST["year"]);
84 $role = validate_input($_POST["role"]);
85 $password = validate_input($_POST["password"]);
86
87 if ($stmt -> execute()) {
88 $result = $stmt -> get_result();
89 $p_id = $stmt -> insert_id;
90 print header("HTTP/1.1 201 Created");
91 print header("Location: https://ada.uprrp.edu/~diego.estrada1/CCOM/4027/db/api/v1/read/?t=board_member&m_id=${m_id}");
92 $out .= json_encode(array("m_id" => $m_id, "position" => $position, "year" => $year, "role" => $role, "password" => $password));
93 } else {
94 print header("HTTP/1.1 500 Something happened ???");
95 }
96 }
97 $db->close();
98 return $out;
99}
100
101function member() {
102 include("../../../lib/db.php");
103 $out = "";
104
105 if (!(isset($_POST["name"]) || isset($_POST["second_name"]) || isset($_POST["last_name"]) || isset($_POST["second_last_name"]) || isset($_POST["email"]) || isset($_POST["phone_number"]) || isset($_POST["status"]))) {
106 $out .= header('HTTP/1.1 400 Bad Request. You must supply `name`, `second_name`, `last_name`, `second_last_name`, `email`, `phone_number` and `status`.');
107 } else {
108 $stmt = $db -> prepare("INSERT INTO product (name, second_name, last_name, second_last_name, email, phone_number, status) VALUES (?, ?, ?, ?, ?, ?, ?);");
109 $stmt -> bind_param("sssssss", $name, $second_name, $last_name, $second_last_name, $email, $phone_number, $status);
110 $name = validate_input($_POST["name"]);
111 $second_name = validate_input($_POST["second_name"]);
112 $last_name = validate_input($_POST["last_name"]);
113 $second_last_name = validate_input($_POST["second_last_name"]);
114 $email = validate_input($_POST["email"]);
115 $phone_number = validate_input($_POST["phone_number"]);
116 $status = validate_input($_POST["status"]);
117
118 if ($stmt -> execute()) {
119 $result = $stmt -> get_result();
120 $m_id = $stmt -> insert_id;
121 print header("HTTP/1.1 201 Created");
122 print header("Location: https://ada.uprrp.edu/~diego.estrada1/CCOM/4027/db/api/v1/read/?t=member&m_id=${m_id}");
123 $out .= json_encode(array("m_id" => $m_id, "name" => $name, "second_name" => $second_name, "last_name" => $last_name, "second_last_name" => $second_last_name, "email" => $email, "phone_number" => $phone_number, "status" => $status));
124 } else {
125 print header("HTTP/1.1 500 Something happened ???");
126 }
127 }
128
129 $db -> close();
130 return $out;
131}
132
133function product() {
134 include("../../../lib/db.php");
135
136 $out = "";
137 if (!(isset($_POST["cents"]) || isset($_POST["description"]))) {
138 $out .= header('HTTP/1.1 400 Bad Request. You must supply `cents` and `description`.');
139 } else {
140 $stmt = $db -> prepare("INSERT INTO product (cents, description) VALUES (?, ?);");
141 $stmt -> bind_param("is", $cents, $description);
142 $cents = validate_input($_POST["cents"]);
143 $description = validate_input($_POST["description"]);
144
145 if ($stmt -> execute()) {
146 $result = $stmt -> get_result();
147 $p_id = $stmt -> insert_id;
148 print header("HTTP/1.1 201 Created");
149 print header("Location: https://ada.uprrp.edu/~diego.estrada1/CCOM/4027/db/api/v1/read/?t=product&p_id=${p_id}");
150 $out .= json_encode(array("p_id" => $p_id, "cents" => $cents, "description" => $description));
151 } else {
152 print header("HTTP/1.1 500 Something happened ???");
153 }
154 }
155
156 $db -> close();
157 return $out;
158}
159
160function transaction() {
161 include("../../../lib/db.php");
162 $out = "";
163
164 if (!(isset($_POST["type"]) || isset($_POST["date"]) || isset($_POST["quantity"]) || isset($_POST["p_id"]))) {
165 $out .= header('HTTP/1.1 400 Bad Request. You must supply `type`, `date`, `quantity` and `p_id`');
166 } else {
167 $stmt = $db -> prepare("INSERT INTO transaction (type, date, quantity, p_id) values (?, ?, ?, ?);");
168 $stmt -> bind_param("ssii", $type, $date, $quantity, $p_id);
169 $type = validate_input($_POST["type"]);
170 $date = validate_input($_POST["date"]);
171 $quantity = validate_input($_POST["quantity"]);
172 $p_id = validate_input($_POST["p_id"]);
173
174 if ($stmt -> execute()) {
175 $result = $stmt -> get_result();
176 $t_id = $stmt -> insert_id;
177 print header("HTTP/1.1 201 Created");
178 print header("Location: https://ada.uprrp.edu/~diego.estrada1/CCOM/4027/db/api/v1/read/?t=transaction&t_id=${t_id}");
179 $out .= json_encode(array("t_id" => $t_id, "type" => $type, "date" => $date, "quantity" => $quantity, "p_id" => $p_id));
180 } else {
181 print header("HTTP/1.1 500 Something happened ???");
182 }
183 }
184}
185?>