-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
168 lines (143 loc) · 5.13 KB
/
Copy pathfunctions.php
File metadata and controls
168 lines (143 loc) · 5.13 KB
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
<?php
error_reporting(0); // !disable error messages
// TODO: connect with DB
$conn = mysqli_connect("kans.local", "root", "", "kans");
// TODO: check the connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// TODO: return response json
function retsponseJson(int $code, String $response)
{
$data = [
'status' => $code,
'message' => $response
];
echo json_encode($data);
}
// TODO:send users list via json
function getCustomerList()
{
global $conn;
// prepare query
$query = $conn->prepare("SELECT user_id, name, address, dob, is_active FROM user WHERE is_customer = ? AND is_active = ?;");
// Bind the parameters to the query
$is_customer = 1;
$is_active = 1;
$query->bind_param("ii", $is_customer, $is_active); // Both are integers
$query->execute();
$query_run = $query->get_result();
if ($query_run) {
if (mysqli_num_rows($query_run) > 0) {
$res = mysqli_fetch_all($query_run, MYSQLI_ASSOC);
$data = [
'status' => 200,
'message' => 'Customer List, Fetched Successfully',
'data' => $res,
];
header("HTTP/1.0 200 Customer List Fetched Successfully");
return json_encode($data);
} else {
header("HTTP/1.0 404 No Customer Found");
retsponseJson(404, "No Customer Found");
}
} else {
header("HTTP/1.0 500 Internal Server Error");
retsponseJson(500, "Internal Server Error");
}
}
// TODO: return error message
function error422(string $message)
{
header("HTTP/1.0 422 Unprocessable Entity");
retsponseJson(422, $message);
exit();
}
// TODO: create new customer on Sql DB
function storeCustomer($customerInput)
{
global $conn;
$name = mysqli_real_escape_string($conn, $customerInput['name']);
$email = mysqli_real_escape_string($conn, $customerInput['email']);
$phone = mysqli_real_escape_string($conn, $customerInput['phone']);
if (empty(trim($name))) return error422("Enter name to proceed");
elseif (empty(trim($email))) return error422("Enter email to proceed");
elseif (empty(trim($phone))) return error422("Enter mobile to proceed");
else {
$result = mysqli_query($conn, "INSERT INTO `user` (`name`, `email`, `mobile`) VALUES ('$name', '$email', '$phone') ");
if ($result) {
header("HTTP/1.0 201 Created");
retsponseJson(201, "Customer Created Successfully");
} else {
header("HTTP/1.0 500 Internal Server Error");
retsponseJson(500, "Internal Server Error");
}
}
}
// TODO: return specific customer data via json
function getCustomer($customerParams)
{
global $conn;
// default output
if ($customerParams['id'] == null) return error422('Enter Valid Id');
$customerId = mysqli_real_escape_string($conn, $customerParams['id']);
$query = "SELECT * FROM user WHERE user_id='$customerId' AND is_customer = 1 LIMIT 1";
$result = mysqli_query($conn, $query);
if ($result) {
if (mysqli_num_rows($result) == 1) {
$res = mysqli_fetch_assoc($result);
$data = [
'status' => 200,
'message' => 'Customer Fetched Successfully',
'data' => $res
];
header("HTTP/1.0 200 Customer Fetched Successfully");
return json_encode($data);
} else {
header("HTTP/1.0 400 Not found");
retsponseJson(404, "No Customer Found");
}
} else {
header("HTTP/1.0 500 Internal Server Error");
retsponseJson(500, "Internal Server Error");
}
}
// TODO: update customer on Sql DB
function updateCustomer($customerInput, $customerParams)
{
global $conn;
if (!isset($customerParams['id']) || $customerParams['id'] == null) return error422('Valide id not detected');
$customerId = mysqli_real_escape_string($conn, $customerParams['id']);
$name = mysqli_real_escape_string($conn, $customerInput['name']);
$email = mysqli_real_escape_string($conn, $customerInput['email']);
$phone = mysqli_real_escape_string($conn, $customerInput['phone']);
if (empty(trim($name))) return error422("Enter name to proceed");
elseif (empty(trim($email))) return error422("Enter email to proceed");
elseif (empty(trim($phone))) return error422("Enter mobile to proceed");
else {
$result = mysqli_query($conn, "UPDATE `user` SET `name` = '$name', `email` = '$email', `mobile` = '$phone' WHERE user_id = '$customerId' AND is_customer = 1");
if ($result) {
header("HTTP/1.0 200 Updated");
retsponseJson(200, "Customer Updated Successfully");
} else {
header("HTTP/1.0 500 Internal Server Error");
retsponseJson(500, "Internal Server Error");
}
}
}
// TODO: delete customer on Sql DB
function deleteCustomer($customerParams)
{
global $conn;
if (!isset($customerParams['id']) || $customerParams['id'] == null) return error422('Valide id not detected');
$customerId = mysqli_real_escape_string($conn, $customerParams['id']);
$result = mysqli_query($conn, "DELETE FROM `user` WHERE user_id = '$customerId' AND is_customer = 1");
if ($result) {
// !204 dosen't return json code to client
header("HTTP/1.0 200 DELETED");
retsponseJson(200, "Customer Deleted Successfully");
} else {
header("HTTP/1.0 404 Not found");
retsponseJson(404, "No Customer Found");
}
}