Skip to content

Commit 8768c4c

Browse files
committed
add new api endpoint to edit user data
1 parent c74a9f5 commit 8768c4c

3 files changed

Lines changed: 119 additions & 2 deletions

File tree

‎src/controllers/UsersController.php‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,66 @@ public function addNewUser() : void {
8181
]);
8282
}
8383
}
84+
85+
public function editUserData($userId) : void {
86+
try {
87+
if (empty($this->user->getUserById($userId))) {
88+
http_response_code(404);
89+
echo json_encode([
90+
"status"=> "success",
91+
"message"=> "user not found"
92+
]);
93+
exit;
94+
}
95+
96+
97+
$inputData = json_decode(file_get_contents('php://input'), true);
98+
99+
if ($inputData === null) {
100+
http_response_code(400);
101+
echo json_encode([
102+
"status" => "error",
103+
"message"=> "data input is empty"
104+
]);
105+
exit;
106+
}
107+
108+
if (!isset($inputData['name']) && !isset($inputData['age']) && !isset($inputData['job'])) {
109+
http_response_code(422);
110+
echo json_encode([
111+
"status" => "error",
112+
"message"=> "required data missing"
113+
]);
114+
exit;
115+
}
116+
117+
if (isset($inputData["name"])) {
118+
$this->user->editUserName($userId,$inputData["name"]);
119+
}
120+
121+
if (isset($inputData["age"])) {
122+
$this->user->editUserAge($userId, $inputData["age"]);
123+
}
124+
125+
if (isset($inputData["job"])) {
126+
$this->user->editUserAge($userId, $inputData["job"]);
127+
}
128+
129+
$result = $this->user->getUserById($userId);
130+
131+
header('Content-Type: application/json');
132+
http_response_code(200);
133+
echo json_encode([
134+
"status"=>"success",
135+
"message"=> "successfully update data",
136+
"data"=> $result
137+
]);
138+
} catch (Exception $e) {
139+
http_response_code(500);
140+
echo json_encode([
141+
"status"=> "error",
142+
"message"=> "server error occured"
143+
]);
144+
}
145+
}
84146
}

‎src/models/User.php‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,55 @@ public function addNewUser($name, $age, $job) : void {
8080
error_log("Query failed: " . $e->getMessage(), 3, '../../logs/error.log');
8181
}
8282
}
83+
84+
public function editUserName($id, $newName) : void {
85+
try {
86+
$query = "UPDATE user SET name = ? WHERE id = ?";
87+
88+
if ($this->conn === null) {
89+
throw new Exception("Database connection is null.");
90+
}
91+
92+
$stmt = $this->conn->prepare($query);
93+
$stmt->bindParam(1, $newName, PDO::PARAM_STR);
94+
$stmt->bindParam(2, $id, PDO::PARAM_INT);
95+
$stmt->execute();
96+
} catch (PDOException $e) {
97+
error_log("Query failed: " . $e->getMessage(), 3, '../../logs/error.log');
98+
}
99+
}
100+
101+
public function editUserAge($id, $newAge) : void {
102+
try {
103+
$query = "UPDATE user SET age = ? WHERE id = ?";
104+
105+
if ($this->conn === null) {
106+
throw new Exception("Database connection is null.");
107+
}
108+
109+
$stmt = $this->conn->prepare($query);
110+
$stmt->bindParam(1, $newAge, PDO::PARAM_STR);
111+
$stmt->bindParam(2, $id, PDO::PARAM_INT);
112+
$stmt->execute();
113+
} catch (PDOException $e) {
114+
error_log("Query failed: " . $e->getMessage(), 3, '../../logs/error.log');
115+
}
116+
}
117+
118+
public function editUserJob($id, $newJob) : void {
119+
try {
120+
$query = "UPDATE user SET job = ? WHERE id = ?";
121+
122+
if ($this->conn === null) {
123+
throw new Exception("Database connection is null.");
124+
}
125+
126+
$stmt = $this->conn->prepare($query);
127+
$stmt->bindParam(1, $newJob, PDO::PARAM_STR);
128+
$stmt->bindParam(2, $id, PDO::PARAM_INT);
129+
$stmt->execute();
130+
} catch (PDOException $e) {
131+
error_log("Query failed: " . $e->getMessage(), 3, '../../logs/error.log');
132+
}
133+
}
83134
}

‎src/routes/api.php‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
echo "You are in the root!";
1515
break;
1616
default:
17-
sendResponse405() ;
17+
sendResponse405();
1818
break;
1919
}
2020
} elseif ($requestUri == "/api/users") {
@@ -35,6 +35,9 @@
3535
case "GET":
3636
$usersController->getUserById($userId);
3737
break;
38+
case "PATCH":
39+
$usersController->editUserData($userId);
40+
break;
3841
default:
3942
sendResponse405();
4043
break;
@@ -45,7 +48,8 @@
4548
}
4649

4750

48-
function sendResponse405() {
51+
function sendResponse405()
52+
{
4953
http_response_code(405);
5054
echo json_encode(["message" => "Method Not Allowed"]);
5155
}

0 commit comments

Comments
 (0)