mirror of
https://github.com/sctn4elk/CustomerRewardsRESTAPI.git
synced 2025-01-09 15:34:30 -06:00
Initial upload
This commit is contained in:
commit
326b1521ba
9
.gitignore
vendored
Normal file
9
.gitignore
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
**/nbproject/private/
|
||||
**/nbproject/Makefile-*.mk
|
||||
**/nbproject/Package-*.bash
|
||||
build/
|
||||
nbbuild/
|
||||
dist/
|
||||
nbdist/
|
||||
.nb-gradle/
|
||||
/Include/config.php
|
60
Controller/API/BaseController.php
Normal file
60
Controller/API/BaseController.php
Normal file
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
|
||||
*/
|
||||
|
||||
/**
|
||||
* Description of BaseController
|
||||
*
|
||||
* @author SCTN4
|
||||
*/
|
||||
class BaseController {
|
||||
/**
|
||||
* __call magic method.
|
||||
*/
|
||||
public function __call($name, $arguments)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
96
Controller/API/CustomerController.php
Normal file
96
Controller/API/CustomerController.php
Normal file
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
header("Content-Type: application/json");
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
|
||||
*/
|
||||
|
||||
/**
|
||||
* Description of CustomerController
|
||||
*
|
||||
* @author SCTN4
|
||||
* http://localhost:8000/index.php/customer/process/insert?name=Mike%20Howard&email=sctn4elk@msn.com&phone=208-841-4159&birthday=05/07/1965&loyalty=1&city=Winnsboro&state=TX&zip=75494
|
||||
*/
|
||||
class CustomerController extends BaseController{
|
||||
/**
|
||||
* "/customer/list" Endpoint - Get list of users
|
||||
*/
|
||||
public function processAction()
|
||||
{
|
||||
$strErrorDesc = '';
|
||||
$requestMethod = $_SERVER["REQUEST_METHOD"];
|
||||
$arrQueryStringParams = $this->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)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
53
Controller/API/PurchaseController.php
Normal file
53
Controller/API/PurchaseController.php
Normal file
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
header("Content-Type: application/json");
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
|
||||
*/
|
||||
|
||||
/**
|
||||
* Description of PurchaseController
|
||||
*
|
||||
* @author SCTN4
|
||||
*/
|
||||
class PurchaseController extends BaseController{
|
||||
/**
|
||||
* "/purchase/list" Endpoint - Get list of purchases
|
||||
*/
|
||||
public function listAction()
|
||||
{
|
||||
$strErrorDesc = '';
|
||||
$requestMethod = $_SERVER["REQUEST_METHOD"];
|
||||
$arrQueryStringParams = $this->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)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
14
Include/bootstrap.php
Normal file
14
Include/bootstrap.php
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/EmptyPHP.php to edit this template
|
||||
*/
|
||||
|
||||
define("PROJECT_ROOT_PATH", __DIR__ . "/../");
|
||||
// include main configuration file
|
||||
require_once PROJECT_ROOT_PATH . "/include/config.php";
|
||||
// include the base controller file
|
||||
require_once PROJECT_ROOT_PATH . "/Controller/Api/BaseController.php";
|
||||
// include the use model file
|
||||
require_once PROJECT_ROOT_PATH . "/Model/CustomerModel.php";
|
11
Include/sampleConfig.php
Normal file
11
Include/sampleConfig.php
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/EmptyPHP.php to edit this template
|
||||
*/
|
||||
|
||||
define("DB_HOST", "<hostname>");
|
||||
define("DB_USERNAME", "<username>");
|
||||
define("DB_PASSWORD", '<password>');
|
||||
define("DB_DATABASE_NAME", "<database>");
|
97
Model/CustomerModel.php
Normal file
97
Model/CustomerModel.php
Normal file
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
|
||||
*/
|
||||
|
||||
/**
|
||||
* Description of CustomerModel
|
||||
*
|
||||
* @author Mike Howard
|
||||
* CREATED DATE: 04/10/2024
|
||||
*
|
||||
* customer
|
||||
* customer_id bigint AI PK
|
||||
* customer_name varchar(255)
|
||||
* customer_email varchar(255)
|
||||
* customer_phone varchar(12)
|
||||
* customer_birthday datetime
|
||||
* address_code_id bigint
|
||||
* loyalty_member tinyint
|
||||
*
|
||||
* address_code
|
||||
* address_code_id bigint AI PK
|
||||
* address_city varchar(45)
|
||||
* address_state varchar(45)
|
||||
* address_zip varchar(10)
|
||||
*/
|
||||
require_once PROJECT_ROOT_PATH . "/Model/Database.php";
|
||||
|
||||
class CustomerModel extends Database {
|
||||
private $params = array();
|
||||
|
||||
public function __set($name, $value)
|
||||
{
|
||||
//echo "Setting '$name' to '$value'\n";
|
||||
$this->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]);
|
||||
}
|
||||
}
|
87
Model/Database.php
Normal file
87
Model/Database.php
Normal file
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
|
||||
*/
|
||||
|
||||
/**
|
||||
* Description of Database
|
||||
*
|
||||
* @author SCTN4
|
||||
*/
|
||||
class Database {
|
||||
protected $connection = null;
|
||||
public function __construct()
|
||||
{
|
||||
try {
|
||||
$this->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() );
|
||||
}
|
||||
}
|
||||
}
|
20
Model/PurchaseModel.php
Normal file
20
Model/PurchaseModel.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
|
||||
*/
|
||||
|
||||
/**
|
||||
* Description of PurchaseModel
|
||||
*
|
||||
* @author SCTN4
|
||||
*/
|
||||
require_once PROJECT_ROOT_PATH . "/Model/Database.php";
|
||||
|
||||
class PurchaseModel extends Database{
|
||||
public function getPurchases($date, $limit)
|
||||
{
|
||||
return $this->select("SELECT * FROM purchases WHERE purchase_date = '{$date}' ORDER BY purchase_id ASC LIMIT {$limit}");
|
||||
}
|
||||
}
|
41
index.php
Normal file
41
index.php
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
header("Content-Type: application/json");
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/EmptyPHP.php to edit this template
|
||||
*/
|
||||
/* USAGE
|
||||
* https://localhost/index.php/{MODULE_NAME}/{METHOD_NAME}?limit={LIMIT_VALUE}
|
||||
* http://localhost/index.php/customer/process/list?limit=20
|
||||
*/
|
||||
require __DIR__ . "/include/bootstrap.php";
|
||||
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
||||
$uri = explode( '/', $uri );
|
||||
|
||||
if (!isset($uri[2]) || !isset($uri[3]) || !isset($uri[4])) {
|
||||
header("HTTP/1.1 404 Not Found");
|
||||
exit();
|
||||
}
|
||||
|
||||
switch($uri[2]) {
|
||||
case "purchase":
|
||||
header("HTTP/1.1 404 Module Not Defined");
|
||||
exit();
|
||||
//require PROJECT_ROOT_PATH . "/Controller/Api/PurchaseController.php";
|
||||
//$objFeedController = new PurchaseController();
|
||||
break;
|
||||
|
||||
case "customer":
|
||||
require PROJECT_ROOT_PATH . "/Controller/Api/CustomerController.php";
|
||||
$objFeedController = new CustomerController();
|
||||
break;
|
||||
|
||||
default:
|
||||
header("HTTP/1.1 404 Module Not Found");
|
||||
exit();
|
||||
break;
|
||||
}
|
||||
|
||||
$strMethodName = $uri[3] . 'Action';
|
||||
$objFeedController->{$strMethodName}();
|
||||
|
8
nbproject/project.properties
Normal file
8
nbproject/project.properties
Normal file
|
@ -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=.
|
9
nbproject/project.xml
Normal file
9
nbproject/project.xml
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://www.netbeans.org/ns/project/1">
|
||||
<type>org.netbeans.modules.php.project</type>
|
||||
<configuration>
|
||||
<data xmlns="http://www.netbeans.org/ns/php-project/1">
|
||||
<name>CustomerRewardsRESTAPI</name>
|
||||
</data>
|
||||
</configuration>
|
||||
</project>
|
Loading…
Reference in New Issue
Block a user