Skip to content

Commit 436d2f6

Browse files
committed
add api endpoint for add new user
1 parent c109a6f commit 436d2f6

3 files changed

Lines changed: 72 additions & 3 deletions

File tree

‎src/controllers/UsersController.php‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function __construct() {
99
$this->user = new User();
1010
}
1111

12-
public function getAllUser() {
12+
public function getAllUser() : void {
1313
try {
1414
$result = $this->user->getAllUser();
1515

@@ -29,7 +29,7 @@ public function getAllUser() {
2929
}
3030
}
3131

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

@@ -50,4 +50,35 @@ public function getUserById($id) {
5050
]);
5151
}
5252
}
53+
54+
public function addNewUser() : void {
55+
try {
56+
$name = $_POST['name'];
57+
$age = $_POST['age'];
58+
$job = $_POST['job'];
59+
60+
$this->user->addNewUser($name, $age, $job);
61+
62+
$result = $this->user->getUserByName($name);
63+
if (empty($result)) {
64+
$message = "Adding new user data failed";
65+
} else {
66+
$message = "Successfully add new user data";
67+
}
68+
69+
header('Content-Type: application/json');
70+
http_response_code(empty($result) ? 404 : 200);
71+
echo json_encode([
72+
"status"=>"success",
73+
"message"=> $message,
74+
"data"=> $result
75+
]);
76+
} catch (Exception $e) {
77+
http_response_code(500);
78+
echo json_encode([
79+
"status"=> "error",
80+
"message"=> "server error occured"
81+
]);
82+
}
83+
}
5384
}

‎src/models/User.php‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,39 @@ public function getUserById($id) : ?array {
4545
error_log("Query failed: " . $e->getMessage(), 3, '../../logs/error.log');
4646
}
4747
}
48-
}
48+
49+
public function getUserByName($name) : ?array {
50+
try {
51+
$query = "SELECT * FROM user WHERE name=:name";
52+
53+
if ($this->conn === null) {
54+
throw new Exception("Database connection is null.");
55+
}
56+
57+
$stmt = $this->conn->prepare($query);
58+
$stmt->bindParam(":name", $name, PDO::PARAM_STR);
59+
$stmt->execute();
60+
return $stmt->fetchAll(PDO::FETCH_ASSOC);
61+
} catch (PDOException $e) {
62+
error_log("Query failed: " . $e->getMessage(), 3, '../../logs/error.log');
63+
}
64+
}
65+
66+
public function addNewUser($name, $age, $job) : void {
67+
try {
68+
$query = "INSERT INTO user (name, age, job) VALUES (?, ?, ?)";
69+
70+
if ($this->conn === null) {
71+
throw new Exception("Database connection is null.");
72+
}
73+
74+
$stmt = $this->conn->prepare($query);
75+
$stmt->bindParam(1, $name, PDO::PARAM_STR);
76+
$stmt->bindParam(2, $age, PDO::PARAM_STR);
77+
$stmt->bindParam(3, $job, PDO::PARAM_STR);
78+
$stmt->execute();
79+
} catch (PDOException $e) {
80+
error_log("Query failed: " . $e->getMessage(), 3, '../../logs/error.log');
81+
}
82+
}
83+
}

‎src/routes/api.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
case "GET":
2323
$usersController->getAllUser();
2424
break;
25+
case "POST":
26+
$usersController->addNewUser();
27+
break;
2528
default:
2629
sendResponse405();
2730
break;

0 commit comments

Comments
 (0)