Clean up, fix customerId issue, finish insert

This commit is contained in:
sctn4elk 2024-05-04 00:22:26 -05:00
parent e8698e360f
commit c05bf0559a
3 changed files with 23 additions and 30 deletions

View File

@ -122,12 +122,10 @@ class CustomerController extends BaseController{
if (! $this->validatePerson($input)) { if (! $this->validatePerson($input)) {
return $this->unprocessableEntityResponse(); return $this->unprocessableEntityResponse();
} }
//return var_dump($input);
//remove customer_id field so it doesn't break //remove customer_id field so it doesn't break
unset($input['customer_id']); unset($input['customer_id']);
//return var_dump($input);
//$modinput = json_encode($input);
//$newinput = json_decode($modinput, TRUE);
$response = $this->customerModel->insertCustomer($input); $response = $this->customerModel->insertCustomer($input);
return $response; return $response;
} }
@ -168,10 +166,13 @@ class CustomerController extends BaseController{
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request'; $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
return; return;
} }
if (isset($this->arrQueryStringParams['customer_id'])) {
$this->customerModel->customerId = $this->arrQueryStringParams['customer_id']; $this->arrQueryStringParams = $this->getQueryStringParams();
if (isset($this->arrQueryStringParams['customerId'])) {
$this->customerModel->customerId = $this->arrQueryStringParams['customerId'];
$result = $this->customerModel->findByCustomerId(); $result = $this->customerModel->findByCustomerId();
if (! $result) { if (!$result) {
return $this->notFoundResponse(); return $this->notFoundResponse();
} }
$response = $this->customerModel->deleteCustomer(); $response = $this->customerModel->deleteCustomer();

View File

@ -115,14 +115,13 @@ class CustomerModel extends Database {
$query .= $jsonPayLoad[$keys[$i]] . ")"; $query .= $jsonPayLoad[$keys[$i]] . ")";
$rowCount = $this->processStatement($query); $rowCount = $this->processStatement($query);
//$result = (object) ['rowCount'=>$rowCount];
return $rowCount; return $rowCount;
} }
public function deleteCustomer() public function deleteCustomer()
{ {
$rowCount = $this->processStatement("DELETE FROM customer WHERE customer_id = ?", [$this->customerId]); $query = "DELETE FROM customer WHERE customer_id = " . $this->customerId;
$result = (object) ['rowCount'=>$rowCount]; $rowCount = $this->processStatement($query);
return $result; return $rowCount;
} }
} }

View File

@ -29,7 +29,15 @@ class Database {
public function processQuery($query = "", $params = []) public function processQuery($query = "", $params = [])
{ {
try { try {
$stmt = $this->executeQuery( $query, $params ); $stmt = $this->connection->prepare( $query );
if($stmt === false) {
throw New Exception("Unable to do prepared statement: " . $query);
}
if( $params ) {
$stmt->bind_param($params[0], $params[1]);
}
$stmt->execute();
$result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC); $result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close(); $stmt->close();
return $result; return $result;
@ -58,20 +66,20 @@ class Database {
public function processStatement($query = "") public function processStatement($query = "")
{ {
try { try {
//return var_dump($query);
//Prepare the statement //Prepare the statement
$stmt = $this->connection->stmt_init(); $stmt = $this->connection->stmt_init();
$stmt = $this->connection->prepare($query); $stmt = $this->connection->prepare($query);
if($stmt === false) { if($stmt === false) {
throw New Exception("Unable to prepare the statement: " . $query); throw New Exception("Unable to prepare the statement: " . $query);
} }
//return var_dump($query);
$result = $stmt->execute(); $result = $stmt->execute();
if($result === false) { if($result === false) {
throw New Exception("Unable to execute the statement: " . $query); throw New Exception("Unable to execute the statement: " . $query);
} }
//$rowCount = $this->executeStatement($stmt);
$rowCount = $this->connection->affected_rows; $rowCount = $this->connection->affected_rows;
if($rowCount < 1) if($rowCount < 1)
{ {
@ -85,19 +93,4 @@ class Database {
} }
return false; return false;
} }
private function executeStatement(&$stmt)
{
try {
$result = $stmt->execute();
if($result === false) {
throw New Exception("Unable to execute the statement.");
}
$rowCount = $this->connection->affected_rows;
return $rowCount;
} catch(Exception $e) {
throw New Exception( $e->getMessage() );
}
}
} }