mirror of
https://github.com/sctn4elk/CustomerRewardsRESTAPI.git
synced 2025-01-09 23:54:29 -06:00
98 lines
4.3 KiB
PHP
98 lines
4.3 KiB
PHP
<?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
|
|
* http://localhost:8000/index.php/customer/process/select?limit=20
|
|
*/
|
|
class CustomerController extends BaseController{
|
|
/**
|
|
* "/customer/list" Endpoint - Get list of users
|
|
*/
|
|
public function processAction($action)
|
|
{
|
|
$strErrorDesc = '';
|
|
$requestMethod = $_SERVER["REQUEST_METHOD"];
|
|
$arrQueryStringParams = $this->getQueryStringParams();
|
|
if (strtoupper($requestMethod) == 'GET') {
|
|
try {
|
|
$customerModel = new CustomerModel();
|
|
|
|
$uri = $this->getUriSegments();
|
|
switch($action) {
|
|
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: ' . $action;
|
|
$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)
|
|
);
|
|
}
|
|
}
|
|
}
|