Commit cff2d3ee authored by thibaut-felten's avatar thibaut-felten

API REST

parent 89ae27da
......@@ -4,14 +4,16 @@
class User{
private $id;
private $name;
private $email;
// public function __construct($props = array()) { $this−>props = $props; }
protected $props;
// public function __get($prop) { return $this−>props[$prop]; }
// public function __set($prop, $val) { $this−>props[$prop] = $val; }
public function __get($prop) {
return $this->props[$prop];
}
public function __set($prop, $val) {
$this->props[$prop] = $val;
}
private static function getAllUsers(){
......
......@@ -14,14 +14,14 @@ class DatabaseConnector {
protected static function createPDO() {
// $db = new PDO("sqlite::memory");
$connectionString = "mysql:host=". _MYSQL_HOST;
$connectionString = "mysql:host=". DB_HOST;
if(defined('_MYSQL_PORT'))
$connectionString .= ";port=". _MYSQL_PORT;
$connectionString .= ";port=". DB_PORT;
$connectionString .= ";dbname=" . _MYSQL_DATABASE;
$connectionString .= ";dbname=" . DB_DBNAME;
static::$pdo = new PDO($connectionString,_MYSQL_USERNAME,_MYSQL_PASSWORD);
static::$pdo = new PDO($connectionString,DB_USER,DB_PASSWORD);
static::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}
\ No newline at end of file
<?php
class UsersController {
private $requestMethod;
public function __construct($requestMethod)
{
$this->requestMethod = $requestMethod;
}
public function processRequest()
{
switch ($this->requestMethod) {
case 'GET':
$response = $this->getAllUsers();
break;
default:
$response = $this->notFoundResponse();
break;
}
header($response['status_code_header']);
if ($response['body']) {
echo $response['body'];
}
}
private function getAllUsers()
{
// TODO ...
}
private function notFoundResponse()
{
$response['status_code_header'] = 'HTTP/1.1 404 Not Found';
$response['body'] = null;
return $response;
}
}
......@@ -2,7 +2,37 @@
class UserModel
{
public static function getAllUsers() {
// TODO ... (cf. TP1)
public static function getAllUsers(){
$pdo = DatabaseConnector::current();
$request = $pdo->prepare("select * from users");
$request->execute();
$allUsers = $request->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class());
return $allUsers;
}
public static function getUserById($id){
$pdo = DatabaseConnector::current();
$request = $pdo->prepare("select * from users where id=".$id);
$request->execute();
$allUsers = $request->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class());
return $allUsers;
}
public static function createUser($name, $email){
$pdo = DatabaseConnector::current();
$request = $pdo->prepare("insert into users(id, name, email) values (NULL, '".$name."','".$email."')");
$request->execute();
}
public static function deleteUser($id){
$pdo = DatabaseConnector::current();
$request = $pdo->prepare("delete from users where id=".$id);
$request->execute();
}
public static function editUser($id, $name, $email){
$pdo = DatabaseConnector::current();
$request = $pdo->prepare("update users set name='".$name."', email='".$email."' where id=".$id);
$request->execute();
}
}
\ No newline at end of file
<?php
class UsersController {
private $requestMethod;
private $params;
public function __construct($requestMethod, $params=null)
{
$this->requestMethod = $requestMethod;
$this->params = $params;
}
public function processRequest()
{
switch ($this->requestMethod) {
case 'GET':
if (isset($this->params)) {
$response = $this->getUserById(($this->params)[0]);
}else{
$response = $this->getAllUsers();
}
break;
case 'POST':
$response = $this->createUser($_POST['name'], $_POST['email']);
break;
case 'DELETE':
$response = $this->deleteUser(($this->params)[0]);
break;
case 'PUT':
if (isset($this->params)) {
$response = $this->editUser(($this->params)[0], ($this->params)[1], ($this->params)[2]);
}
break;
default:
$response = $this->notFoundResponse();
break;
}
header($response['status_code_header']);
if ($response['body']) {
echo $response['body'];
}
}
private function createUser($name, $email)
{
$user = UserModel::createUser($name, $email);
$response =[];
$response['status_code_header']=http_response_code(200);
$response['body'] = "User created";
return $response;
}
private function editUser($id, $name, $email)
{
$user = UserModel::editUser($id, $name, $email);
$response =[];
$response['status_code_header']=http_response_code(200);
$response['body'] = "User updated";
return $response;
}
private function deleteUser($id)
{
$user = UserModel::deleteUser($id);
$response =[];
$response['status_code_header']=http_response_code(200);
$response['body'] = "User deleted";
return $response;
}
private function getUserById($id)
{
$user = UserModel::getUserById($id);
$response =[];
$response['status_code_header']=http_response_code(200);
$response['body'] = json_encode($user);
return $response;
}
private function getAllUsers()
{
$users = UserModel::getAllUsers();
$response =[];
$response['status_code_header']=http_response_code(200);
$response['body'] = json_encode($users);
return $response;
}
private function notFoundResponse()
{
$response['status_code_header'] = 'HTTP/1.1 404 Not Found';
$response['body'] = null;
return $response;
}
}
......@@ -42,12 +42,18 @@ $requestMethod = $_SERVER["REQUEST_METHOD"];
$controllerName = $route['controller'];
switch($controllerName) {
case 'users' :
// GET api.php?/users
// POST api.php?/users
$controller = new UsersController($requestMethod);
break;
case 'user':
$params = $route['params'];
$controller = new UsersController($requestMethod,$params);
break;
default :
header("HTTP/1.1 404 Not Found");
exit();
......
<?php
define('_MYSQL_HOST','localhost');
define('_MYSQL_PORT',3306);
define('_MYSQL_DBNAME','dbtest');
define('_MYSQL_USER','root');
define('_MYSQL_PASSWORD','root');
\ No newline at end of file
define('DB_HOST','localhost');
define('DB_PORT',3306);
define('DB_DBNAME','dbtest');
define('DB_USER','root');
define('DB_PASSWORD','root');
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment