mirror of
https://github.com/sctn4elk/CustomerRewardsRESTAPI.git
synced 2025-01-09 15:24:30 -06:00
196 lines
6.5 KiB
PHP
196 lines
6.5 KiB
PHP
<?php
|
|
/**
|
|
* Description of CustomerController
|
|
*
|
|
* @author Mike Howard
|
|
* 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
|
|
*/
|
|
public $customerModel;
|
|
public $action;
|
|
|
|
function __construct() {
|
|
parent::__construct();
|
|
$this->basename = "CustomerController";
|
|
}
|
|
|
|
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();
|
|
break;
|
|
|
|
case "insert":
|
|
$response = $this->insertCustomer();
|
|
break;
|
|
|
|
case "update":
|
|
$response = $this->updateCustomer();
|
|
break;
|
|
|
|
case "delete":
|
|
$response = $this->deleteCustomer();
|
|
break;
|
|
|
|
default:
|
|
$response = $this->unprocessableRequestResponse("processAction");
|
|
break;
|
|
}
|
|
|
|
$responseData = json_encode($response);
|
|
|
|
} catch (Error $e) {
|
|
$this->internalErrorResponse($e);
|
|
}
|
|
|
|
// send output
|
|
if (!$this->strErrorDesc) {
|
|
$this->sendOutput(
|
|
$responseData,
|
|
array('Content-Type: application/json', 'HTTP/1.1 200 OK')
|
|
);
|
|
} else {
|
|
$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') {
|
|
$response = $this->unprocessableRequestResponse("selectAction");
|
|
return $response;
|
|
}
|
|
|
|
$this->arrQueryStringParams = $this->getQueryStringParams();
|
|
|
|
if (isset($this->arrQueryStringParams['customer_id'])) {
|
|
$response = $this->selectByIdAction();
|
|
} else {
|
|
$this->customerModel->limit = 20;
|
|
|
|
if (isset($this->arrQueryStringParams['limit'])) {
|
|
$this->customerModel->limit = $this->arrQueryStringParams['limit'];
|
|
}
|
|
$response = $this->customerModel->findAllCustomers();
|
|
unset($this->customerModel->limit);
|
|
}
|
|
return $response;
|
|
}
|
|
|
|
private function selectByIdAction(){
|
|
if (isset($this->arrQueryStringParams['customer_id'])) {
|
|
$this->customerModel->customerId = $this->arrQueryStringParams['customer_id'];
|
|
$response = $this->customerModel->findByCustomerId();
|
|
unset($this->customerModel->customerId);
|
|
} else {
|
|
$response = $this->notFoundResponse("selectByIdAction");
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
private function insertCustomer()
|
|
{
|
|
if ($this->checkRequestType('POST') == 'false') {
|
|
$response = $this->unprocessableRequestResponse("insertCustomer");
|
|
return $response;
|
|
}
|
|
// reads the raw POST data and returns it as a string.
|
|
$jsonPayload = file_get_contents('php://input');
|
|
$input = json_decode($jsonPayload, TRUE);
|
|
if (! $this->validatePerson($input)) {
|
|
return $this->unprocessableEntityResponse();
|
|
}
|
|
|
|
//remove customer_id field so it doesn't break
|
|
unset($input['customer_id']);
|
|
unset($input['image_path']);
|
|
|
|
$response = $this->customerModel->insertCustomer($input);
|
|
return $response;
|
|
}
|
|
|
|
private function updateCustomer()
|
|
{
|
|
if ($this->checkRequestType('PUT') == 'false') {
|
|
$response = $this->unprocessableRequestResponse("updateCustomer");
|
|
return $response;
|
|
}
|
|
$jsonPayload = file_get_contents('php://input');
|
|
$input = json_decode($jsonPayload, TRUE);
|
|
|
|
if (! $this->validatePerson($input)) {
|
|
return $this->unprocessableEntityResponse("validatePerson");
|
|
}
|
|
|
|
if ($input['customer_id'] != null) {
|
|
$this->customerModel->customerId = $input['customer_id'];
|
|
$result = $this->customerModel->findByCustomerId();
|
|
if (! $result) {
|
|
return $this->notFoundResponse("updateCustomer->findByCustomerId");
|
|
}
|
|
|
|
$response = $this->customerModel->updateCustomer($input);
|
|
unset($this->customerModel->customerId);
|
|
} else {
|
|
return $this->notFoundResponse("updateCustomer->customer_id");
|
|
}
|
|
return $response;
|
|
}
|
|
|
|
private function deleteCustomer()
|
|
{
|
|
if ($this->checkRequestType('DELETE') == 'false') {
|
|
$response = $this->unprocessableRequestResponse("deleteCustomer");
|
|
return $response;
|
|
}
|
|
|
|
$this->arrQueryStringParams = $this->getQueryStringParams();
|
|
|
|
if (isset($this->arrQueryStringParams['customerId'])) {
|
|
$this->customerModel->customerId = $this->arrQueryStringParams['customerId'];
|
|
$result = $this->customerModel->findByCustomerId();
|
|
if (!$result) {
|
|
return $this->notFoundResponse("deleteCustomer->findByCustomerId");
|
|
}
|
|
$response = $this->customerModel->deleteCustomer();
|
|
unset($this->customerModel->customerId);
|
|
} else {
|
|
return $this->notFoundResponse("deleteCustomer->customer_id");
|
|
}
|
|
return $response;
|
|
}
|
|
|
|
private function validatePerson($input)
|
|
{
|
|
$validtion = false;
|
|
if($input['customer_name_first'] != null){
|
|
$validtion = true;
|
|
if($input['customer_name_last'] == null) {
|
|
$validtion = false;
|
|
}
|
|
}
|
|
return $validtion;
|
|
}
|
|
}
|