From c05bf0559a8cb5d88ba05c153fe249d79d3cf98d Mon Sep 17 00:00:00 2001 From: sctn4elk Date: Sat, 4 May 2024 00:22:26 -0500 Subject: [PATCH] Clean up, fix customerId issue, finish insert --- Controller/API/CustomerController.php | 15 +++++++------ Model/CustomerModel.php | 7 +++--- Model/Database.php | 31 +++++++++++---------------- 3 files changed, 23 insertions(+), 30 deletions(-) diff --git a/Controller/API/CustomerController.php b/Controller/API/CustomerController.php index 923d217..1310ebf 100644 --- a/Controller/API/CustomerController.php +++ b/Controller/API/CustomerController.php @@ -122,12 +122,10 @@ class CustomerController extends BaseController{ if (! $this->validatePerson($input)) { return $this->unprocessableEntityResponse(); } - //return var_dump($input); + //remove customer_id field so it doesn't break unset($input['customer_id']); - //return var_dump($input); - //$modinput = json_encode($input); - //$newinput = json_decode($modinput, TRUE); + $response = $this->customerModel->insertCustomer($input); return $response; } @@ -168,10 +166,13 @@ class CustomerController extends BaseController{ $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request'; 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(); - if (! $result) { + if (!$result) { return $this->notFoundResponse(); } $response = $this->customerModel->deleteCustomer(); diff --git a/Model/CustomerModel.php b/Model/CustomerModel.php index 0a5aa3a..275a170 100644 --- a/Model/CustomerModel.php +++ b/Model/CustomerModel.php @@ -115,14 +115,13 @@ class CustomerModel extends Database { $query .= $jsonPayLoad[$keys[$i]] . ")"; $rowCount = $this->processStatement($query); - //$result = (object) ['rowCount'=>$rowCount]; return $rowCount; } public function deleteCustomer() { - $rowCount = $this->processStatement("DELETE FROM customer WHERE customer_id = ?", [$this->customerId]); - $result = (object) ['rowCount'=>$rowCount]; - return $result; + $query = "DELETE FROM customer WHERE customer_id = " . $this->customerId; + $rowCount = $this->processStatement($query); + return $rowCount; } } diff --git a/Model/Database.php b/Model/Database.php index ca6be17..8a3d329 100644 --- a/Model/Database.php +++ b/Model/Database.php @@ -29,7 +29,15 @@ class Database { public function processQuery($query = "", $params = []) { 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); $stmt->close(); return $result; @@ -58,20 +66,20 @@ class Database { public function processStatement($query = "") { - try { + try { + //return var_dump($query); //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); } - //return var_dump($query); + $result = $stmt->execute(); if($result === false) { throw New Exception("Unable to execute the statement: " . $query); } - //$rowCount = $this->executeStatement($stmt); $rowCount = $this->connection->affected_rows; if($rowCount < 1) { @@ -85,19 +93,4 @@ class Database { } 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() ); - } - } }