[go: nahoru, domu]

Skip to content

Commit

Permalink
feat(users): add update user
Browse files Browse the repository at this point in the history
  • Loading branch information
s3m3dov committed Apr 12, 2022
1 parent d0f8b52 commit d49982e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
62 changes: 47 additions & 15 deletions Controller/Api/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public function Action()
);
}
}

/**
* "/users/create" Endpoint - Create a user
*/
public function createAction()
{
$requestMethod = $_SERVER["REQUEST_METHOD"];
Expand Down Expand Up @@ -80,22 +82,52 @@ public function createAction()
}

/**
public function updateAction($id)
* "/users/update" Endpoint - Update a user
*/
public function updateAction()
{
$result = $this->personGateway->find($id);
if (! $result) {
return $this->notFoundResponse();
}
$input = (array) json_decode(file_get_contents('php://input'), TRUE);
if (! $this->validatePerson($input)) {
return $this->unprocessableEntityResponse();
}
$this->personGateway->update($id, $input);
$response['status_code_header'] = 'HTTP/1.1 200 OK';
$response['body'] = null;
return $response;
}

$requestMethod = $_SERVER["REQUEST_METHOD"];
$arrQueryStringParams = $this->getQueryStringParams();
$errorMessage = '';
$errorCode = '';

if ($requestMethod == 'PUT') {
try {
$input = file_get_contents('php://input');
$input = array_values(json_decode($input, true));
$userModel = new UserModel();
if (isset($arrQueryStringParams['id'])) {
$user = $userModel->getUser($arrQueryStringParams['id']);
if (empty($user)) {
$this->sendError('404 Not Found', 'No data found');
} else {
$userModel->updateUser($arrQueryStringParams['id'], $input);
}
} else {
$this->sendError('400 Bad Request', 'User ID is required');
}

} catch (Exception $e) {
$errorMessage = $e->getMessage().' Something went wrong! Please contact support.';
$errorCode = '500 Internal Server Error';
}
} else {
$errorMessage = 'Method not supported';
$errorCode = '422 Unprocessable Entity';
}

// send output
if ($errorMessage != '') {
$this->sendError($errorCode, $errorMessage);
} else {
$this->sendOutput(
json_encode(array('message' => 'User updated successfully')),
array('Content-Type: application/json', 'HTTP/1.1 200 OK')
);
}
}
/*
public function deleteAction($id)
{
$result = $this->personGateway->find($id);
Expand Down
2 changes: 1 addition & 1 deletion Model/UserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function insertUser(array $input): mysqli_stmt
public function updateUser($id, array $input)
{
$query = "UPDATE " . self::$users_table .
"SET " . self::$user_fullname . "=?, " . self::$user_email . "=?, " . self::$user_password .
" SET " . self::$user_fullname . "=?, " . self::$user_email . "=?, " . self::$user_password .
"=? WHERE " . self::$user_id . "=?";
$types = str_repeat("s", count($input)) . "i";
$params = array_merge($input, [$id]);
Expand Down

0 comments on commit d49982e

Please sign in to comment.