Formatting code, work on update call

This commit is contained in:
sctn4elk 2024-05-02 16:00:15 -05:00
parent 24da83e643
commit 564ed4f50c
3 changed files with 81 additions and 45 deletions

View File

@ -27,50 +27,51 @@ class CustomerController extends BaseController{
function __construct() {
$this->customerModel = new CustomerModel();
$this->requestMethod = $this->getServerRequestMethod();
$this->arrQueryStringParams = $this->getQueryStringParams();
}
public function processAction() {
$this->strErrorDesc = '';
$this->strErrorHeader = '';
try {
switch($this->action) {
case "select":
$response = $this->selectAction();
break;
case "insert":
$response = $this->insertCustomer();
break;
case "insert":
$response = $this->insertCustomer();
break;
case "update":
case "update":
$response = $this->updateCustomer();
break;
break;
case "delete":
case "delete":
$response = $this->deleteCustomer();
break;
break;
default:
$strErrorDesc = 'Controller Method not supported for processAction: ' . $this->action;
$strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
break;
}
$responseData = json_encode($response);
} catch (Error $e) {
$this->strErrorDesc = $e->getMessage().' Something went wrong in processAction! Please contact support.';
$this->strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
default:
$response = (object) ['Result' => 'Default'];
$this->strErrorDesc = 'Controller Method not supported for processAction: ' . $this->action;
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
break;
}
$responseData = json_encode($response);
} catch (Error $e) {
$this->strErrorDesc = $e->getMessage().' Something went wrong in processAction! Please contact support.';
$this->strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
}
// send output
if (!$strErrorDesc) {
if (!$this->strErrorDesc) {
$this->sendOutput(
$responseData,
array('Content-Type: application/json', 'HTTP/1.1 200 OK')
);
} else {
$this->sendOutput(json_encode(array('error' => $strErrorDesc)),
array('Content-Type: application/json', $strErrorHeader)
$this->sendOutput(json_encode(array('error' => $this->strErrorDesc)),
array('Content-Type: application/json', $this->strErrorHeader)
);
}
}
@ -81,6 +82,7 @@ class CustomerController extends BaseController{
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
return;
}
$this->arrQueryStringParams = $this->getQueryStringParams();
if (isset($this->arrQueryStringParams['customer_id'])) {
$response = $this->selectByIdAction();
} else {
@ -131,14 +133,14 @@ class CustomerController extends BaseController{
return;
}
$jsonPayload = file_get_contents('php://input');
$input = json_decode($jsonPayload);
$input = json_decode($jsonPayload, TRUE);
if (! $this->validatePerson($input)) {
return $this->unprocessableEntityResponse();
}
if ($input->customer_id != null) {
$this->customerModel->customerId = $input->customer_id;
if ($input['customer_id'] != null) {
$this->customerModel->customerId = $input['customer_id'];
$result = $this->customerModel->findByCustomerId();
if (! $result) {
return $this->notFoundResponse();
@ -185,9 +187,9 @@ class CustomerController extends BaseController{
private function validatePerson($input)
{
$validtion = false;
if($input->customer_name_first != null){
if($input['customer_name_first'] != null){
$validtion = true;
if($input->customer_name_last == null) {
if($input['customer_name_last'] == null) {
$validtion = false;
}
}

View File

@ -89,17 +89,21 @@ class CustomerModel extends Database {
public function insertCustomer($jsonPayLoad)
{
$rowCount = $this->processStatement("CALL insert_new_customer_proc", $jsonPayLoad);
return $rowCount;
$result = (object) ['rowCount'=>$rowCount];
return $result;
}
public function updateCustomer($jsonPayLoad)
{
$rowCount = $this->processStatement("CALL update_existing_customer_proc", $jsonPayLoad);
return $rowCount;
$result = (object) ['rowCount'=>$rowCount];
return $result;
}
public function deleteCustomer()
{
return $this->processStatement("DELETE FROM customer WHERE customer_id = ?", [$this->customerId]);
$rowCount = $this->processStatement("DELETE FROM customer WHERE customer_id = ?", [$this->customerId]);
$result = (object) ['rowCount'=>$rowCount];
return $result;
}
}

View File

@ -59,35 +59,65 @@ class Database {
public function processStatement($query = "", $params = [])
{
try {
$parameters = str_repeat('?,', count($params) - 1) . '?';
$query += "(".$parameters.")";
$stmt = $this->executeStatement( $query, $params );
$result = $this->connection->affected_rows;
$keys = array_keys($params);
$n = count($params);
$query .= " (" . $params[$keys[0]] . ", ";
for($i = 1; $i < $n-1; $i++) {
$query .= "'" . $params[$keys[$i]] . "', ";
}
$query .= $params[$keys[$i]] . ")";
//Prepare the statement
$stmt = $this->connection->stmt_init();
$stmt = $this->connection->prepare($query);
if($stmt === false) {
throw New Exception("Unable to prepare the statement: " . $query);
}
$result = $stmt->execute();
if($result === false) {
throw New Exception("Unable to execute the statement.");
}
$rowCount = $this->connection->affected_rows;
if($rowCount < 1)
{
throw New Exception("Statement did not return any rows: " . $query);
}
$stmt->close();
return $result;
return $rowCount;
} catch(Exception $e) {
throw New Exception( $e->getMessage() );
}
return false;
}
private function executeStatement($query = "", $params = [])
private function executeStatement(&$stmt)
{
try {
$stmt = $this->connection->prepare($query);
if($stmt === false) {
throw New Exception("Unable to do prepared statement: " . $query);
return var_dump($stmt);
$result = $stmt->execute();
if($result === false) {
throw New Exception("Unable to execute the statement.");
}
/*if( $params ) {
$stmt->bind_param(str_repeat("s", count($params)), ...$params);
}*/
$stmt->execute($params);
//$stmt->execute();
return $stmt;
$rowCount = $this->connection->affected_rows;
return $rowCount;
} catch(Exception $e) {
throw New Exception( $e->getMessage() );
}
}
private function initStatement()
{
$statement = mysqli_stmt_init($this->connection);
return $statement;
}
private function prepareStatement($statement, $querystring)
{
}
}