Commit c02760a5 authored by Quentin Vrel's avatar Quentin Vrel

Tp3 : douleur sur la mise à jour du user

parent 81917c8e
......@@ -3,6 +3,7 @@ class Request {
protected $controllerName;
protected $uriParameters;
protected $data;
protected static $_instance;
public static function getCurrentRequest(){
......@@ -16,6 +17,7 @@ class Request {
public function __construct() {
$this->initBaseURI();
$this->initControllerAndParametersFromURI();
$this->initData();
}
// intialise baseURI
......@@ -62,9 +64,32 @@ class Request {
return $this->controllerName;
}
public function getUriParams() {
return $this->uriParameters;
}
public function initData() {
if ($this->getHttpMethod() === 'PUT'){
$this->data = "";
$stream = fopen("php://input", "r");
$next_byte=fread($stream, '1024');
$this->data .= $next_byte;
fclose($stream);
$this->data=json_decode($this->data, TRUE);
}
}
// retourne la méthode HTTP utilisée dans la requête courante
public function getHttpMethod() {
return $_SERVER["REQUEST_METHOD"];
}
public function getData() {
return $this->data;
}
}
\ No newline at end of file
......@@ -14,8 +14,15 @@ class UserController extends Controller {
{
switch ($this->request->getHttpMethod()) {
case 'GET':
if ($this->request->getUriParams())
return $this->getUser($this->request->getUriParams()[0]);
return $this->getAllUsers();
break;
case 'PUT':
if ($this->request->getUriParams())
return $this->updateUser($this->request->getUriParams()[0],$this->request->getData());
break;
}
return Response::errorResponse("unsupported parameters or method in users");
}
......@@ -28,4 +35,38 @@ class UserController extends Controller {
// TODO
return $response;
}
protected function getUser($id){
$user = User::getRow($id);
$response = Response::okResponse(json_encode($user));
return $response;
}
protected function updateUser($id, $data){
$sets=[];
if(isset($data['login'])){
$sets[] = ['USER_LOGIN',$data['login']];
}
if(isset($data['email'])){
$sets[] = ['USER_EMAIL',$data['email']];
}
if(isset($data['role'])){
$sets[] = ['USER_ROLE',$data['role']];
}
if(isset($data['pwd'])){
$sets[] = ['USER_PWD',$data['pwd']];
}
if(isset($data['name'])){
$sets[] = ['USER_NAME',$data['name']];
}
if(isset($data['surname'])){
$sets[] = ['USER_SURNAME',$data['surname']];
}
//$sets = implode(', ', $sets);
$success = true;
foreach ($sets as $set ) {
$success &= User::update($id, $set);
}
$response = $success?Response::okResponse("Updated"):Response::errorResponse("failed");
return $response;
}
}
\ No newline at end of file
......@@ -12,4 +12,20 @@ class User extends Model {
$stm = parent::exec('USER_LIST');
return $stm->fetchAll();
}
public static function getRow($id) {
$stm = parent::exec('USER_GET_WITH_ID', ['id' => $id]);
return $stm->fetchAll();
}
public static function update($id, $set) {
$stm = parent::exec('USER_UPDATE', ['id' => $id, 'set_field' => $set[0], 'set_value' => $set[1]]);
try {
return true;
} catch (\Throwable $th) {
die("dommage, fromage");
}
}
}
\ No newline at end of file
......@@ -4,10 +4,16 @@ User::addSqlQuery('USER_LIST',
'SELECT * FROM USER ORDER BY USER_LOGIN');
User::addSqlQuery('USER_GET_WITH_LOGIN',
'SELECT * FROM USER WHERE USER_LOGIN=:login');
'SELECT * FROM USER WHERE USER_LOGIN=:login');
User::addSqlQuery('USER_GET_WITH_ID',
'SELECT * FROM USER WHERE USER_ID=:id');
User::addSqlQuery('USER_CREATE',
'INSERT INTO USER (USER_ID, USER_LOGIN, USER_EMAIL, USER_ROLE, USER_PWD, USER_NAME, USER_SURNAME) VALUES (NULL, :login, :email, :role, :pwd, :name, :surname)');
User::addSqlQuery('USER_CONNECT',
'SELECT * FROM USER WHERE USER_LOGIN=:login and USER_PWD=:password');
User::addSqlQuery('USER_UPDATE',
'UPDATE `USER` SET :set_field = :set_value WHERE `USER_ID` = :id');
\ 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