Skip to content

Commit dee4ed3

Browse files
committed
add new api endpoint to get user data by id
1 parent 49f5035 commit dee4ed3

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎src/controllers/UsersController.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,11 @@ public function getAllUser() {
1515
header('Content-Type: application/json');
1616
echo json_encode($result);
1717
}
18+
19+
public function getUserById($id) {
20+
$result = $this->user->getUserById($id);
21+
22+
header('Content-Type: application/json');
23+
echo json_encode($result);
24+
}
1825
}

‎src/models/User.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,14 @@ public function getAllUser() : array {
1818
return [];
1919
}
2020
}
21+
22+
public function getUserById($id) : array {
23+
if ($this->conn) {
24+
$query = "SELECT * FROM user WHERE id=" . $id;
25+
$stmt = $this->conn->query($query);
26+
return $stmt->fetchAll(PDO::FETCH_ASSOC);
27+
} else {
28+
return [];
29+
}
30+
}
2131
}

‎src/routes/api.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
echo "You are in the root page!";
1212
} elseif ($requestUri == "/users" && $requestMethod == "GET") {
1313
$usersController->getAllUser();
14+
} elseif (preg_match('#^/users/(\d+)$#', $requestUri, $matches) && $requestMethod == "GET") {
15+
$userId = $matches[1];
16+
$usersController->getUserById($userId);
1417
} else {
1518
http_response_code(404);
1619
echo "Error 404! No route found!";

0 commit comments

Comments
 (0)