Skip to content

Commit 6c798e3

Browse files
committed
add getalluser api endpoint
1 parent 1d1473d commit 6c798e3

4 files changed

Lines changed: 55 additions & 5 deletions

File tree

‎config/database.php‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Database {
1111
private $DB_USER;
1212
private $DB_PASS;
1313
private $DB_NAME;
14-
public $conn = null;
14+
public $conn;
1515

1616
public function __construct() {
1717
$this->DB_HOST = $_ENV['DB_HOST'];
@@ -20,12 +20,17 @@ public function __construct() {
2020
$this->DB_NAME = $_ENV['DB_NAME'];
2121
}
2222

23-
public function connect() {
23+
public function connect(): ?PDO {
24+
$this->conn = null;
25+
2426
try {
25-
$dsn = "mysql:host" . $this->DB_HOST . ";db_name" . $this->DB_NAME . ";charset=utf8";
26-
$pdo = new PDO($dsn, $this->DB_USER, $this->DB_PASS);
27+
$dsn = "mysql:host=" . $this->DB_HOST . ";dbname=" . $this->DB_NAME . ";charset=utf8";
28+
$this->conn = new PDO($dsn, $this->DB_USER, $this->DB_PASS);
29+
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
2730
} catch (PDOException $e) {
2831
echo "Connection failed: " . $e->getMessage();
2932
}
33+
34+
return $this->conn;
3035
}
3136
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../models/User.php';
4+
5+
class UsersController {
6+
private $user;
7+
8+
public function __construct() {
9+
$this->user = new User();
10+
}
11+
12+
public function getAllUser() {
13+
$result = $this->user->getAllUser();
14+
15+
header('Content-Type: application/json');
16+
echo json_encode($result);
17+
}
18+
}

‎src/models/User.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../../config/database.php';
4+
5+
class User {
6+
private $conn;
7+
8+
public function __construct() {
9+
$this->conn = (new Database())->connect();
10+
}
11+
12+
public function getAllUser() : array {
13+
if ($this->conn) {
14+
$query = "SELECT * FROM user";
15+
$stmt = $this->conn->query($query);
16+
return $stmt->fetchAll(PDO::FETCH_ASSOC);
17+
} else {
18+
return [];
19+
}
20+
}
21+
}

‎src/routes/api.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
<?php
22

3-
$requestUri = $_SERVER['REQUEST_URI'];
3+
require_once __DIR__ . '/../controllers/UsersController.php';
4+
5+
$usersController = new UsersController();
6+
7+
$requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
48
$requestMethod = $_SERVER['REQUEST_METHOD'];
59

610
if ($requestUri == "/" && $requestMethod == "GET") {
711
echo "You are in the root page!";
12+
} elseif ($requestUri == "/users" && $requestMethod == "GET") {
13+
$usersController->getAllUser();
814
} else {
915
http_response_code(404);
1016
echo "Error 404! No route found!";

0 commit comments

Comments
 (0)