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

View File

@ -11,6 +11,7 @@ class BaseController {
public $arrQueryStringParams;
public $strErrorDesc;
public $strErrorHeader;
public $strErrorMessage;
public function __construct() {
$this->basename = 'BaseController';
@ -26,16 +27,25 @@ class BaseController {
$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';
if (strtoupper($this->requestMethod) == $request) {
$response = 'true';
}
$response['status_code_header'] = 'HTTP/1.1 500 Internal Server Error';
$response['body'] = json_encode([
'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;
}
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['body'] = json_encode([
@ -43,10 +53,14 @@ class BaseController {
]);
$this->strErrorDesc = 'Request Method not supported for processAction';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
$this->strErrorMessage = $msg;
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['body'] = json_encode([
@ -54,23 +68,28 @@ class BaseController {
]);
$this->strErrorDesc = 'Unprocessable Payload';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Payload';
$this->strErrorMessage = $msg;
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['body'] = null;
$this->strErrorDesc = 'Request Entity Not Found';
$this->strErrorHeader = 'HTTP/1.1 422 Entity Not Found';
$this->strErrorMessage = $msg;
return $response;
}
/**
* Get URI elements.
*
* @return array
*/
* Get URI elements.
*
* @return array
*/
public function getUriSegments()
{
$requestUri = filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_STRING); /*htmlspecialchars()*/
@ -79,10 +98,10 @@ class BaseController {
return $uri;
}
/**
* Get querystring params.
*
* @return array
*/
* Get querystring params.
*
* @return array
*/
public function getQueryStringParams()
{
$query = array();
@ -96,12 +115,25 @@ class BaseController {
$requestMethod = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING); /*htmlspecialchars()*/
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.
*
* @param mixed $data
* @param string $httpHeader
*/
* Send API output.
*
* @param mixed $data
* @param string $httpHeader
*/
public function sendOutput($data, $httpHeaders=array())
{
header_remove('Set-Cookie');

View File

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

View File

@ -9,12 +9,12 @@
require_once PD . "/Model/Database.php";
require_once PD . "/Model/ModelTraits.php";
class AttributeModel {
class AttributeModel extends Database {
use ModelTraits;
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()

View File

@ -2,7 +2,7 @@
/**
* Description of Database
*
* @author SCTN4
* @author Mike Howard
*/
class Database {
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-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}
* http://localhost/index.php/customer/process/list?limit=20
*/