-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.php
201 lines (163 loc) · 7.37 KB
/
data.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
require_once('connection.php');
require_once('functions.php');
// buat variabel query
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
if(empty($_GET)) {
// Cek token pada setiap permintaan
// $headers = getallheaders();
// $apiToken = isset($headers['Auth']) ? trim(str_replace('Bearer', '', $headers['Auth'])) : '';
if ($apiToken === 'fira') {
$data = "SELECT * FROM mahasiswa";
$query = mysqli_query($conn, $data);
$result = query_data($query);
http_response_code(200);
// merubah menjadi json
echo json_encode (
array('result' => $result)
);
} else {
// Token tidak valid, beri respon error
http_response_code(401);
echo json_encode(array('message' => 'Unauthorized'));
}
//
} else if($_GET['id']) {
// ambil data dari header
$headers = getallheaders();
$apiToken = isset($headers['Auth']) ? trim(str_replace('Bearer', '', $headers['Auth'])) : '';
if($apiToken == 'fira') {
$data = "SELECT * FROM mahasiswa where id = " . $_GET['id'];
$query = mysqli_query($conn, $data);
$result = query_data($query);
if($result != []) {
// merubah menjadi json
http_response_code(200);
echo json_encode (
array('result' => $result)
);
} else {
http_response_code(404);
echo json_encode(array('message' => 'Data tidak di temukan.'));
}
} else {
// Token tidak valid, beri respon error
http_response_code(401);
echo json_encode(array('message' => 'Unauthorized'));
}
}
} else if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// ambil data dari header
$headers = getallheaders();
$apiToken = isset($headers['Auth']) ? trim(str_replace('Bearer', '', $headers['Auth'])) : '';
if($apiToken == 'fira') {
$input = json_decode(file_get_contents("php://input"), true);
if($input['nama'] == null || $input['npm'] == null || $input['kelamin'] == null) {
echo json_encode(array('message' => 'Masukkan semua data.'));
} else {
// Validate and sanitize input
$nama = mysqli_real_escape_string($conn, $input['nama']);
$npm = mysqli_real_escape_string($conn, $input['npm']);
$kelamin = mysqli_real_escape_string($conn, $input['kelamin']);
// Insert new data
$insertQuery = "INSERT INTO mahasiswa (nama, npm, kelamin) VALUES ('$nama', '$npm', '$kelamin')";
$insertResult = mysqli_query($conn, $insertQuery);
if ($insertResult) {
http_response_code(200);
echo json_encode(array('message' => 'Data inserted successfully.'));
} else {
http_response_code(400);
echo json_encode(array('message' => 'Failed to insert data.'));
}
}
} else {
http_response_code(401);
echo json_encode(array('message' => 'Unauthorized'));
}
} else if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
// ambil data dari header
$headers = getallheaders();
$apiToken = isset($headers['Auth']) ? trim(str_replace('Bearer', '', $headers['Auth'])) : '';
if($apiToken == 'fira') {
$input = json_decode(file_get_contents("php://input"), true);
// Pastikan id tidak kosong dan valid
if (isset($input['id']) && is_numeric($input['id'])) {
$id = mysqli_real_escape_string($conn, $input['id']);
// Cek apakah ID ada di database sebelum menghapus
$checkQuery = "SELECT * FROM mahasiswa WHERE id = $id";
$checkResult = mysqli_query($conn, $checkQuery);
if (mysqli_num_rows($checkResult) > 0) {
// Lakukan penghapusan
$deleteQuery = "DELETE FROM mahasiswa WHERE id = $id";
$deleteResult = mysqli_query($conn, $deleteQuery);
if ($deleteResult) {
http_response_code(200);
echo json_encode(array('message' => 'Data deleted successfully.'));
} else {
echo json_encode(array('message' => 'Failed to delete data.'));
}
} else {
http_response_code(404);
echo json_encode(array('status' => 404, 'message' => 'Data dengan ID tersebut tidak ditemukan.'));
}
} else {
echo json_encode(array('message' => 'ID tidak valid.'));
}
} else {
http_response_code(401);
echo json_encode(array('message' => 'Unauthorized'));
}
} else if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
$header = getallheaders();
$apiToken = (isset($header['Auth'])) ? trim(str_replace('Bearer', '', $header['Auth'])) : '';
if($apiToken == 'fira') {
$input = json_decode(file_get_contents("php://input"), true);
// Pastikan id tidak kosong dan valid
if (isset($input['id']) && is_numeric($input['id'])) {
$id = mysqli_real_escape_string($conn, $input['id']);
// Cek apakah ID ada di database sebelum mengupdate
$checkQuery = "SELECT * FROM mahasiswa WHERE id = $id";
$checkResult = mysqli_query($conn, $checkQuery);
if (mysqli_num_rows($checkResult) > 0) {
// Membangun kueri UPDATE berdasarkan data yang diterima
$updateFields = array();
if (isset($input['nama'])) {
$nama = mysqli_real_escape_string($conn, $input['nama']);
$updateFields[] = "nama = '$nama'";
}
if (isset($input['npm'])) {
$npm = mysqli_real_escape_string($conn, $input['npm']);
$updateFields[] = "npm = '$npm'";
}
if (isset($input['kelamin'])) {
$kelamin = mysqli_real_escape_string($conn, $input['kelamin']);
$updateFields[] = "kelamin = '$kelamin'";
}
// Pastikan ada setidaknya satu kolom yang akan diupdate
if (!empty($updateFields)) {
$updateQuery = "UPDATE mahasiswa SET " . implode(', ', $updateFields) . " WHERE id = $id";
$updateResult = mysqli_query($conn, $updateQuery);
if ($updateResult) {
http_response_code(200);
echo json_encode(array('message' => 'Data updated successfully.'));
} else {
http_response_code(400);
echo json_encode(array('message' => 'Failed to update data.'));
}
} else {
echo json_encode(array('message' => 'Tidak ada data yang akan diupdate.'));
}
} else {
http_response_code(404);
echo json_encode(array('status' => 404, 'message' => 'Data dengan ID tersebut tidak ditemukan.'));
}
} else {
http_response_code(404);
echo json_encode(array('message' => 'ID tidak valid.'));
}
} else {
http_response_code(401);
echo json_encode(array('message' => 'Unauthorized'));
}
}
?>