CustomerRewardsRESTAPI/Controller/API/CustomerController.php

196 lines
6.5 KiB
PHP
Raw Normal View History

2024-04-15 11:38:59 -05:00
<?php
/**
* Description of CustomerController
*
* @author Mike Howard
2024-04-15 11:38:59 -05:00
* 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
2024-04-15 11:38:59 -05:00
*/
2024-05-22 14:55:08 -05:00
header("Content-Type: application/json");
require_once PD . "/Model/CustomerModel.php";
2024-05-09 16:40:42 -05:00
class CustomerController extends BaseController {
2024-04-15 11:38:59 -05:00
/**
* "/customer/list" Endpoint - Get list of users
*/
public $customerModel;
public $action;
function __construct() {
parent::__construct();
$this->basename = "CustomerController";
}
public function processAction() {
$this->strErrorDesc = '';
2024-05-02 16:00:15 -05:00
$this->strErrorHeader = '';
2024-05-22 14:55:08 -05:00
$this->strErrorMessage = '';
2024-05-13 15:12:31 -05:00
try {
$this->requestMethod = $this->getServerRequestMethod();
2024-05-22 14:55:08 -05:00
$this->customerModel = new CustomerModel();
switch($this->action) {
case "select":
$response = $this->selectAction();
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-05-22 14:55:08 -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-05-22 14:55:08 -05:00
$response = $this->deleteCustomer();
2024-05-02 16:00:15 -05:00
break;
2024-05-09 16:40:42 -05:00
2024-05-02 16:00:15 -05:00
default:
2024-05-22 14:55:08 -05:00
$response = $this->unprocessableRequestResponse("processAction");
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
$responseData = json_encode($response);
2024-05-22 14:55:08 -05:00
2024-05-02 16:00:15 -05:00
} catch (Error $e) {
2024-05-22 14:55:08 -05:00
$this->internalErrorResponse($e);
2024-05-02 16:00:15 -05:00
}
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-22 14:55:08 -05:00
$this->sendOutput(json_encode(
array('error' => $this->strErrorDesc,
'message' => $this->strErrorMessage,
'controller' => $this->basename)),
array('Content-Type: application/json', $this->strErrorHeader)
2024-04-15 11:38:59 -05:00
);
}
}
private function selectAction(){
if ($this->checkRequestType('GET') == 'false') {
2024-05-22 14:55:08 -05:00
$response = $this->unprocessableRequestResponse("selectAction");
return $response;
}
2024-05-02 16:00:15 -05:00
$this->arrQueryStringParams = $this->getQueryStringParams();
2024-05-13 15:12:31 -05:00
if (isset($this->arrQueryStringParams['customer_id'])) {
$response = $this->selectByIdAction();
} else {
2024-05-22 14:55:08 -05:00
$this->customerModel->limit = 20;
if (isset($this->arrQueryStringParams['limit'])) {
2024-05-13 15:12:31 -05:00
$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 {
2024-05-22 14:55:08 -05:00
$response = $this->notFoundResponse("selectByIdAction");
}
return $response;
}
2024-04-30 22:54:04 -05:00
private function insertCustomer()
{
2024-04-30 22:54:04 -05:00
if ($this->checkRequestType('POST') == 'false') {
2024-05-22 14:55:08 -05:00
$response = $this->unprocessableRequestResponse("insertCustomer");
return $response;
2024-04-30 22:54:04 -05:00
}
// 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-03 12:41:39 -05:00
//remove customer_id field so it doesn't break
unset($input['customer_id']);
2024-05-24 23:33:31 -05:00
unset($input['image_path']);
2024-05-03 12:41:39 -05:00
$response = $this->customerModel->insertCustomer($input);
return $response;
}
2024-04-30 22:54:04 -05:00
private function updateCustomer()
{
2024-04-30 22:54:04 -05:00
if ($this->checkRequestType('PUT') == 'false') {
2024-05-22 14:55:08 -05:00
$response = $this->unprocessableRequestResponse("updateCustomer");
return $response;
}
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);
if (! $this->validatePerson($input)) {
2024-05-22 14:55:08 -05:00
return $this->unprocessableEntityResponse("validatePerson");
}
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) {
2024-05-22 14:55:08 -05:00
return $this->notFoundResponse("updateCustomer->findByCustomerId");
2024-04-30 22:54:04 -05:00
}
$response = $this->customerModel->updateCustomer($input);
unset($this->customerModel->customerId);
} else {
2024-05-22 14:55:08 -05:00
return $this->notFoundResponse("updateCustomer->customer_id");
}
return $response;
}
2024-04-30 22:54:04 -05:00
private function deleteCustomer()
{
if ($this->checkRequestType('DELETE') == 'false') {
2024-05-22 14:55:08 -05:00
$response = $this->unprocessableRequestResponse("deleteCustomer");
return $response;
}
$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();
if (!$result) {
2024-05-22 14:55:08 -05:00
return $this->notFoundResponse("deleteCustomer->findByCustomerId");
2024-04-30 22:54:04 -05:00
}
$response = $this->customerModel->deleteCustomer();
unset($this->customerModel->customerId);
} else {
2024-05-22 14:55:08 -05:00
return $this->notFoundResponse("deleteCustomer->customer_id");
}
return $response;
}
2024-05-09 16:40:42 -05:00
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-15 11:38:59 -05:00
}