CustomerRewardsRESTAPI/Controller/API/AttributeController.php

103 lines
3.4 KiB
PHP
Raw Normal View History

2024-05-21 12:43:37 -05:00
<?php
/**
* Description of AttributeController
*
* @author Mike Howard
*/
class AttributeController extends BaseController {
public $attributeModel;
public $action;
function __construct() {
parent::__construct();
$this->basename = "AttributeController";
}
public function processAction() {
$this->strErrorDesc = '';
$this->strErrorHeader = '';
try {
$this->requestMethod = $this->getServerRequestMethod();
switch($this->action) {
case "select":
$response = $this->selectAction();
break;
case "insert":
$response = $this->insertAttribute();
break;
case "update":
$response = $this->updateAttribute();
break;
case "delete":
$response = $this->deleteAttribute();
break;
default:
$response = (object) ['Result' => 'Default'];
$this->strErrorDesc = 'Controller Method not supported for processAction: ' . $this->action;
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
break;
}
$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';
}
// 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)),
array('Content-Type: application/json', $this->strErrorHeader)
);
}
}
private function selectAction(){
if ($this->checkRequestType('GET') == 'false') {
$this->strErrorDesc = 'Request Method not supported for processAction';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
return;
}
$this->attributeModel = new AttributeModel();
$this->arrQueryStringParams = $this->getQueryStringParams();
if (isset($this->arrQueryStringParams['loyalty_member_id'])) {
$response = $this->selectByIdAction();
} else {
//$this->attributeModel->limit = 10;
if (isset($this->arrQueryStringParams['limit'])) {
$this->attributeModel->limit = $this->arrQueryStringParams['limit'];
}
$response = $this->attributeModel->findAllAttributes();
unset($this->attributeModel->limit);
}
return $response;
}
private function selectByIdAction(){
if (isset($this->arrQueryStringParams['loyalty_member_id'])) {
$this->attributeModel->attributeId = $this->arrQueryStringParams['loyalty_member_id'];
$response = $this->attributeModel->findAttributesByMemberId();
unset($this->attributeModel->attributeId);
} else {
return $this->notFoundResponse();
}
return $response;
}
}