diff --git a/Controller/API/AttributeController.php b/Controller/API/AttributeController.php new file mode 100644 index 0000000..1650e87 --- /dev/null +++ b/Controller/API/AttributeController.php @@ -0,0 +1,102 @@ +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; + } +} diff --git a/Model/AttributeModel.php b/Model/AttributeModel.php new file mode 100644 index 0000000..2b4a18a --- /dev/null +++ b/Model/AttributeModel.php @@ -0,0 +1,24 @@ +processQuery("SELECT * FROM loyalty_attribute ORDER BY loyalty_attribute_id ASC LIMIT ?", ["i", $this->limit]); + } + + public function findAttributesByMemberId() + { + return $this->processQuery("SELECT * FROM loyalty_member_view WHERE loyalty_member_id = ?", ["i", $this->memberId]); + } +} diff --git a/Model/CustomerModel.php b/Model/CustomerModel.php index 08030a0..d4d9b57 100644 --- a/Model/CustomerModel.php +++ b/Model/CustomerModel.php @@ -1,8 +1,4 @@