Commit ba9156dc authored by quentin.vrel's avatar quentin.vrel

tp3 update réparé

parent 44702c15
...@@ -20,8 +20,7 @@ class UserController extends Controller { ...@@ -20,8 +20,7 @@ class UserController extends Controller {
return $this->getAllUsers(); return $this->getAllUsers();
break; break;
case 'PUT': case 'PUT':
if ($this->request->getUriParams()) return $this->updateUser($this->request->getData());
return $this->updateUser($this->request->getUriParams()[0],$this->request->getData());
break; break;
} }
return Response::errorResponse("unsupported parameters or method in users"); return Response::errorResponse("unsupported parameters or method in users");
...@@ -41,32 +40,9 @@ class UserController extends Controller { ...@@ -41,32 +40,9 @@ class UserController extends Controller {
$response = Response::okResponse(json_encode($user)); $response = Response::okResponse(json_encode($user));
return $response; return $response;
} }
protected function updateUser($id, $data){ protected function updateUser($array){
$sets=[]; User::updateUser($array);
if(isset($data['login'])){ $response = Response::okResponse('User succesfully updated !');
$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; return $response;
} }
} }
\ No newline at end of file
...@@ -17,14 +17,8 @@ class User extends Model { ...@@ -17,14 +17,8 @@ class User extends Model {
return $stm->fetchAll(); return $stm->fetchAll();
} }
public static function update($id, $set) { public static function updateUser($array) {
$stm = parent::exec('USER_UPDATE', ['id' => $id, 'set_field' => $set[0], 'set_value' => $set[1]]); $stm = parent::exec('USER_UPDATE', $array);
try {
return true;
} catch (\Throwable $th) {
die("dommage, fromage");
}
} }
......
...@@ -16,4 +16,4 @@ User::addSqlQuery('USER_CONNECT', ...@@ -16,4 +16,4 @@ User::addSqlQuery('USER_CONNECT',
'SELECT * FROM USER WHERE USER_LOGIN=:login and USER_PWD=:password'); 'SELECT * FROM USER WHERE USER_LOGIN=:login and USER_PWD=:password');
User::addSqlQuery('USER_UPDATE', User::addSqlQuery('USER_UPDATE',
'UPDATE `USER` SET :set_field = :set_value WHERE `USER_ID` = :id'); 'UPDATE USER SET USER_EMAIL = :email WHERE USER_ID = :id');
\ No newline at end of file \ No newline at end of file
...@@ -70,7 +70,8 @@ class Request { ...@@ -70,7 +70,8 @@ class Request {
public function initData() { public function initData() {
if ($this->getHttpMethod() === 'PUT' || $this->getHttpMethod() === 'POST'){ if ($this->getHttpMethod() === 'PUT' || $this->getHttpMethod() === 'POST'){
$this->data = json_decode(file_get_contents("php://input"), true); $jsondata=file_get_contents("php://input");
$this->data = json_decode($jsondata, true);
} }
} }
......
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