2024-04-15 11:38:59 -05:00
|
|
|
<?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
|
2024-04-17 13:55:27 -05:00
|
|
|
* http://localhost:8000/index.php/customer/process/select?limit=20
|
2024-04-15 11:38:59 -05:00
|
|
|
*/
|
|
|
|
class CustomerController extends BaseController{
|
|
|
|
/**
|
|
|
|
* "/customer/list" Endpoint - Get list of users
|
|
|
|
*/
|
2024-04-30 12:36:23 -05:00
|
|
|
private $customerModel;
|
|
|
|
private $requestMethod;
|
|
|
|
private $arrQueryStringParams;
|
|
|
|
private $strErrorDesc;
|
|
|
|
private $strErrorHeader;
|
|
|
|
|
|
|
|
public $action;
|
|
|
|
|
|
|
|
function __construct() {
|
|
|
|
$this->customerModel = new CustomerModel();
|
|
|
|
$this->requestMethod = $this->getServerRequestMethod();
|
|
|
|
}
|
|
|
|
|
2024-05-01 15:03:43 -05:00
|
|
|
public function processAction() {
|
2024-04-30 12:36:23 -05:00
|
|
|
$this->strErrorDesc = '';
|
2024-05-02 16:00:15 -05:00
|
|
|
$this->strErrorHeader = '';
|
2024-04-30 12:36:23 -05:00
|
|
|
try {
|
|
|
|
switch($this->action) {
|
|
|
|
case "select":
|
2024-05-01 15:03:43 -05:00
|
|
|
$response = $this->selectAction();
|
2024-04-30 12:36:23 -05:00
|
|
|
break;
|
|
|
|
|
2024-05-02 16:00:15 -05:00
|
|
|
case "insert":
|
|
|
|
$response = $this->insertCustomer();
|
|
|
|
break;
|
2024-04-15 11:38:59 -05:00
|
|
|
|
2024-05-02 16:00:15 -05:00
|
|
|
case "update":
|
2024-04-30 22:54:04 -05:00
|
|
|
$response = $this->updateCustomer();
|
2024-05-02 16:00:15 -05:00
|
|
|
break;
|
2024-04-15 11:38:59 -05:00
|
|
|
|
2024-05-02 16:00:15 -05:00
|
|
|
case "delete":
|
2024-04-30 22:54:04 -05:00
|
|
|
$response = $this->deleteCustomer();
|
2024-05-02 16:00:15 -05:00
|
|
|
break;
|
2024-04-15 11:38:59 -05:00
|
|
|
|
2024-05-02 16:00:15 -05:00
|
|
|
default:
|
|
|
|
$response = (object) ['Result' => 'Default'];
|
|
|
|
$this->strErrorDesc = 'Controller Method not supported for processAction: ' . $this->action;
|
|
|
|
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
|
|
|
|
break;
|
2024-04-15 11:38:59 -05:00
|
|
|
}
|
2024-04-30 12:36:23 -05:00
|
|
|
|
2024-05-02 16:00:15 -05:00
|
|
|
$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';
|
|
|
|
}
|
|
|
|
|
2024-04-15 11:38:59 -05:00
|
|
|
// send output
|
2024-05-02 16:00:15 -05:00
|
|
|
if (!$this->strErrorDesc) {
|
2024-04-15 11:38:59 -05:00
|
|
|
$this->sendOutput(
|
|
|
|
$responseData,
|
|
|
|
array('Content-Type: application/json', 'HTTP/1.1 200 OK')
|
|
|
|
);
|
|
|
|
} else {
|
2024-05-02 16:00:15 -05:00
|
|
|
$this->sendOutput(json_encode(array('error' => $this->strErrorDesc)),
|
|
|
|
array('Content-Type: application/json', $this->strErrorHeader)
|
2024-04-15 11:38:59 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-04-30 12:36:23 -05:00
|
|
|
|
|
|
|
private function selectAction(){
|
|
|
|
if ($this->checkRequestType('GET') == 'false') {
|
|
|
|
$this->strErrorDesc = 'Request Method not supported for processAction';
|
2024-05-01 15:03:43 -05:00
|
|
|
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
|
2024-04-30 12:36:23 -05:00
|
|
|
return;
|
|
|
|
}
|
2024-05-02 16:00:15 -05:00
|
|
|
$this->arrQueryStringParams = $this->getQueryStringParams();
|
2024-05-01 15:03:43 -05:00
|
|
|
if (isset($this->arrQueryStringParams['customer_id'])) {
|
|
|
|
$response = $this->selectByIdAction();
|
|
|
|
} else {
|
|
|
|
$this->customerModel->limit = 10;
|
2024-04-30 12:36:23 -05:00
|
|
|
|
2024-05-01 15:03:43 -05:00
|
|
|
if (isset($this->arrQueryStringParams['limit'])) {
|
|
|
|
$this->customerModel->limit = $this->arrQueryStringParams['limit'];
|
|
|
|
}
|
|
|
|
$response = $this->customerModel->findAllCustomers();
|
|
|
|
unset($this->customerModel->limit);
|
2024-04-30 12:36:23 -05:00
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function selectByIdAction(){
|
2024-05-01 15:03:43 -05:00
|
|
|
if (isset($this->arrQueryStringParams['customer_id'])) {
|
|
|
|
$this->customerModel->customerId = $this->arrQueryStringParams['customer_id'];
|
2024-04-30 12:36:23 -05:00
|
|
|
$response = $this->customerModel->findByCustomerId();
|
|
|
|
unset($this->customerModel->customerId);
|
|
|
|
} else {
|
|
|
|
return $this->notFoundResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2024-04-30 22:54:04 -05:00
|
|
|
private function insertCustomer()
|
2024-04-30 12:36:23 -05:00
|
|
|
{
|
2024-04-30 22:54:04 -05:00
|
|
|
if ($this->checkRequestType('POST') == 'false') {
|
|
|
|
$this->strErrorDesc = 'Request Method not supported for processAction';
|
2024-05-01 15:03:43 -05:00
|
|
|
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
|
2024-04-30 22:54:04 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// reads the raw POST data and returns it as a string.
|
2024-05-03 12:41:39 -05:00
|
|
|
$jsonPayload = file_get_contents('php://input');
|
|
|
|
$input = json_decode($jsonPayload, TRUE);
|
2024-05-03 19:27:12 -05:00
|
|
|
if (! $this->validatePerson($input)) {
|
|
|
|
return $this->unprocessableEntityResponse();
|
|
|
|
}
|
2024-05-04 00:22:26 -05:00
|
|
|
|
2024-05-03 12:41:39 -05:00
|
|
|
//remove customer_id field so it doesn't break
|
|
|
|
unset($input['customer_id']);
|
2024-05-04 00:22:26 -05:00
|
|
|
|
2024-05-03 12:41:39 -05:00
|
|
|
$response = $this->customerModel->insertCustomer($input);
|
2024-04-30 12:36:23 -05:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2024-04-30 22:54:04 -05:00
|
|
|
private function updateCustomer()
|
2024-04-30 12:36:23 -05:00
|
|
|
{
|
2024-04-30 22:54:04 -05:00
|
|
|
if ($this->checkRequestType('PUT') == 'false') {
|
|
|
|
$this->strErrorDesc = 'Request Method not supported for processAction';
|
2024-05-01 15:03:43 -05:00
|
|
|
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
|
2024-04-30 22:54:04 -05:00
|
|
|
return;
|
2024-04-30 12:36:23 -05:00
|
|
|
}
|
2024-05-02 02:19:13 -05:00
|
|
|
$jsonPayload = file_get_contents('php://input');
|
2024-05-02 16:00:15 -05:00
|
|
|
$input = json_decode($jsonPayload, TRUE);
|
2024-05-01 15:03:43 -05:00
|
|
|
|
|
|
|
if (! $this->validatePerson($input)) {
|
|
|
|
return $this->unprocessableEntityResponse();
|
|
|
|
}
|
|
|
|
|
2024-05-02 16:00:15 -05:00
|
|
|
if ($input['customer_id'] != null) {
|
|
|
|
$this->customerModel->customerId = $input['customer_id'];
|
2024-04-30 22:54:04 -05:00
|
|
|
$result = $this->customerModel->findByCustomerId();
|
|
|
|
if (! $result) {
|
|
|
|
return $this->notFoundResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->customerModel->updateCustomer($input);
|
|
|
|
unset($this->customerModel->customerId);
|
|
|
|
} else {
|
|
|
|
return $this->notFoundResponse();
|
2024-04-30 12:36:23 -05:00
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2024-04-30 22:54:04 -05:00
|
|
|
private function deleteCustomer()
|
2024-04-30 12:36:23 -05:00
|
|
|
{
|
2024-05-01 15:03:43 -05:00
|
|
|
if ($this->checkRequestType('DELETE') == 'false') {
|
|
|
|
$this->strErrorDesc = 'Request Method not supported for processAction';
|
|
|
|
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
|
|
|
|
return;
|
|
|
|
}
|
2024-05-04 00:22:26 -05:00
|
|
|
|
|
|
|
$this->arrQueryStringParams = $this->getQueryStringParams();
|
|
|
|
|
|
|
|
if (isset($this->arrQueryStringParams['customerId'])) {
|
|
|
|
$this->customerModel->customerId = $this->arrQueryStringParams['customerId'];
|
2024-04-30 22:54:04 -05:00
|
|
|
$result = $this->customerModel->findByCustomerId();
|
2024-05-04 00:22:26 -05:00
|
|
|
if (!$result) {
|
2024-04-30 22:54:04 -05:00
|
|
|
return $this->notFoundResponse();
|
|
|
|
}
|
|
|
|
$response = $this->customerModel->deleteCustomer();
|
|
|
|
unset($this->customerModel->customerId);
|
|
|
|
} else {
|
2024-04-30 12:36:23 -05:00
|
|
|
return $this->notFoundResponse();
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2024-05-01 15:03:43 -05:00
|
|
|
private function checkRequestType($request)
|
2024-04-30 12:36:23 -05:00
|
|
|
{
|
2024-05-01 15:03:43 -05:00
|
|
|
$response = 'false';
|
|
|
|
if (strtoupper($this->requestMethod) == $request) {
|
|
|
|
$response = 'true';
|
2024-04-30 12:36:23 -05:00
|
|
|
}
|
2024-05-01 15:03:43 -05:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function validatePerson($input)
|
|
|
|
{
|
2024-05-02 02:19:13 -05:00
|
|
|
$validtion = false;
|
2024-05-03 12:41:39 -05:00
|
|
|
if($input['customer_name_first'] != null){
|
2024-05-02 02:19:13 -05:00
|
|
|
$validtion = true;
|
2024-05-02 16:00:15 -05:00
|
|
|
if($input['customer_name_last'] == null) {
|
2024-05-02 02:19:13 -05:00
|
|
|
$validtion = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $validtion;
|
2024-04-30 12:36:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private function unprocessableEntityResponse()
|
|
|
|
{
|
2024-05-01 15:03:43 -05:00
|
|
|
$response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Payload';
|
2024-04-30 12:36:23 -05:00
|
|
|
$response['body'] = json_encode([
|
|
|
|
'error' => 'Invalid input'
|
|
|
|
]);
|
2024-05-02 02:19:13 -05:00
|
|
|
$this->strErrorDesc = 'Unprocessable Payload';
|
|
|
|
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Payload';
|
2024-04-30 12:36:23 -05:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function notFoundResponse()
|
|
|
|
{
|
2024-05-01 15:03:43 -05:00
|
|
|
$response['status_code_header'] = 'HTTP/1.1 404 Entity Not Found';
|
2024-04-30 12:36:23 -05:00
|
|
|
$response['body'] = null;
|
2024-05-02 02:19:13 -05:00
|
|
|
$this->strErrorDesc = 'Request Entity Not Found';
|
|
|
|
$this->strErrorHeader = 'HTTP/1.1 422 Entity Not Found';
|
2024-04-30 12:36:23 -05:00
|
|
|
return $response;
|
|
|
|
}
|
2024-04-15 11:38:59 -05:00
|
|
|
}
|