mirror of
https://github.com/sctn4elk/CustomerRewardsRESTAPI.git
synced 2025-01-09 15:44:29 -06:00
Formatting code, work on update call
This commit is contained in:
parent
24da83e643
commit
564ed4f50c
|
@ -27,50 +27,51 @@ class CustomerController extends BaseController{
|
||||||
function __construct() {
|
function __construct() {
|
||||||
$this->customerModel = new CustomerModel();
|
$this->customerModel = new CustomerModel();
|
||||||
$this->requestMethod = $this->getServerRequestMethod();
|
$this->requestMethod = $this->getServerRequestMethod();
|
||||||
$this->arrQueryStringParams = $this->getQueryStringParams();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function processAction() {
|
public function processAction() {
|
||||||
$this->strErrorDesc = '';
|
$this->strErrorDesc = '';
|
||||||
|
$this->strErrorHeader = '';
|
||||||
try {
|
try {
|
||||||
switch($this->action) {
|
switch($this->action) {
|
||||||
case "select":
|
case "select":
|
||||||
$response = $this->selectAction();
|
$response = $this->selectAction();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "insert":
|
case "insert":
|
||||||
$response = $this->insertCustomer();
|
$response = $this->insertCustomer();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "update":
|
case "update":
|
||||||
$response = $this->updateCustomer();
|
$response = $this->updateCustomer();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "delete":
|
case "delete":
|
||||||
$response = $this->deleteCustomer();
|
$response = $this->deleteCustomer();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
$strErrorDesc = 'Controller Method not supported for processAction: ' . $this->action;
|
$response = (object) ['Result' => 'Default'];
|
||||||
$strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
|
$this->strErrorDesc = 'Controller Method not supported for processAction: ' . $this->action;
|
||||||
break;
|
$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';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$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
|
// send output
|
||||||
if (!$strErrorDesc) {
|
if (!$this->strErrorDesc) {
|
||||||
$this->sendOutput(
|
$this->sendOutput(
|
||||||
$responseData,
|
$responseData,
|
||||||
array('Content-Type: application/json', 'HTTP/1.1 200 OK')
|
array('Content-Type: application/json', 'HTTP/1.1 200 OK')
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
$this->sendOutput(json_encode(array('error' => $strErrorDesc)),
|
$this->sendOutput(json_encode(array('error' => $this->strErrorDesc)),
|
||||||
array('Content-Type: application/json', $strErrorHeader)
|
array('Content-Type: application/json', $this->strErrorHeader)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,6 +82,7 @@ class CustomerController extends BaseController{
|
||||||
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
|
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
$this->arrQueryStringParams = $this->getQueryStringParams();
|
||||||
if (isset($this->arrQueryStringParams['customer_id'])) {
|
if (isset($this->arrQueryStringParams['customer_id'])) {
|
||||||
$response = $this->selectByIdAction();
|
$response = $this->selectByIdAction();
|
||||||
} else {
|
} else {
|
||||||
|
@ -131,14 +133,14 @@ class CustomerController extends BaseController{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$jsonPayload = file_get_contents('php://input');
|
$jsonPayload = file_get_contents('php://input');
|
||||||
$input = json_decode($jsonPayload);
|
$input = json_decode($jsonPayload, TRUE);
|
||||||
|
|
||||||
if (! $this->validatePerson($input)) {
|
if (! $this->validatePerson($input)) {
|
||||||
return $this->unprocessableEntityResponse();
|
return $this->unprocessableEntityResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($input->customer_id != null) {
|
if ($input['customer_id'] != null) {
|
||||||
$this->customerModel->customerId = $input->customer_id;
|
$this->customerModel->customerId = $input['customer_id'];
|
||||||
$result = $this->customerModel->findByCustomerId();
|
$result = $this->customerModel->findByCustomerId();
|
||||||
if (! $result) {
|
if (! $result) {
|
||||||
return $this->notFoundResponse();
|
return $this->notFoundResponse();
|
||||||
|
@ -185,9 +187,9 @@ class CustomerController extends BaseController{
|
||||||
private function validatePerson($input)
|
private function validatePerson($input)
|
||||||
{
|
{
|
||||||
$validtion = false;
|
$validtion = false;
|
||||||
if($input->customer_name_first != null){
|
if($input['customer_name_first'] != null){
|
||||||
$validtion = true;
|
$validtion = true;
|
||||||
if($input->customer_name_last == null) {
|
if($input['customer_name_last'] == null) {
|
||||||
$validtion = false;
|
$validtion = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -89,17 +89,21 @@ class CustomerModel extends Database {
|
||||||
public function insertCustomer($jsonPayLoad)
|
public function insertCustomer($jsonPayLoad)
|
||||||
{
|
{
|
||||||
$rowCount = $this->processStatement("CALL insert_new_customer_proc", $jsonPayLoad);
|
$rowCount = $this->processStatement("CALL insert_new_customer_proc", $jsonPayLoad);
|
||||||
return $rowCount;
|
$result = (object) ['rowCount'=>$rowCount];
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateCustomer($jsonPayLoad)
|
public function updateCustomer($jsonPayLoad)
|
||||||
{
|
{
|
||||||
$rowCount = $this->processStatement("CALL update_existing_customer_proc", $jsonPayLoad);
|
$rowCount = $this->processStatement("CALL update_existing_customer_proc", $jsonPayLoad);
|
||||||
return $rowCount;
|
$result = (object) ['rowCount'=>$rowCount];
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function deleteCustomer()
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,35 +59,65 @@ class Database {
|
||||||
public function processStatement($query = "", $params = [])
|
public function processStatement($query = "", $params = [])
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$parameters = str_repeat('?,', count($params) - 1) . '?';
|
$keys = array_keys($params);
|
||||||
$query += "(".$parameters.")";
|
$n = count($params);
|
||||||
$stmt = $this->executeStatement( $query, $params );
|
|
||||||
$result = $this->connection->affected_rows;
|
$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();
|
$stmt->close();
|
||||||
return $result;
|
return $rowCount;
|
||||||
} catch(Exception $e) {
|
} catch(Exception $e) {
|
||||||
throw New Exception( $e->getMessage() );
|
throw New Exception( $e->getMessage() );
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function executeStatement($query = "", $params = [])
|
private function executeStatement(&$stmt)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$stmt = $this->connection->prepare($query);
|
return var_dump($stmt);
|
||||||
if($stmt === false) {
|
$result = $stmt->execute();
|
||||||
throw New Exception("Unable to do prepared statement: " . $query);
|
|
||||||
|
if($result === false) {
|
||||||
|
throw New Exception("Unable to execute the statement.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*if( $params ) {
|
$rowCount = $this->connection->affected_rows;
|
||||||
$stmt->bind_param(str_repeat("s", count($params)), ...$params);
|
return $rowCount;
|
||||||
}*/
|
|
||||||
|
|
||||||
$stmt->execute($params);
|
|
||||||
//$stmt->execute();
|
|
||||||
return $stmt;
|
|
||||||
} catch(Exception $e) {
|
} catch(Exception $e) {
|
||||||
throw New Exception( $e->getMessage() );
|
throw New Exception( $e->getMessage() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function initStatement()
|
||||||
|
{
|
||||||
|
$statement = mysqli_stmt_init($this->connection);
|
||||||
|
return $statement;
|
||||||
|
}
|
||||||
|
private function prepareStatement($statement, $querystring)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user