diff --git a/Controller/API/CustomerController.php b/Controller/API/CustomerController.php index f431159..7d62222 100644 --- a/Controller/API/CustomerController.php +++ b/Controller/API/CustomerController.php @@ -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; } } diff --git a/Model/CustomerModel.php b/Model/CustomerModel.php index 7ba0d8a..3b82732 100644 --- a/Model/CustomerModel.php +++ b/Model/CustomerModel.php @@ -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; } } diff --git a/Model/Database.php b/Model/Database.php index 237ba9a..37c5ab7 100644 --- a/Model/Database.php +++ b/Model/Database.php @@ -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) + { + + } }