mirror of
https://github.com/sctn4elk/CustomerRewardsRESTAPI.git
synced 2025-01-10 05:34:29 -06:00
166 lines
5.6 KiB
PHP
166 lines
5.6 KiB
PHP
<?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;
|
|
|
|
function __construct() {
|
|
parent::__construct();
|
|
$this->basename = "AttributeController";
|
|
}
|
|
|
|
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();
|
|
break;
|
|
|
|
case "insert":
|
|
$response = $this->insertMemberAttribute();
|
|
break;
|
|
|
|
case "update":
|
|
$response = $this->updateMemberAttribute();
|
|
break;
|
|
|
|
case "delete":
|
|
$response = $this->deleteMemberAttribute();
|
|
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['loyalty_attribute_name'])) {
|
|
$this->attributeModel->attributeName = $this->arrQueryStringParams['loyalty_attribute_name'];
|
|
$this->attributeModel->memberId = $this->arrQueryStringParams['loyalty_member_id'];
|
|
return $this->attributeModel->findMemberAttributeByAttrName();
|
|
}
|
|
|
|
if (isset($this->arrQueryStringParams['loyalty_member_id'])) {
|
|
$response = $this->selectByMemberIdAction();
|
|
} else {
|
|
$response = $this->attributeModel->findAllAttributes();
|
|
}
|
|
return $response;
|
|
}
|
|
|
|
private function selectByMemberIdAction(){
|
|
if (isset($this->arrQueryStringParams['loyalty_member_id'])) {
|
|
$this->attributeModel->memberId = $this->arrQueryStringParams['loyalty_member_id'];
|
|
$response = $this->attributeModel->findAttributesByMemberId();
|
|
unset($this->attributeModel->memberId);
|
|
} else {
|
|
$response = $this->notFoundResponse("selectByMemberIdAction");
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
private function updateMemberAttribute(){
|
|
if ($this->checkRequestType('PUT') == 'false') {
|
|
$response = $this->unprocessableRequestResponse("updateMemberAttribute");
|
|
return $response;
|
|
}
|
|
$jsonPayload = file_get_contents('php://input');
|
|
$input = json_decode($jsonPayload, TRUE);
|
|
|
|
if (! $this->validateAttribute($input)) {
|
|
return $this->unprocessableEntityResponse("validateAttribute");
|
|
}
|
|
|
|
if ($input['LOYALTY_MEMBER_ID'] != null) {
|
|
$this->attributeModel->memberId = $input['LOYALTY_MEMBER_ID'];
|
|
$this->attributeModel->attributeId = $input['LOYALTY_ATTRIBUTE_ID'];
|
|
|
|
$result = $this->attributeModel->findMemberAttribute();
|
|
if (!$result) {
|
|
return $this->notFoundResponse("attributeModel->findMemberAttribute");
|
|
}
|
|
|
|
$response = $this->attributeModel->updateMemberAttribute($input);
|
|
unset($this->attributeModel->memberId);
|
|
} else {
|
|
return $this->notFoundResponse("attributeModel->LOYALTY_MEMBER_ID");
|
|
}
|
|
return $response;
|
|
}
|
|
|
|
private function insertMemberAttribute(){
|
|
if ($this->checkRequestType('POST') == 'false') {
|
|
$response = $this->unprocessableRequestResponse("insertMemberAttribute");
|
|
return $response;
|
|
}
|
|
|
|
return $this->unprocessableRequestResponse("insertMemberAttribute Not Implemented");
|
|
}
|
|
|
|
private function deleteMemberAttribute(){
|
|
if ($this->checkRequestType('DELETE') == 'false') {
|
|
$response = $this->unprocessableRequestResponse("deleteMemberAttribute");
|
|
return $response;
|
|
}
|
|
|
|
return $this->unprocessableRequestResponse("deleteMemberAttribute Not Implemented");
|
|
}
|
|
|
|
private function validateAttribute($input)
|
|
{
|
|
$validtion = false;
|
|
if($input['LOYALTY_MEMBER_ID'] != null){
|
|
$validtion = true;
|
|
if($input['LOYALTY_ATTRIBUTE_ID'] == null) {
|
|
$validtion = false;
|
|
}
|
|
}
|
|
return $validtion;
|
|
}
|
|
}
|