CustomerRewardsRESTAPI/Controller/API/CustomerController.php

241 lines
8.9 KiB
PHP
Raw Normal View History

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
* 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
*/
private $customerModel;
private $requestMethod;
private $arrQueryStringParams;
private $strErrorDesc;
private $strErrorHeader;
public $action;
function __construct() {
$this->customerModel = new CustomerModel();
$this->requestMethod = $this->getServerRequestMethod();
$this->arrQueryStringParams = $this->getQueryStringParams();
}
public function processAction()
2024-04-15 11:38:59 -05:00
{
$this->strErrorDesc = '';
try {
switch($this->action) {
case "select":
if (isset($this->arrQueryStringParams['id'])) {
$response = $this->selectByIdAction();
} else {
$response = $this->selectAction();
}
break;
2024-04-15 11:38:59 -05:00
case "insert":
2024-04-30 22:54:04 -05:00
/*$customerModel->first = $arrQueryStringParams['first'];
2024-04-15 11:38:59 -05:00
$customerModel->last = $arrQueryStringParams['last'];
$customerModel->email = $arrQueryStringParams['email'];
$customerModel->phone = $arrQueryStringParams['phone'];
$customerModel->birthday = $arrQueryStringParams['birthday'];
2024-04-20 09:33:27 -05:00
$customerModel->street = $arrQueryStringParams['street'];
2024-04-15 11:38:59 -05:00
$customerModel->city = $arrQueryStringParams['city'];
$customerModel->state = $arrQueryStringParams['state'];
$customerModel->zip = $arrQueryStringParams['zip'];
2024-04-30 22:54:04 -05:00
$customerModel->loyalty = $arrQueryStringParams['loyalty'];*/
2024-04-15 11:38:59 -05:00
2024-04-30 22:54:04 -05:00
$response = $this->insertCustomer();
2024-04-15 11:38:59 -05:00
2024-04-30 22:54:04 -05:00
/*unset($customerModel->first);
2024-04-15 11:38:59 -05:00
unset($customerModel->last);
unset($customerModel->email);
unset($customerModel->phone);
unset($customerModel->birthday);
2024-04-20 09:33:27 -05:00
unset($customerModel->street);
2024-04-15 11:38:59 -05:00
unset($customerModel->city);
unset($customerModel->state);
unset($customerModel->zip);
2024-04-30 22:54:04 -05:00
unset($customerModel->loyalty);*/
2024-04-15 11:38:59 -05:00
break;
case "update":
2024-04-30 22:54:04 -05:00
$response = $this->updateCustomer();
/*$arrCustomer = $this->customerModel->updateCustomer($arrQueryStringParams);*/
2024-04-15 11:38:59 -05:00
break;
case "delete":
2024-04-30 22:54:04 -05:00
/*$arrCustomer = $this->customerModel->deleteCustomer($arrQueryStringParams);*/
$this->customerModel->customerId = $this->arrQueryStringParams['customer_id'];
$response = $this->deleteCustomer();
2024-04-15 11:38:59 -05:00
break;
default:
$strErrorDesc = 'Controller Method not supported for processAction: ' . $action;
2024-04-15 11:38:59 -05:00
$strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
break;
}
$responseData = json_encode($response);
2024-04-15 11:38:59 -05:00
} 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
}
2024-04-15 11:38:59 -05:00
// send output
if (!$strErrorDesc) {
$this->sendOutput(
$responseData,
array('Content-Type: application/json', 'HTTP/1.1 200 OK')
);
} else {
$this->sendOutput(json_encode(array('error' => $strErrorDesc)),
array('Content-Type: application/json', $strErrorHeader)
);
}
}
private function selectAction(){
if ($this->checkRequestType('GET') == 'false') {
$this->strErrorDesc = 'Request Method not supported for processAction';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
return;
}
$this->customerModel->limit = 10;
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 ($this->checkRequestType('GET') == 'false') {
$this->strErrorDesc = 'Request Method not supported for processAction';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
return;
}
2024-04-30 22:54:04 -05:00
if (isset($this->arrQueryStringParams['customerId'])) {
$this->customerModel->customerId = $this->arrQueryStringParams['customerId'];
$response = $this->customerModel->findByCustomerId();
unset($this->customerModel->customerId);
} else {
return $this->notFoundResponse();
}
return $response;
}
private function checkRequestType($request)
{
$response = 'false';
if (strtoupper($this->requestMethod) == $request) {
$response = 'true';
}
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') {
$this->strErrorDesc = 'Request Method not supported for processAction';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
return;
}
// reads the raw POST data and returns it as a string.
$jsonPayload = (array) json_decode(file_get_contents('php://input'), TRUE);
if (! $this->validatePerson($jsonPayload)) {
return $this->unprocessableEntityResponse();
}
2024-04-30 22:54:04 -05:00
$response = $this->customerModel->insertCustomer($jsonPayload);
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') {
$this->strErrorDesc = 'Request Method not supported for processAction';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
return;
}
2024-04-30 22:54:04 -05:00
if (isset($this->arrQueryStringParams['customerId'])) {
$this->customerModel->customerId = $this->arrQueryStringParams['customerId'];
$result = $this->customerModel->findByCustomerId();
if (! $result) {
return $this->notFoundResponse();
}
$input = (array) json_decode(file_get_contents('php://input'), TRUE);
if (! $this->validatePerson($input)) {
return $this->unprocessableEntityResponse();
}
$response = $this->customerModel->updateCustomer($input);
unset($this->customerModel->customerId);
} else {
return $this->notFoundResponse();
}
return $response;
}
2024-04-30 22:54:04 -05:00
private function deleteCustomer()
{
2024-04-30 22:54:04 -05:00
if (isset($this->arrQueryStringParams['customerId'])) {
$this->customerModel->customerId = $this->arrQueryStringParams['customerId'];
$result = $this->customerModel->findByCustomerId();
if (! $result) {
return $this->notFoundResponse();
}
$response = $this->customerModel->deleteCustomer();
unset($this->customerModel->customerId);
} else {
return $this->notFoundResponse();
}
return $response;
}
private function validatePerson($input)
{
2024-04-30 22:54:04 -05:00
if (! isset($input['first'])) {
return false;
}
2024-04-30 22:54:04 -05:00
if (! isset($input['last'])) {
return false;
}
return true;
}
private function unprocessableEntityResponse()
{
$response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Entity';
$response['body'] = json_encode([
'error' => 'Invalid input'
]);
return $response;
}
private function notFoundResponse()
{
$response['status_code_header'] = 'HTTP/1.1 404 Not Found';
$response['body'] = null;
return $response;
}
2024-04-15 11:38:59 -05:00
}