From 326b1521ba454d728f3e90d50c874d78c1697e41 Mon Sep 17 00:00:00 2001 From: sctn4elk Date: Mon, 15 Apr 2024 11:38:59 -0500 Subject: [PATCH] Initial upload --- .gitignore | 9 +++ Controller/API/BaseController.php | 60 +++++++++++++++++ Controller/API/CustomerController.php | 96 ++++++++++++++++++++++++++ Controller/API/PurchaseController.php | 53 +++++++++++++++ Include/bootstrap.php | 14 ++++ Include/sampleConfig.php | 11 +++ Model/CustomerModel.php | 97 +++++++++++++++++++++++++++ Model/Database.php | 87 ++++++++++++++++++++++++ Model/PurchaseModel.php | 20 ++++++ index.php | 41 +++++++++++ nbproject/project.properties | 8 +++ nbproject/project.xml | 9 +++ 12 files changed, 505 insertions(+) create mode 100644 .gitignore create mode 100644 Controller/API/BaseController.php create mode 100644 Controller/API/CustomerController.php create mode 100644 Controller/API/PurchaseController.php create mode 100644 Include/bootstrap.php create mode 100644 Include/sampleConfig.php create mode 100644 Model/CustomerModel.php create mode 100644 Model/Database.php create mode 100644 Model/PurchaseModel.php create mode 100644 index.php create mode 100644 nbproject/project.properties create mode 100644 nbproject/project.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e8e1aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +**/nbproject/private/ +**/nbproject/Makefile-*.mk +**/nbproject/Package-*.bash +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ +/Include/config.php diff --git a/Controller/API/BaseController.php b/Controller/API/BaseController.php new file mode 100644 index 0000000..4805fb7 --- /dev/null +++ b/Controller/API/BaseController.php @@ -0,0 +1,60 @@ +sendOutput('', array('HTTP/1.1 404 Not Found')); + } + /** +* Get URI elements. +* +* @return array +*/ + protected function getUriSegments() + { + $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); + $uri = explode( '/', $uri ); + return $uri; + } + /** +* Get querystring params. +* +* @return array +*/ + protected function getQueryStringParams() + { + $query = array(); + $parameters = parse_str($_SERVER['QUERY_STRING'], $query); + return $query; + } + /** +* Send API output. +* +* @param mixed $data +* @param string $httpHeader +*/ + protected function sendOutput($data, $httpHeaders=array()) + { + header_remove('Set-Cookie'); + if (is_array($httpHeaders) && count($httpHeaders)) { + foreach ($httpHeaders as $httpHeader) { + header($httpHeader); + } + } + echo $data; + exit; + } +} diff --git a/Controller/API/CustomerController.php b/Controller/API/CustomerController.php new file mode 100644 index 0000000..8778600 --- /dev/null +++ b/Controller/API/CustomerController.php @@ -0,0 +1,96 @@ +getQueryStringParams(); + if (strtoupper($requestMethod) == 'GET') { + try { + $customerModel = new CustomerModel(); + + $uri = $this->getUriSegments(); + switch($uri[4]) { + case "select": + $customerModel->limit = 10; + if (isset($arrQueryStringParams['limit']) && $arrQueryStringParams['limit']) { + $customerModel->limit = $arrQueryStringParams['limit']; + } + $arrCustomer = $customerModel->getCustomers(); + unset($customerModel->limit); + break; + case "insert": + $customerModel->first = $arrQueryStringParams['first']; + $customerModel->last = $arrQueryStringParams['last']; + $customerModel->email = $arrQueryStringParams['email']; + $customerModel->phone = $arrQueryStringParams['phone']; + $customerModel->birthday = $arrQueryStringParams['birthday']; + $customerModel->loyalty = $arrQueryStringParams['loyalty']; + $customerModel->city = $arrQueryStringParams['city']; + $customerModel->state = $arrQueryStringParams['state']; + $customerModel->zip = $arrQueryStringParams['zip']; + + $arrCustomer = $customerModel->insertCustomer(); + + unset($customerModel->first); + unset($customerModel->last); + unset($customerModel->email); + unset($customerModel->phone); + unset($customerModel->birthday); + unset($customerModel->loyalty); + unset($customerModel->city); + unset($customerModel->state); + unset($customerModel->zip); + break; + + case "update": + $arrCustomer = $customerModel->updateCustomer($arrQueryStringParams); + break; + + case "delete": + $arrCustomer = $customerModel->deleteCustomer($arrQueryStringParams); + break; + + default: + $strErrorDesc = 'Controller Method not supported for processAction: ' . $uri[4]; + $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; + break; + } + + $responseData = json_encode($arrCustomer); + } catch (Error $e) { + $strErrorDesc = $e->getMessage().' Something went wrong in processAction! Please contact support.'; + $strErrorHeader = 'HTTP/1.1 500 Internal Server Error'; + } + } else { + $strErrorDesc = 'Request Method not supported for processAction'; + $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; + } + // send output + if (!$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) + ); + } + } +} diff --git a/Controller/API/PurchaseController.php b/Controller/API/PurchaseController.php new file mode 100644 index 0000000..7124f6c --- /dev/null +++ b/Controller/API/PurchaseController.php @@ -0,0 +1,53 @@ +getQueryStringParams(); + if (strtoupper($requestMethod) == 'GET') { + try { + $purchaseModel = new PurchaseModel(); + $intLimit = 10; + $dtDate = date("M/d/yy"); + if (isset($arrQueryStringParams['limit']) && $arrQueryStringParams['limit']) { + $intLimit = $arrQueryStringParams['limit']; + $dtDate = $arrQueryStringParams['date']; + } + $arrPurchases = $purchaseModel->getPurchases($dtDate, $intLimit); + $responseData = json_encode($arrPurchases); + } catch (Error $e) { + $strErrorDesc = $e->getMessage().'Something went wrong! Please contact support.'; + $strErrorHeader = 'HTTP/1.1 500 Internal Server Error'; + } + } else { + $strErrorDesc = 'Method not supported'; + $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; + } + // send output + if (!$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) + ); + } + } +} diff --git a/Include/bootstrap.php b/Include/bootstrap.php new file mode 100644 index 0000000..a990ff7 --- /dev/null +++ b/Include/bootstrap.php @@ -0,0 +1,14 @@ +"); +define("DB_USERNAME", ""); +define("DB_PASSWORD", ''); +define("DB_DATABASE_NAME", ""); diff --git a/Model/CustomerModel.php b/Model/CustomerModel.php new file mode 100644 index 0000000..7f30b9f --- /dev/null +++ b/Model/CustomerModel.php @@ -0,0 +1,97 @@ +params[$name] = $value; + } + + public function __get($name) + { + //echo "Getting '$name'\n"; + if (array_key_exists($name, $this->params)) { + return $this->params[$name]; + } + + $trace = debug_backtrace(); + trigger_error( + 'Undefined property via __get(): ' . $name . + ' in ' . $trace[0]['file'] . + ' on line ' . $trace[0]['line'], + E_USER_NOTICE); + return null; + } + + public function __isset($name) + { + //echo "Is '$name' set?\n"; + return isset($this->params[$name]); + } + + public function __unset($name) + { + //echo "Unsetting '$name'\n"; + unset($this->params[$name]); + } + + public function getCustomers() + { + return $this->processQuery("SELECT * FROM customer_view ORDER BY customer_id ASC LIMIT ?", ["i", $this->limit]); + } + + public function insertCustomer() + { + $rowCount = $this->processStatement("CALL insert_new_customer_proc(?,?,?,?,?,?,?,?,?)", + [$this->first, + $this->last, + $this->email, + $this->phone, + $this->birthday, + $this->loyalty, + $this->city, + $this->state, + $this->zip]); + return $rowCount; + } + + public function updateCustomer($id, $param_name, $param_value) + { + return $this->processStatement("UPDATE customer SET ? = ? WHERE customer_id = ?", [$param_name, $param_value, $id]); + } + + public function deleteCustomer($id) + { + return $this->processStatement("DELETE FROM customer WHERE customer_id = ?", [$id]); + } +} diff --git a/Model/Database.php b/Model/Database.php new file mode 100644 index 0000000..b4ad654 --- /dev/null +++ b/Model/Database.php @@ -0,0 +1,87 @@ +connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE_NAME); + + if ( mysqli_connect_errno()) { + throw new Exception("Could not connect to database."); + } + } catch (Exception $e) { + throw new Exception($e->getMessage()); + } + } + + public function processQuery($query = "", $params = []) + { + try { + $stmt = $this->executeQuery( $query, $params ); + $result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC); + $stmt->close(); + return $result; + } catch(Exception $e) { + throw New Exception( $e->getMessage() ); + } + return false; + } + + private function executeQuery($query = "" , $params = []) + { + try { + $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(); + return $stmt; + } catch(Exception $e) { + throw New Exception( $e->getMessage()); + } + } + + public function processStatement($query = "", $params = []) + { + try { + $stmt = $this->executeStatement( $query, $params ); + $result = $this->connection->affected_rows; + $stmt->close(); + return $result; + } catch(Exception $e) { + throw New Exception( $e->getMessage() ); + } + return false; + } + + private function executeStatement($query = "", $params = []) + { + try { + $stmt = $this->connection->prepare( $query ); + if($stmt === false) { + throw New Exception("Unable to do prepared statement: " . $query); + } + if( $params ) { + $stmt->bind_param(str_repeat('s', count($params)), ...$params); + } + $stmt->execute(); + return $stmt; + } catch(Exception $e) { + throw New Exception( $e->getMessage() ); + } + } +} diff --git a/Model/PurchaseModel.php b/Model/PurchaseModel.php new file mode 100644 index 0000000..1ce2ad7 --- /dev/null +++ b/Model/PurchaseModel.php @@ -0,0 +1,20 @@ +select("SELECT * FROM purchases WHERE purchase_date = '{$date}' ORDER BY purchase_id ASC LIMIT {$limit}"); + } +} diff --git a/index.php b/index.php new file mode 100644 index 0000000..b8c82f8 --- /dev/null +++ b/index.php @@ -0,0 +1,41 @@ +{$strMethodName}(); + diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..50a6cf9 --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,8 @@ +browser.reload.on.save=true +include.path=${php.global.include.path} +php.version=PHP_81 +source.encoding=UTF-8 +src.dir=. +tags.asp=false +tags.short=false +web.root=. diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..a928ac6 --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,9 @@ + + + org.netbeans.modules.php.project + + + CustomerRewardsRESTAPI + + +