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

tp3 update réparé

parent 44702c15
......@@ -20,8 +20,7 @@ class UserController extends Controller {
return $this->getAllUsers();
break;
case 'PUT':
if ($this->request->getUriParams())
return $this->updateUser($this->request->getUriParams()[0],$this->request->getData());
return $this->updateUser($this->request->getData());
break;
}
return Response::errorResponse("unsupported parameters or method in users");
......@@ -41,32 +40,9 @@ class UserController extends Controller {
$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");
protected function updateUser($array){
User::updateUser($array);
$response = Response::okResponse('User succesfully updated !');
return $response;
}
}
\ No newline at end of file
......@@ -17,14 +17,8 @@ class User extends Model {
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");
}
public static function updateUser($array) {
$stm = parent::exec('USER_UPDATE', $array);
}
......
......@@ -16,4 +16,4 @@ 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
'UPDATE USER SET USER_EMAIL = :email WHERE USER_ID = :id');
\ No newline at end of file
......@@ -70,7 +70,8 @@ class Request {
public function initData() {
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