Work on attributes

This commit is contained in:
sctn4elk 2024-05-22 14:55:08 -05:00
parent 5481bb7698
commit 0878d1af1c
6 changed files with 118 additions and 84 deletions

View File

@ -1,10 +1,13 @@
<?php <?php
/** /**
* Description of AttributeController * Description of AttributeController
* *
* @author Mike Howard * @author Mike Howard
*/ */
header("Content-Type: application/json");
require_once PD . "/Model/AttributeModel.php";
class AttributeController extends BaseController { class AttributeController extends BaseController {
public $attributeModel; public $attributeModel;
public $action; public $action;
@ -17,9 +20,11 @@ class AttributeController extends BaseController {
public function processAction() { public function processAction() {
$this->strErrorDesc = ''; $this->strErrorDesc = '';
$this->strErrorHeader = ''; $this->strErrorHeader = '';
$this->strErrorMessage = '';
try { try {
$this->requestMethod = $this->getServerRequestMethod(); $this->requestMethod = $this->getServerRequestMethod();
$this->attributeModel = new AttributeModel();
switch($this->action) { switch($this->action) {
case "select": case "select":
@ -39,16 +44,14 @@ class AttributeController extends BaseController {
break; break;
default: default:
$response = (object) ['Result' => 'Default']; $response = $this->unprocessableRequestResponse("processAction");
$this->strErrorDesc = 'Controller Method not supported for processAction: ' . $this->action;
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
break; break;
} }
$responseData = json_encode($response); $responseData = json_encode($response);
} catch (Error $e) { } catch (Error $e) {
$this->strErrorDesc = $e->getMessage().' Something went wrong in processAction! Please contact support.'; $this->internalErrorResponse($e);
$this->strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
} }
// send output // send output
@ -58,7 +61,10 @@ class AttributeController extends BaseController {
array('Content-Type: application/json', 'HTTP/1.1 200 OK') array('Content-Type: application/json', 'HTTP/1.1 200 OK')
); );
} else { } else {
$this->sendOutput(json_encode(array('error' => $this->strErrorDesc)), $this->sendOutput(json_encode(
array('error' => $this->strErrorDesc,
'message' => $this->strErrorMessage,
'controller' => $this->basename)),
array('Content-Type: application/json', $this->strErrorHeader) array('Content-Type: application/json', $this->strErrorHeader)
); );
} }
@ -66,35 +72,27 @@ class AttributeController extends BaseController {
private function selectAction(){ private function selectAction(){
if ($this->checkRequestType('GET') == 'false') { if ($this->checkRequestType('GET') == 'false') {
$this->strErrorDesc = 'Request Method not supported for processAction'; $response = $this->unprocessableRequestResponse("selectAction");
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request'; return $response;
return;
} }
$this->attributeModel = new AttributeModel();
$this->arrQueryStringParams = $this->getQueryStringParams(); $this->arrQueryStringParams = $this->getQueryStringParams();
if (isset($this->arrQueryStringParams['loyalty_member_id'])) { if (isset($this->arrQueryStringParams['loyalty_member_id'])) {
$response = $this->selectByIdAction(); $response = $this->selectByMemberIdAction();
} else { } else {
//$this->attributeModel->limit = 10;
if (isset($this->arrQueryStringParams['limit'])) {
$this->attributeModel->limit = $this->arrQueryStringParams['limit'];
}
$response = $this->attributeModel->findAllAttributes(); $response = $this->attributeModel->findAllAttributes();
unset($this->attributeModel->limit);
} }
return $response; return $response;
} }
private function selectByIdAction(){ private function selectByMemberIdAction(){
if (isset($this->arrQueryStringParams['loyalty_member_id'])) { if (isset($this->arrQueryStringParams['loyalty_member_id'])) {
$this->attributeModel->attributeId = $this->arrQueryStringParams['loyalty_member_id']; $this->attributeModel->memberId = $this->arrQueryStringParams['loyalty_member_id'];
$response = $this->attributeModel->findAttributesByMemberId(); $response = $this->attributeModel->findAttributesByMemberId();
unset($this->attributeModel->attributeId); unset($this->attributeModel->memberId);
} else { } else {
return $this->notFoundResponse(); $response = $this->notFoundResponse("selectByMemberIdAction");
} }
return $response; return $response;

View File

@ -11,6 +11,7 @@ class BaseController {
public $arrQueryStringParams; public $arrQueryStringParams;
public $strErrorDesc; public $strErrorDesc;
public $strErrorHeader; public $strErrorHeader;
public $strErrorMessage;
public function __construct() { public function __construct() {
$this->basename = 'BaseController'; $this->basename = 'BaseController';
@ -26,16 +27,25 @@ class BaseController {
$this->sendOutput('', array('HTTP/1.1 404 Non-Existant method or inaccessible method called')); $this->sendOutput('', array('HTTP/1.1 404 Non-Existant method or inaccessible method called'));
} }
public function checkRequestType($request) /*
* Set the error description when an unknown error occurs
*/
public function internalErrorResponse($error)
{ {
$response = 'false'; $response['status_code_header'] = 'HTTP/1.1 500 Internal Server Error';
if (strtoupper($this->requestMethod) == $request) { $response['body'] = json_encode([
$response = 'true'; 'error' => 'Invalid request'
} ]);
$this->strErrorDesc = 'An internal Error has occured! Please contact support.';
$this->strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
$this->strErrorMessage = $error->getMessage();
return $response; return $response;
} }
public function unprocessableRequestResponse() /*
* Set the error description when an unknown method is called
*/
public function unprocessableRequestResponse($msg)
{ {
$response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Request'; $response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Request';
$response['body'] = json_encode([ $response['body'] = json_encode([
@ -43,10 +53,14 @@ class BaseController {
]); ]);
$this->strErrorDesc = 'Request Method not supported for processAction'; $this->strErrorDesc = 'Request Method not supported for processAction';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request'; $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
$this->strErrorMessage = $msg;
return $response; return $response;
} }
public function unprocessableEntityResponse() /*
* Set the error description when the payload does not contain the required info
*/
public function unprocessableEntityResponse($msg)
{ {
$response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Payload'; $response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Payload';
$response['body'] = json_encode([ $response['body'] = json_encode([
@ -54,23 +68,28 @@ class BaseController {
]); ]);
$this->strErrorDesc = 'Unprocessable Payload'; $this->strErrorDesc = 'Unprocessable Payload';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Payload'; $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Payload';
$this->strErrorMessage = $msg;
return $response; return $response;
} }
public function notFoundResponse() /*
* Set the error description when the
*/
public function notFoundResponse($msg)
{ {
$response['status_code_header'] = 'HTTP/1.1 404 Entity Not Found'; $response['status_code_header'] = 'HTTP/1.1 404 Entity Not Found';
$response['body'] = null; $response['body'] = null;
$this->strErrorDesc = 'Request Entity Not Found'; $this->strErrorDesc = 'Request Entity Not Found';
$this->strErrorHeader = 'HTTP/1.1 422 Entity Not Found'; $this->strErrorHeader = 'HTTP/1.1 422 Entity Not Found';
$this->strErrorMessage = $msg;
return $response; return $response;
} }
/** /**
* Get URI elements. * Get URI elements.
* *
* @return array * @return array
*/ */
public function getUriSegments() public function getUriSegments()
{ {
$requestUri = filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_STRING); /*htmlspecialchars()*/ $requestUri = filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_STRING); /*htmlspecialchars()*/
@ -79,10 +98,10 @@ class BaseController {
return $uri; return $uri;
} }
/** /**
* Get querystring params. * Get querystring params.
* *
* @return array * @return array
*/ */
public function getQueryStringParams() public function getQueryStringParams()
{ {
$query = array(); $query = array();
@ -96,12 +115,25 @@ class BaseController {
$requestMethod = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING); /*htmlspecialchars()*/ $requestMethod = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING); /*htmlspecialchars()*/
return $requestMethod; return $requestMethod;
} }
/*
* Validate the resuest type against the called method
*/
public function checkRequestType($request)
{
$response = 'false';
if (strtoupper($this->requestMethod) == $request) {
$response = 'true';
}
return $response;
}
/** /**
* Send API output. * Send API output.
* *
* @param mixed $data * @param mixed $data
* @param string $httpHeader * @param string $httpHeader
*/ */
public function sendOutput($data, $httpHeaders=array()) public function sendOutput($data, $httpHeaders=array())
{ {
header_remove('Set-Cookie'); header_remove('Set-Cookie');

View File

@ -1,7 +1,4 @@
<?php <?php
header("Content-Type: application/json");
require_once PD . "/Model/CustomerModel.php";
/** /**
* Description of CustomerController * Description of CustomerController
* *
@ -9,6 +6,10 @@ require_once PD . "/Model/CustomerModel.php";
* 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/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 * http://localhost:8000/index.php/customer/process/select?limit=20
*/ */
header("Content-Type: application/json");
require_once PD . "/Model/CustomerModel.php";
class CustomerController extends BaseController { class CustomerController extends BaseController {
/** /**
* "/customer/list" Endpoint - Get list of users * "/customer/list" Endpoint - Get list of users
@ -24,9 +25,11 @@ class CustomerController extends BaseController {
public function processAction() { public function processAction() {
$this->strErrorDesc = ''; $this->strErrorDesc = '';
$this->strErrorHeader = ''; $this->strErrorHeader = '';
$this->strErrorMessage = '';
try { try {
$this->requestMethod = $this->getServerRequestMethod(); $this->requestMethod = $this->getServerRequestMethod();
$this->customerModel = new CustomerModel();
switch($this->action) { switch($this->action) {
case "select": case "select":
@ -46,16 +49,14 @@ class CustomerController extends BaseController {
break; break;
default: default:
$response = (object) ['Result' => 'Default']; $response = $this->unprocessableRequestResponse("processAction");
$this->strErrorDesc = 'Controller Method not supported for processAction: ' . $this->action;
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
break; break;
} }
$responseData = json_encode($response); $responseData = json_encode($response);
} catch (Error $e) { } catch (Error $e) {
$this->strErrorDesc = $e->getMessage().' Something went wrong in processAction! Please contact support.'; $this->internalErrorResponse($e);
$this->strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
} }
// send output // send output
@ -65,7 +66,10 @@ class CustomerController extends BaseController {
array('Content-Type: application/json', 'HTTP/1.1 200 OK') array('Content-Type: application/json', 'HTTP/1.1 200 OK')
); );
} else { } else {
$this->sendOutput(json_encode(array('error' => $this->strErrorDesc)), $this->sendOutput(json_encode(
array('error' => $this->strErrorDesc,
'message' => $this->strErrorMessage,
'controller' => $this->basename)),
array('Content-Type: application/json', $this->strErrorHeader) array('Content-Type: application/json', $this->strErrorHeader)
); );
} }
@ -73,18 +77,16 @@ class CustomerController extends BaseController {
private function selectAction(){ private function selectAction(){
if ($this->checkRequestType('GET') == 'false') { if ($this->checkRequestType('GET') == 'false') {
$this->strErrorDesc = 'Request Method not supported for processAction'; $response = $this->unprocessableRequestResponse("selectAction");
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request'; return $response;
return;
} }
$this->customerModel = new CustomerModel();
$this->arrQueryStringParams = $this->getQueryStringParams(); $this->arrQueryStringParams = $this->getQueryStringParams();
if (isset($this->arrQueryStringParams['customer_id'])) { if (isset($this->arrQueryStringParams['customer_id'])) {
$response = $this->selectByIdAction(); $response = $this->selectByIdAction();
} else { } else {
//$this->customerModel->limit = 10; $this->customerModel->limit = 20;
if (isset($this->arrQueryStringParams['limit'])) { if (isset($this->arrQueryStringParams['limit'])) {
$this->customerModel->limit = $this->arrQueryStringParams['limit']; $this->customerModel->limit = $this->arrQueryStringParams['limit'];
@ -101,7 +103,7 @@ class CustomerController extends BaseController {
$response = $this->customerModel->findByCustomerId(); $response = $this->customerModel->findByCustomerId();
unset($this->customerModel->customerId); unset($this->customerModel->customerId);
} else { } else {
return $this->notFoundResponse(); $response = $this->notFoundResponse("selectByIdAction");
} }
return $response; return $response;
@ -110,9 +112,8 @@ class CustomerController extends BaseController {
private function insertCustomer() private function insertCustomer()
{ {
if ($this->checkRequestType('POST') == 'false') { if ($this->checkRequestType('POST') == 'false') {
$this->strErrorDesc = 'Request Method not supported for processAction'; $response = $this->unprocessableRequestResponse("insertCustomer");
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request'; return $response;
return;
} }
// reads the raw POST data and returns it as a string. // reads the raw POST data and returns it as a string.
$jsonPayload = file_get_contents('php://input'); $jsonPayload = file_get_contents('php://input');
@ -131,28 +132,27 @@ class CustomerController extends BaseController {
private function updateCustomer() private function updateCustomer()
{ {
if ($this->checkRequestType('PUT') == 'false') { if ($this->checkRequestType('PUT') == 'false') {
$this->strErrorDesc = 'Request Method not supported for processAction'; $response = $this->unprocessableRequestResponse("updateCustomer");
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request'; return $response;
return;
} }
$jsonPayload = file_get_contents('php://input'); $jsonPayload = file_get_contents('php://input');
$input = json_decode($jsonPayload, TRUE); $input = json_decode($jsonPayload, TRUE);
if (! $this->validatePerson($input)) { if (! $this->validatePerson($input)) {
return $this->unprocessableEntityResponse(); return $this->unprocessableEntityResponse("validatePerson");
} }
if ($input['customer_id'] != null) { if ($input['customer_id'] != null) {
$this->customerModel->customerId = $input['customer_id']; $this->customerModel->customerId = $input['customer_id'];
$result = $this->customerModel->findByCustomerId(); $result = $this->customerModel->findByCustomerId();
if (! $result) { if (! $result) {
return $this->notFoundResponse(); return $this->notFoundResponse("updateCustomer->findByCustomerId");
} }
$response = $this->customerModel->updateCustomer($input); $response = $this->customerModel->updateCustomer($input);
unset($this->customerModel->customerId); unset($this->customerModel->customerId);
} else { } else {
return $this->notFoundResponse(); return $this->notFoundResponse("updateCustomer->customer_id");
} }
return $response; return $response;
} }
@ -160,9 +160,8 @@ class CustomerController extends BaseController {
private function deleteCustomer() private function deleteCustomer()
{ {
if ($this->checkRequestType('DELETE') == 'false') { if ($this->checkRequestType('DELETE') == 'false') {
$this->strErrorDesc = 'Request Method not supported for processAction'; $response = $this->unprocessableRequestResponse("deleteCustomer");
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request'; return $response;
return;
} }
$this->arrQueryStringParams = $this->getQueryStringParams(); $this->arrQueryStringParams = $this->getQueryStringParams();
@ -171,12 +170,12 @@ class CustomerController extends BaseController {
$this->customerModel->customerId = $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("deleteCustomer->findByCustomerId");
} }
$response = $this->customerModel->deleteCustomer(); $response = $this->customerModel->deleteCustomer();
unset($this->customerModel->customerId); unset($this->customerModel->customerId);
} else { } else {
return $this->notFoundResponse(); return $this->notFoundResponse("deleteCustomer->customer_id");
} }
return $response; return $response;
} }

View File

@ -9,12 +9,12 @@
require_once PD . "/Model/Database.php"; require_once PD . "/Model/Database.php";
require_once PD . "/Model/ModelTraits.php"; require_once PD . "/Model/ModelTraits.php";
class AttributeModel { class AttributeModel extends Database {
use ModelTraits; use ModelTraits;
public function findAllAttributes() public function findAllAttributes()
{ {
return $this->processQuery("SELECT * FROM loyalty_attribute ORDER BY loyalty_attribute_id ASC LIMIT ?", ["i", $this->limit]); return $this->processQuery("SELECT * FROM loyalty_attribute ORDER BY loyalty_attribute_id ASC");
} }
public function findAttributesByMemberId() public function findAttributesByMemberId()

View File

@ -2,7 +2,7 @@
/** /**
* Description of Database * Description of Database
* *
* @author SCTN4 * @author Mike Howard
*/ */
class Database { class Database {
protected $connection = null; protected $connection = null;

View File

@ -4,7 +4,12 @@ header("Access-Control-Allow-Methods: OPTIONS,GET,POST,PUT,DELETE");
header("Access-Control-Max-Age: 3600"); header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"); header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
/* USAGE /*
* * Description
*
* @author Mike Howard
*
* USAGE
* https://localhost/index.php/{MODULE_NAME}/{METHOD_NAME}?limit={LIMIT_VALUE} * https://localhost/index.php/{MODULE_NAME}/{METHOD_NAME}?limit={LIMIT_VALUE}
* http://localhost/index.php/customer/process/list?limit=20 * http://localhost/index.php/customer/process/list?limit=20
*/ */