Skip to content

Commit 002c548

Browse files
committed
add new api endpoint to delete user data
1 parent 8768c4c commit 002c548

3 files changed

Lines changed: 53 additions & 6 deletions

File tree

‎src/controllers/UsersController.php‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public function getAllUser() : void {
1313
try {
1414
$result = $this->user->getAllUser();
1515

16-
header('Content-Type: application/json');
1716
http_response_code(200);
17+
header('Content-Type: application/json');
1818
echo json_encode([
1919
"status"=>"success",
2020
"message"=> "successfully get all user data from database",
@@ -29,14 +29,14 @@ public function getAllUser() : void {
2929
}
3030
}
3131

32-
public function getUserById($id) : void {
32+
public function getUserById($userId) : void {
3333
try {
34-
$result = $this->user->getUserById($id);
34+
$result = $this->user->getUserById($userId);
3535

3636
$message = empty($result) ? "User not found" : "Successfully retrieved user data";
3737

38-
header('Content-Type: application/json');
3938
http_response_code(empty($result) ? 404 : 200);
39+
header('Content-Type: application/json');
4040
echo json_encode([
4141
"status"=>"success",
4242
"message"=> $message,
@@ -66,8 +66,8 @@ public function addNewUser() : void {
6666
$message = "Successfully add new user data";
6767
}
6868

69-
header('Content-Type: application/json');
7069
http_response_code(empty($result) ? 400 : 201);
70+
header('Content-Type: application/json');
7171
echo json_encode([
7272
"status"=>"success",
7373
"message"=> $message,
@@ -128,8 +128,8 @@ public function editUserData($userId) : void {
128128

129129
$result = $this->user->getUserById($userId);
130130

131-
header('Content-Type: application/json');
132131
http_response_code(200);
132+
header('Content-Type: application/json');
133133
echo json_encode([
134134
"status"=>"success",
135135
"message"=> "successfully update data",
@@ -143,4 +143,32 @@ public function editUserData($userId) : void {
143143
]);
144144
}
145145
}
146+
147+
public function deleteUserById($userId) : void {
148+
try {
149+
if (empty($this->user->getUserById($userId))) {
150+
http_response_code(404);
151+
echo json_encode([
152+
"status"=> "success",
153+
"message"=> "user not found"
154+
]);
155+
exit;
156+
}
157+
158+
$this->user->deleteUserById($userId);
159+
160+
http_response_code(200);
161+
header('Content-Type: application/json');
162+
echo json_encode([
163+
"status"=>"success",
164+
"message"=> "successfully update data"
165+
]);
166+
} catch (Exception $e) {
167+
http_response_code(500);
168+
echo json_encode([
169+
"status"=> "error",
170+
"message"=> "server error occured"
171+
]);
172+
}
173+
}
146174
}

‎src/models/User.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,20 @@ public function editUserJob($id, $newJob) : void {
131131
error_log("Query failed: " . $e->getMessage(), 3, '../../logs/error.log');
132132
}
133133
}
134+
135+
public function deleteUserById($userId) : void {
136+
try {
137+
$query = "DELETE FROM user WHERE id = ?";
138+
139+
if ($this->conn === null) {
140+
throw new Exception("Database connection is null.");
141+
}
142+
143+
$stmt = $this->conn->prepare($query);
144+
$stmt->bindParam(1, $userId, PDO::PARAM_INT);
145+
$stmt->execute();
146+
} catch (PDOException $e) {
147+
error_log("Query failed: " . $e->getMessage(), 3, '../../logs/error.log');
148+
}
149+
}
134150
}

‎src/routes/api.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
case "PATCH":
3939
$usersController->editUserData($userId);
4040
break;
41+
case "DELETE":
42+
$usersController->deleteUserById($userId);
43+
break;
4144
default:
4245
sendResponse405();
4346
break;

0 commit comments

Comments
 (0)